2014-05-08 16:59:35 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2015-04-04 17:44:07 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
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-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from .helpers import domain_for_validation
|
|
|
|
from .models import Domain, Record
|
|
|
|
from . import validators
|
|
|
|
|
|
|
|
|
|
|
|
class RecordSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Record
|
|
|
|
fields = ('type', 'value')
|
|
|
|
|
|
|
|
def get_identity(self, data):
|
|
|
|
return data.get('value')
|
|
|
|
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
class DomainSerializer(AccountSerializerMixin, HyperlinkedModelSerializer):
|
2014-05-08 16:59:35 +00:00
|
|
|
""" Validates if this zone generates a correct zone file """
|
2015-05-28 09:43:57 +00:00
|
|
|
records = RecordSerializer(required=False, many=True)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Domain
|
2015-04-30 09:51:55 +00:00
|
|
|
fields = ('url', 'id', 'name', 'records')
|
2014-10-15 21:18:50 +00:00
|
|
|
postonly_fields = ('name',)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-20 15:51:24 +00:00
|
|
|
def clean_name(self, attrs, source):
|
|
|
|
""" prevent users creating subdomains of other users domains """
|
|
|
|
name = attrs[source]
|
2015-09-04 10:22:14 +00:00
|
|
|
parent = Domain.objects.get_parent(name)
|
2015-04-29 21:35:56 +00:00
|
|
|
if parent and parent.account != self.account:
|
2014-10-20 15:51:24 +00:00
|
|
|
raise ValidationError(_("Can not create subdomains of other users domains"))
|
|
|
|
return attrs
|
|
|
|
|
2015-05-18 15:21:42 +00:00
|
|
|
def validate(self, data):
|
2014-05-08 16:59:35 +00:00
|
|
|
""" Checks if everything is consistent """
|
2015-05-18 15:21:42 +00:00
|
|
|
data = super(DomainSerializer, self).validate(data)
|
2015-05-28 10:55:48 +00:00
|
|
|
name = data.get('name')
|
|
|
|
if name:
|
|
|
|
instance = self.instance
|
|
|
|
if instance is None:
|
|
|
|
instance = Domain(name=name, account=self.account)
|
2015-05-18 15:21:42 +00:00
|
|
|
records = data['records']
|
2015-05-28 10:55:48 +00:00
|
|
|
domain = domain_for_validation(instance, records)
|
2014-11-14 15:51:18 +00:00
|
|
|
validators.validate_zone(domain.render_zone())
|
2015-05-18 15:21:42 +00:00
|
|
|
return data
|
2015-05-28 09:43:57 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
records = validated_data.pop('records')
|
|
|
|
domain = super(DomainSerializer, self).create(validated_data)
|
|
|
|
for record in records:
|
|
|
|
domain.records.create(type=record['type'], value=record['value'])
|
|
|
|
return domain
|
|
|
|
|
2015-05-28 10:55:48 +00:00
|
|
|
def update(self, instance, validated_data):
|
2015-05-28 09:43:57 +00:00
|
|
|
precords = validated_data.pop('records')
|
2015-05-28 10:55:48 +00:00
|
|
|
domain = super(DomainSerializer, self).update(instance, validated_data)
|
2015-05-28 09:43:57 +00:00
|
|
|
to_delete = []
|
|
|
|
for erecord in domain.records.all():
|
|
|
|
match = False
|
|
|
|
for ix, precord in enumerate(precords):
|
|
|
|
if erecord.type == precord['type'] and erecord.value == precord['value']:
|
|
|
|
match = True
|
|
|
|
break
|
|
|
|
if match:
|
2015-05-28 10:55:48 +00:00
|
|
|
precords.pop(ix)
|
2015-05-28 09:43:57 +00:00
|
|
|
else:
|
|
|
|
to_delete.append(erecord)
|
|
|
|
for precord in precords:
|
|
|
|
try:
|
|
|
|
recycled = to_delete.pop()
|
|
|
|
except IndexError:
|
|
|
|
domain.records.create(type=precord['type'], value=precord['value'])
|
|
|
|
else:
|
|
|
|
recycled.type = precord['type']
|
|
|
|
recycled.value = precord['value']
|
|
|
|
recycled.save()
|
|
|
|
return domain
|