Source code for goat.serializers
from rest_framework import serializers
from goat.models import *
import json
[docs]class UserSerializer(serializers.ModelSerializer):
[docs]class IdentitySystemSerializer(serializers.ModelSerializer):
added_by = UserSerializer()
[docs]class IdentitySystemLightSerializer(serializers.ModelSerializer):
added_by = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
[docs]class AuthorityLightSerializer(serializers.ModelSerializer):
added_by = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
[docs]class AuthoritySerializer(serializers.ModelSerializer):
"""
Identical to :class:`.AuthorityDetailSerializer` except that the
configuration is hidden.
"""
added_by = UserSerializer()
builtin_identity_system = IdentitySystemLightSerializer()
[docs]class AuthorityDetailSerializer(serializers.ModelSerializer):
added_by = UserSerializer()
builtin_identity_system = IdentitySystemLightSerializer()
[docs] def validate_configuration(self, value):
"""
If a configuration is provided, ensure that it is valid JSON.
"""
if not value:
try:
json.loads(value)
except ValueError:
raise serializers.ValidationError("Configuration must be valid"
"JSON.")
return value
[docs]class ConceptSerializer(serializers.ModelSerializer):
added_by = UserSerializer()
authority = AuthorityLightSerializer()
[docs]class ConceptLightSerializer(serializers.ModelSerializer):
# added_by = UserSerializer()
# authority = AuthorityLightSerializer()
authority = serializers.PrimaryKeyRelatedField(queryset=Authority.objects.all(), allow_null=True)
concept_type = serializers.PrimaryKeyRelatedField(queryset=Concept.objects.all(), allow_null=True)
[docs]class ConceptRepresentationMixin(serializers.ModelSerializer):
concepts = serializers.PrimaryKeyRelatedField(queryset=Concept.objects.all(), many=True)
[docs] def to_internal_value(self, data):
"""
Concepts can be passed as IDs, or as URIs.
"""
concepts = []
for concept in data.getlist('concepts', []):
if type(concept) in [str, unicode] and concept.startswith('http'):
try:
concepts.append(Concept.objects.get(identifier=concept).id)
except Concept.DoesNotExist:
raise serializers.ValidationError({
'concepts': 'No such concept: %s' % concept
})
else:
concepts.append(concept)
data.setlist('concepts', concepts)
return super(ConceptRepresentationMixin, self).to_internal_value(data)
[docs] def to_representation(self, obj):
"""
Concepts should be represented as URIs.
"""
concepts = obj.concepts.values_list('identifier', flat=True)
data = super(ConceptRepresentationMixin, self).to_representation(obj)
data['concepts'] = concepts
return data
[docs]class IdentityLightSerializer(ConceptRepresentationMixin, serializers.ModelSerializer):
part_of = serializers.PrimaryKeyRelatedField(queryset=IdentitySystem.objects.all())
concepts = serializers.PrimaryKeyRelatedField(queryset=Concept.objects.all(), many=True)
[docs]class IdentitySerializer(ConceptRepresentationMixin, serializers.ModelSerializer):
added_by = UserSerializer()
part_of = IdentitySystemLightSerializer()