django-orchestra/orchestra/apps/domains/serializers.py

42 lines
1.3 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.core.exceptions import ValidationError
from rest_framework import serializers
from orchestra.apps.accounts.serializers import AccountSerializerMixin
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')
class DomainSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
""" Validates if this zone generates a correct zone file """
records = RecordSerializer(required=False, many=True, allow_add_remove=True)
class Meta:
model = Domain
fields = ('url', 'id', 'name', 'records')
def full_clean(self, instance):
""" Checks if everything is consistent """
instance = super(DomainSerializer, self).full_clean(instance)
if instance and instance.name:
records = self.init_data['records']
domain = domain_for_validation(instance, records)
try:
validators.validate_zone(domain.render_zone())
except ValidationError as err:
2014-10-10 14:39:46 +00:00
self._errors = {
'all': err.message
}
2014-05-08 16:59:35 +00:00
return None
return instance