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

108 lines
3.7 KiB
Python
Raw Permalink Normal View History

2021-06-18 09:11:50 +00:00
from django.db import transaction
2014-08-22 15:31:44 +00:00
from rest_framework import serializers
2015-05-18 15:21:42 +00:00
from orchestra.api.serializers import SetPasswordHyperlinkedSerializer, RelatedHyperlinkedModelSerializer
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
2015-05-18 15:21:42 +00:00
class RelatedDomainSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
2014-10-17 10:04:47 +00:00
class Meta:
model = Address.domain.field.related_model
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name')
2014-10-17 10:04:47 +00:00
class RelatedAddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
domain = RelatedDomainSerializer()
2014-10-17 10:04:47 +00:00
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-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
2021-10-14 11:03:08 +00:00
class AddressRelatedField(serializers.HyperlinkedRelatedField):
# Filter addresses by account (user)
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(account=self.context['account'])
class MailboxWritableSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
2021-10-14 11:03:08 +00:00
addresses = AddressRelatedField(many=True, view_name='address-detail', queryset=Address.objects.all())
class Meta:
model = Mailbox
fields = (
'url', 'id', 'name', 'password', 'filtering', 'custom_filtering', 'addresses', 'is_active'
)
postonly_fields = ('name', 'password')
2021-10-14 11:03:08 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['addresses'].context['account'] = self.account
@transaction.atomic
def create(self, validated_data):
addresses = validated_data.pop('addresses', [])
instance = super().create(validated_data)
instance.addresses.set(addresses)
return instance
@transaction.atomic
def update(self, instance, validated_data):
addresses = validated_data.pop('addresses', [])
instance.addresses.set(addresses)
return super().update(instance, validated_data)
2015-05-18 15:21:42 +00:00
class RelatedMailboxSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
class Meta:
model = Mailbox
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name')
2014-08-22 15:31:44 +00:00
class AddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
domain = RelatedDomainSerializer()
2021-06-18 09:11:50 +00:00
mailboxes = RelatedMailboxSerializer(many=True, required=False)
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-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)
2021-06-18 09:11:50 +00:00
mailboxes = attrs.get('mailboxes', [])
forward = attrs.get('forward', '')
if not mailboxes and not 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
2021-06-08 11:37:00 +00:00
2021-06-18 09:11:50 +00:00
@transaction.atomic
2021-06-08 11:37:00 +00:00
def create(self, validated_data):
2021-06-18 09:11:50 +00:00
mailboxes = validated_data.pop('mailboxes', [])
obj = super().create(validated_data)
2021-06-08 11:37:00 +00:00
obj.mailboxes.set(mailboxes)
return obj
2021-06-18 09:11:50 +00:00
@transaction.atomic
def update(self, instance, validated_data):
mailboxes = validated_data.pop('mailboxes', [])
instance.mailboxes.set(mailboxes)
return super().update(instance, validated_data)