2014-10-09 17:04:12 +00:00
|
|
|
from django.forms import widgets
|
2015-04-04 18:10:39 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-10-16 15:11:52 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2014-05-08 16:59:35 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
from orchestra.api.serializers import HyperlinkedModelSerializer
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.serializers import AccountSerializerMixin
|
2014-10-09 17:04:12 +00:00
|
|
|
from orchestra.core.validators import validate_password
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from .models import List
|
|
|
|
|
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
class RelatedDomainSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = List.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'])
|
|
|
|
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
class ListSerializer(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-16 15:11:52 +00:00
|
|
|
address_domain = RelatedDomainSerializer(required=False)
|
2014-10-09 17:04:12 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = List
|
2014-10-09 17:04:12 +00:00
|
|
|
fields = ('url', 'name', 'address_name', 'address_domain', 'admin_email')
|
2014-10-15 21:18:50 +00:00
|
|
|
postonly_fields = ('name',)
|
2014-10-09 17:04:12 +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
|
|
|
|
|
2014-10-20 15:51:24 +00:00
|
|
|
def validate_address_domain(self, attrs, source):
|
|
|
|
address_domain = attrs.get(source)
|
|
|
|
address_name = attrs.get('address_name')
|
2014-10-16 15:11:52 +00:00
|
|
|
if self.object:
|
|
|
|
address_domain = address_domain or self.object.address_domain
|
|
|
|
address_name = address_name or self.object.address_name
|
2014-10-20 15:51:24 +00:00
|
|
|
if address_name and not address_domain:
|
2014-10-16 15:11:52 +00:00
|
|
|
raise serializers.ValidationError(
|
2014-10-20 15:51:24 +00:00
|
|
|
_("address_domains should should be provided when providing an addres_name"))
|
2014-10-16 15:11:52 +00:00
|
|
|
return attrs
|
|
|
|
|
2014-10-09 17:04:12 +00:00
|
|
|
def save_object(self, obj, **kwargs):
|
|
|
|
if not obj.pk:
|
|
|
|
obj.set_password(self.init_data.get('password', ''))
|
|
|
|
super(ListSerializer, self).save_object(obj, **kwargs)
|