Source code for goat.serializers

from rest_framework import serializers

from goat.models import *

import json


[docs]class UserSerializer(serializers.ModelSerializer):
[docs] class Meta: model = User fields = ('username', 'email')
[docs]class IdentitySystemSerializer(serializers.ModelSerializer): added_by = UserSerializer()
[docs] class Meta: model = IdentitySystem fields = '__all__'
[docs]class IdentitySystemLightSerializer(serializers.ModelSerializer): added_by = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
[docs] class Meta: model = IdentitySystem fields = ('id', 'name', 'added_by')
[docs]class AuthorityLightSerializer(serializers.ModelSerializer): added_by = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
[docs] class Meta: model = Authority fields = ('id', 'name', 'added_by')
[docs]class AuthoritySerializer(serializers.ModelSerializer): """ Identical to :class:`.AuthorityDetailSerializer` except that the configuration is hidden. """ added_by = UserSerializer() builtin_identity_system = IdentitySystemLightSerializer()
[docs] class Meta: model = Authority exclude = ('configuration',)
[docs]class AuthorityDetailSerializer(serializers.ModelSerializer): added_by = UserSerializer() builtin_identity_system = IdentitySystemLightSerializer()
[docs] class Meta: model = Authority fields = '__all__'
[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 Meta: model = Concept exclude = ('data', )
[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 Meta: model = Concept exclude = ('data', )
[docs]class ConceptRepresentationMixin(serializers.ModelSerializer): concepts = serializers.PrimaryKeyRelatedField(queryset=Concept.objects.all(), many=True)
[docs] class Meta: model = Identity fields = '__all__'
[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 Meta: model = Identity fields = '__all__'
[docs]class IdentitySerializer(ConceptRepresentationMixin, serializers.ModelSerializer): added_by = UserSerializer() part_of = IdentitySystemLightSerializer()
[docs] class Meta: model = Identity fields = '__all__'