|
| 1 | +from django.contrib.contenttypes.models import ContentType |
| 2 | +from rest_framework import serializers |
| 3 | + |
| 4 | +from datahub.company.models import ( |
| 5 | + Advisor, |
| 6 | + Company, |
| 7 | +) |
| 8 | +from datahub.core.serializers import NestedRelatedField |
| 9 | +from datahub.documents.models import ( |
| 10 | + GenericDocument, |
| 11 | + SharePointDocument, |
| 12 | +) |
| 13 | +from datahub.documents.utils import format_content_type |
| 14 | + |
| 15 | + |
| 16 | +class SharePointDocumentSerializer(serializers.ModelSerializer): |
| 17 | + |
| 18 | + created_by = NestedRelatedField(Advisor, extra_fields=['name', 'email']) |
| 19 | + modified_by = NestedRelatedField(Advisor, extra_fields=['name', 'email']) |
| 20 | + |
| 21 | + class Meta: |
| 22 | + model = SharePointDocument |
| 23 | + fields = '__all__' |
| 24 | + |
| 25 | + |
| 26 | +class DocumentRelatedField(serializers.RelatedField): |
| 27 | + """Serializer field for the GenericDocument.document field. |
| 28 | +
|
| 29 | + Currently, only SharePointDocument objects are supported. |
| 30 | +
|
| 31 | + To add support for another document type, add an elif statement to the to_representation |
| 32 | + method to check for the new model and set the serializer accordingly. |
| 33 | +
|
| 34 | + For example: |
| 35 | +
|
| 36 | + ``` |
| 37 | + elif isinstance(instance, YourDocumentModel): |
| 38 | + serializer = YourDocumentSerializer(instance) |
| 39 | + ``` |
| 40 | + """ |
| 41 | + |
| 42 | + def to_representation(self, instance): |
| 43 | + """Convert model instance to built-in Python (JSON friendly) data types.""" |
| 44 | + if isinstance(instance, SharePointDocument): |
| 45 | + serializer = SharePointDocumentSerializer(instance) |
| 46 | + else: |
| 47 | + raise Exception(f'Unexpected document type: {type(instance)}') |
| 48 | + return serializer.data |
| 49 | + |
| 50 | + |
| 51 | +class RelatedObjectRelatedField(serializers.RelatedField): |
| 52 | + """Serializer field for the GenericDocument.related_object field. |
| 53 | +
|
| 54 | + Currently, only Company objects are support. |
| 55 | +
|
| 56 | + To add support for another type of related object, add the model to the tuple |
| 57 | + in the `isinstance` call in the to_representation method - e.g. |
| 58 | + `isinstance(instance, (Company, YourModel, ...))`. The model must contain the fields |
| 59 | + `id` and `name`, otherwise, you will need to add an elif statement and customise |
| 60 | + the return object accordingly. |
| 61 | + """ |
| 62 | + |
| 63 | + def to_representation(self, instance): |
| 64 | + """Convert model instance to built-in Python (JSON friendly) data types.""" |
| 65 | + if isinstance(instance, (Company)): |
| 66 | + return { |
| 67 | + 'id': str(instance.id), |
| 68 | + 'name': instance.name, |
| 69 | + } |
| 70 | + content_type = ContentType.objects.get_for_model(instance) |
| 71 | + raise Exception(f'Unexpected type of related object: {format_content_type(content_type)}') |
| 72 | + |
| 73 | + |
| 74 | +class GenericDocumentRetrieveSerializer(serializers.ModelSerializer): |
| 75 | + """Serializer for retrieving Generic Document objects.""" |
| 76 | + |
| 77 | + created_by = NestedRelatedField(Advisor, extra_fields=['name', 'email']) |
| 78 | + modified_by = NestedRelatedField(Advisor, extra_fields=['name', 'email']) |
| 79 | + document = DocumentRelatedField(read_only=True) |
| 80 | + related_object = RelatedObjectRelatedField(read_only=True) |
| 81 | + |
| 82 | + class Meta: |
| 83 | + model = GenericDocument |
| 84 | + fields = '__all__' |
| 85 | + |
| 86 | + def to_representation(self, instance): |
| 87 | + """Convert model instance to built-in Python (JSON friendly) data types.""" |
| 88 | + representation = super().to_representation(instance) |
| 89 | + representation.update({ |
| 90 | + 'document_type': format_content_type(instance.document_type), |
| 91 | + 'related_object_type': format_content_type(instance.related_object_type), |
| 92 | + }) |
| 93 | + return representation |
0 commit comments