django-orchestra-test/orchestra/contrib/lists/serializers.py

45 lines
1.8 KiB
Python
Raw Normal View History

2015-04-23 14:34:04 +00:00
from django.core.validators import RegexValidator
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-05-08 16:59:35 +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-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
2015-05-18 15:21:42 +00:00
class RelatedDomainSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
class Meta:
model = List.address_domain.field.model
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'name')
2015-04-23 14:34:04 +00:00
class ListSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
2014-10-09 17:04:12 +00:00
password = serializers.CharField(max_length=128, label=_('Password'),
2015-04-23 14:34:04 +00:00
write_only=True, style={'widget': widgets.PasswordInput},
validators=[
validate_password,
RegexValidator(r'^[^"\'\\]+$',
_('Enter a valid password. '
'This value may contain any ascii character except for '
' \'/"/\\/ characters.'), 'invalid'),
])
address_domain = RelatedDomainSerializer(required=False)
2014-05-08 16:59:35 +00:00
class Meta:
model = List
fields = ('url', 'id', 'name', 'password', 'address_name', 'address_domain', 'admin_email', 'is_active',)
2015-04-23 14:34:04 +00:00
postonly_fields = ('name', 'password')
2015-07-29 09:05:07 +00:00
def validate_address_domain(self, address_name):
2015-04-23 14:34:04 +00:00
if self.instance:
address_domain = address_domain or self.instance.address_domain
address_name = address_name or self.instance.address_name
2014-10-20 15:51:24 +00:00
if address_name and not address_domain:
raise serializers.ValidationError(
2014-10-20 15:51:24 +00:00
_("address_domains should should be provided when providing an addres_name"))
2015-07-29 09:05:07 +00:00
return address_name