class BadgeClassSerializerV1_0(serializers.Serializer): name = BadgeStringField(required=True) description = BadgeStringField(required=True) image = BadgeImageURLOrDataURIField(required=True) criteria = BadgeURLField(required=True) issuer = serializers.URLField(write_only=True, required=True) alignment = serializers.ListField( child=AlignmentObjectSerializer(), required=False, write_only=True) # TODO: implement to_representation tags = serializers.ListField(child=BadgeStringField(), required=False) def to_representation(self, badge): obj = ObjectView(dict(badge)) badge_props = super(BadgeClassSerializerV1_0, self).to_representation(obj) header = OrderedDict() if not self.context.get('embedded', False): header['@context'] = 'https://w3id.org/openbadges/v1' header['type'] = 'BadgeClass' header['id'] = self.context.get('instance').badge_url result = OrderedDict(header.items() + badge_props.items()) issuer_serializer_class = self.get_issuer_serializer_class() issuer_serializer = issuer_serializer_class( self.context.get('instance').issuer, context=self.context) result['issuer'] = issuer_serializer.data return result def get_issuer_serializer_class(self): return IssuerSerializerV1_0
class BadgeClassSerializerV0_5(serializers.Serializer): """ A 0.5 Open Badge assertion embedded a representation of the accomplishment awarded. """ version = serializers.ChoiceField(['0.5.0'], write_only=True, required=False) name = BadgeStringField(required=True) description = BadgeStringField(required=True) image = BadgeImageURLField(required=True) criteria = BadgeURLField(required=True) issuer = IssuerSerializerV0_5(write_only=True, required=True) def to_representation(self, badge): obj = ObjectView(dict(badge)) badge_props = super(BadgeClassSerializerV0_5, self).to_representation(obj) header = OrderedDict() if not self.context.get('embedded', False): header['@context'] = 'https://w3id.org/openbadges/v1' header['type'] = 'BadgeClass' result = OrderedDict(header.items() + badge_props.items()) issuer_serializer = IssuerSerializerV0_5(badge.get('issuer'), context=self.context) result['issuer'] = issuer_serializer.data return result
class IssuerSerializerV0_5(serializers.Serializer): """ A representation of a badge's issuing organization is found embedded in 0.5 badge assertions. """ origin = BadgeURLField(required=True) name = BadgeStringField(required=True) org = BadgeStringField(write_only=True, required=False) contact = BadgeEmailField(required=False) def to_representation(self, issuer): obj = ObjectView(dict(issuer)) issuer_props = super(IssuerSerializerV0_5, self).to_representation(obj) # Update old keys to new ones for prop in (('origin', 'url'), ('contact', 'email')): if issuer_props.get(prop[0]) is not None: issuer_props[prop[1]] = issuer_props.pop(prop[0]) header = OrderedDict() if not self.context.get('embedded', False): header['@context'] = 'https://w3id.org/openbadges/v1' header['type'] = 'Issuer' result = OrderedDict(header.items() + issuer_props.items()) return result
class BadgeInstanceSerializerV0_5_1(V0_5Base): """ Serializer for 0.5 Open Badge assertions that have hashed recipient email addresses. """ recipient = HashString(write_only=True, required=True) salt = BadgeStringField(write_only=True, required=False)
def get_fields(self): fields = super(BadgeInstanceSerializerV1_1, self).get_fields() fields.update({ ('@context', BadgeStringField(required=True, required_value='https://w3id.org/openbadges/v1')) }) return fields
class IssuerSerializerV1_0(serializers.Serializer): name = BadgeStringField(required=True) url = BadgeURLField(required=True) description = BadgeStringField(required=False) email = BadgeEmailField(required=False) image = BadgeImageURLOrDataURIField(required=False) revocationList = BadgeURLField(required=False) def to_representation(self, badge): obj = ObjectView(dict(badge)) issuer_props = super(IssuerSerializerV1_0, self).to_representation(obj) header = OrderedDict() if not self.context.get('embedded', False): header['@context'] = 'https://w3id.org/openbadges/v1' header['type'] = 'Issuer' header['id'] = self.context.get('instance').issuer_url result = OrderedDict(header.items() + issuer_props.items()) return result
class BadgeInstanceSerializerV1_0(serializers.Serializer): """ Serializer for 1.0 Open Badge assertions, which require a uid, have no linked data context. """ uid = BadgeStringField(required=True) recipient = RecipientSerializer(required=True) badge = serializers.URLField(write_only=True, required=True) issuedOn = BadgeDateTimeField(required=True) verify = VerificationObjectSerializer(write_only=True, required=True) image = BadgeImageURLField(required=False) expires = BadgeDateTimeField(required=False) evidence = BadgeURLField(required=False) def to_representation(self, instance): """ Converts a 1.0 Badge Instance to serialized verbose v1.1 style output """ obj = ObjectView(instance.json) self.context['recipient_id'] = instance.recipient_id instance_props = super(BadgeInstanceSerializerV1_0, self).to_representation(obj) header = OrderedDict() if not self.context.get('embedded', False): header['@context'] = 'https://w3id.org/openbadges/v1' header['type'] = 'Assertion' header['id'] = instance.instance_url result = OrderedDict(header.items() + instance_props.items()) badge_class_serializer_class = self.get_badge_class_serializer_class() badge_class_serializer = badge_class_serializer_class(instance.badge, context={ 'instance': instance, 'embedded': True }) result['badge'] = badge_class_serializer.data return result def get_badge_class_serializer_class(self): return BadgeClassSerializerV1_0