django-orchestra/orchestra/contrib/mailboxes/serializers.py

68 lines
2.5 KiB
Python
Raw Normal View History

2014-10-09 17:04:12 +00:00
from django.forms import widgets
from django.shortcuts import get_object_or_404
2015-04-04 18:10:39 +00:00
from django.utils.translation import ugettext_lazy as _
2014-08-22 15:31:44 +00:00
from rest_framework import serializers
2015-04-23 14:34:04 +00:00
from orchestra.api.serializers import SetPasswordHyperlinkedSerializer
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.serializers import AccountSerializerMixin
2014-08-22 15:31:44 +00:00
from .models import Mailbox, Address
2014-10-17 10:04:47 +00:00
class RelatedDomainSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = Address.domain.field.rel.to
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name')
2014-10-17 10:04:47 +00:00
def from_native(self, data, files=None):
queryset = self.opts.model.objects.filter(account=self.account)
return get_object_or_404(queryset, name=data['name'])
class RelatedAddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
domain = RelatedDomainSerializer()
class Meta:
model = Address
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name', 'domain', 'forward')
2015-04-23 19:46:23 +00:00
#
# def from_native(self, data, files=None):
# queryset = self.opts.model.objects.filter(account=self.account)
# return get_object_or_404(queryset, name=data['name'])
2014-10-17 10:04:47 +00:00
2015-04-23 14:34:04 +00:00
class MailboxSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
2014-10-17 10:04:47 +00:00
addresses = RelatedAddressSerializer(many=True, read_only=True)
2014-10-09 17:04:12 +00:00
2014-08-22 15:31:44 +00:00
class Meta:
model = Mailbox
2014-10-09 17:04:12 +00:00
fields = (
2015-04-30 09:51:55 +00:00
'url', 'id', 'name', 'password', 'filtering', 'custom_filtering', 'addresses', 'is_active'
2014-10-09 17:04:12 +00:00
)
2015-04-23 14:34:04 +00:00
postonly_fields = ('name', 'password')
2014-08-22 15:31:44 +00:00
class RelatedMailboxSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = Mailbox
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name')
def from_native(self, data, files=None):
queryset = self.opts.model.objects.filter(account=self.account)
return get_object_or_404(queryset, name=data['name'])
2014-08-22 15:31:44 +00:00
class AddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
domain = RelatedDomainSerializer()
2015-04-23 14:34:04 +00:00
mailboxes = RelatedMailboxSerializer(many=True, required=False) #allow_add_remove=True
2014-08-22 15:31:44 +00:00
class Meta:
model = Address
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name', 'domain', 'mailboxes', 'forward')
2014-08-22 15:31:44 +00:00
2014-10-06 14:57:02 +00:00
def validate(self, attrs):
2015-04-23 14:34:04 +00:00
attrs = super(AddressSerializer, self).validate(attrs)
2014-10-06 14:57:02 +00:00
if not attrs['mailboxes'] and not attrs['forward']:
2014-11-09 10:16:07 +00:00
raise serializers.ValidationError("A mailbox or forward address should be provided.")
2014-10-06 14:57:02 +00:00
return attrs