2015-02-25 17:29:39 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
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
|
|
|
|
|
|
|
|
from orchestra.api.fields import OptionField
|
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-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from .models import Website, Content
|
2015-03-10 22:27:32 +00:00
|
|
|
from .validators import validate_domain_protocol
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
class RelatedDomainSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Website.domains.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 RelatedWebAppSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
2015-03-23 15:36:51 +00:00
|
|
|
# model = Content.webapp.field.rel.to
|
2014-10-16 15:11:52 +00:00
|
|
|
fields = ('url', 'name', 'type')
|
|
|
|
|
|
|
|
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-05-08 16:59:35 +00:00
|
|
|
class ContentSerializer(serializers.HyperlinkedModelSerializer):
|
2014-10-16 15:11:52 +00:00
|
|
|
webapp = RelatedWebAppSerializer()
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = Content
|
|
|
|
fields = ('webapp', 'path')
|
2014-10-14 13:50:19 +00:00
|
|
|
|
|
|
|
def get_identity(self, data):
|
|
|
|
return '%s-%s' % (data.get('website'), data.get('path'))
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
class WebsiteSerializer(AccountSerializerMixin, HyperlinkedModelSerializer):
|
2014-10-16 15:11:52 +00:00
|
|
|
domains = RelatedDomainSerializer(many=True, allow_add_remove=True, required=False)
|
2014-05-08 16:59:35 +00:00
|
|
|
contents = ContentSerializer(required=False, many=True, allow_add_remove=True,
|
2015-03-10 16:57:23 +00:00
|
|
|
source='content_set')
|
2015-03-23 15:36:51 +00:00
|
|
|
directives = OptionField(required=False)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Website
|
2015-03-23 15:36:51 +00:00
|
|
|
fields = ('url', 'name', 'protocol', 'domains', 'is_active', 'contents', 'directives')
|
2014-10-15 21:18:50 +00:00
|
|
|
postonly_fileds = ('name',)
|
2015-02-25 17:29:39 +00:00
|
|
|
|
|
|
|
def full_clean(self, instance):
|
2015-03-23 15:36:51 +00:00
|
|
|
""" Prevent multiples domains on the same protocol """
|
2015-02-25 17:29:39 +00:00
|
|
|
for domain in instance._m2m_data['domains']:
|
2015-03-10 22:27:32 +00:00
|
|
|
try:
|
|
|
|
validate_domain_protocol(instance, domain, instance.protocol)
|
|
|
|
except ValidationError as e:
|
|
|
|
# TODO not sure about this one
|
|
|
|
self.add_error(None, e)
|
2015-02-25 17:29:39 +00:00
|
|
|
return instance
|
|
|
|
|