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

86 lines
3.2 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
2014-10-09 17:04:12 +00:00
from django.utils.translation import ugettext, ugettext_lazy as _
2014-08-22 15:31:44 +00:00
from rest_framework import serializers
2014-10-15 21:18:50 +00:00
from orchestra.api.serializers import HyperlinkedModelSerializer
2014-08-22 15:31:44 +00:00
from orchestra.apps.accounts.serializers import AccountSerializerMixin
2014-10-09 17:04:12 +00:00
from orchestra.core.validators import validate_password
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
fields = ('url', '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'])
class RelatedAddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
domain = RelatedDomainSerializer()
class Meta:
model = Address
fields = ('url', 'name', 'domain', 'forward')
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-15 21:18:50 +00:00
class MailboxSerializer(AccountSerializerMixin, HyperlinkedModelSerializer):
2014-10-09 17:04:12 +00:00
password = serializers.CharField(max_length=128, label=_('Password'),
validators=[validate_password], write_only=True, required=False,
widget=widgets.PasswordInput)
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 = (
'url', 'name', 'password', 'filtering', 'custom_filtering', 'addresses', 'is_active'
)
2014-10-15 21:18:50 +00:00
postonly_fields = ('name',)
2014-10-06 14:57:02 +00:00
def validate_password(self, attrs, source):
""" POST only password """
if self.object:
if 'password' in attrs:
raise serializers.ValidationError(_("Can not set password"))
elif 'password' not in attrs:
raise serializers.ValidationError(_("Password required"))
return attrs
def save_object(self, obj, **kwargs):
# FIXME this method will be called when saving nested serializers :(
if not obj.pk:
obj.set_password(obj.password)
super(MailboxSerializer, self).save_object(obj, **kwargs)
2014-08-22 15:31:44 +00:00
class RelatedMailboxSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = Mailbox
fields = ('url', '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()
mailboxes = RelatedMailboxSerializer(many=True, allow_add_remove=True, required=False)
2014-08-22 15:31:44 +00:00
class Meta:
model = Address
fields = ('url', 'name', 'domain', 'mailboxes', 'forward')
2014-10-06 14:57:02 +00:00
def validate(self, attrs):
if not attrs['mailboxes'] and not attrs['forward']:
2014-10-07 13:08:59 +00:00
raise serializers.ValidationError("mailboxes or forward addresses should be provided")
2014-10-06 14:57:02 +00:00
return attrs