We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Expose timestamps associated with annotations with the API
The `..Annotation` models currently stores the `created_at` and `updated_at` fields but they are not exposed by the API.

I'd like to propose exposing them through the API so that downstream analysis can be conducted using simple API calls, like those made using [`doccano-client`](https://github.com/doccano/doccano-client), for instance.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `app/api/serializers.py`
Content:
```
1 from django.conf import settings
2 from django.contrib.auth import get_user_model
3 from django.shortcuts import get_object_or_404
4 from rest_framework import serializers
5 from rest_polymorphic.serializers import PolymorphicSerializer
6 from rest_framework.exceptions import ValidationError
7 
8 
9 from .models import Label, Project, Document, RoleMapping, Role
10 from .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject, Speech2textProject
11 from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation, Speech2textAnnotation
12 
13 
14 class UserSerializer(serializers.ModelSerializer):
15 
16     class Meta:
17         model = get_user_model()
18         fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')
19 
20 
21 class LabelSerializer(serializers.ModelSerializer):
22 
23     def validate(self, attrs):
24         prefix_key = attrs.get('prefix_key')
25         suffix_key = attrs.get('suffix_key')
26 
27         # In the case of user don't set any shortcut key.
28         if prefix_key is None and suffix_key is None:
29             return super().validate(attrs)
30 
31         # Don't allow shortcut key not to have a suffix key.
32         if prefix_key and not suffix_key:
33             raise ValidationError('Shortcut key may not have a suffix key.')
34 
35         # Don't allow to save same shortcut key when prefix_key is null.
36         try:
37             context = self.context['request'].parser_context
38             project_id = context['kwargs']['project_id']
39             label_id = context['kwargs'].get('label_id')
40         except (AttributeError, KeyError):
41             pass  # unit tests don't always have the correct context set up
42         else:
43             conflicting_labels = Label.objects.filter(
44                 suffix_key=suffix_key,
45                 prefix_key=prefix_key,
46                 project=project_id,
47             )
48 
49             if label_id is not None:
50                 conflicting_labels = conflicting_labels.exclude(id=label_id)
51 
52             if conflicting_labels.exists():
53                 raise ValidationError('Duplicate shortcut key.')
54 
55         return super().validate(attrs)
56 
57     class Meta:
58         model = Label
59         fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')
60 
61 
62 class DocumentSerializer(serializers.ModelSerializer):
63     annotations = serializers.SerializerMethodField()
64     annotation_approver = serializers.SerializerMethodField()
65 
66     def get_annotations(self, instance):
67         request = self.context.get('request')
68         project = instance.project
69         model = project.get_annotation_class()
70         serializer = project.get_annotation_serializer()
71         annotations = model.objects.filter(document=instance.id)
72         if request and not project.collaborative_annotation:
73             annotations = annotations.filter(user=request.user)
74         serializer = serializer(annotations, many=True)
75         return serializer.data
76 
77     @classmethod
78     def get_annotation_approver(cls, instance):
79         approver = instance.annotations_approved_by
80         return approver.username if approver else None
81 
82     class Meta:
83         model = Document
84         fields = ('id', 'text', 'annotations', 'meta', 'annotation_approver')
85 
86 
87 class ApproverSerializer(DocumentSerializer):
88 
89     class Meta:
90         model = Document
91         fields = ('id', 'annotation_approver')
92 
93 
94 class ProjectSerializer(serializers.ModelSerializer):
95     current_users_role = serializers.SerializerMethodField()
96 
97     def get_current_users_role(self, instance):
98         role_abstractor = {
99             "is_project_admin": settings.ROLE_PROJECT_ADMIN,
100             "is_annotator": settings.ROLE_ANNOTATOR,
101             "is_annotation_approver": settings.ROLE_ANNOTATION_APPROVER,
102         }
103         queryset = RoleMapping.objects.values("role_id__name")
104         if queryset:
105             users_role = get_object_or_404(
106                 queryset, project=instance.id, user=self.context.get("request").user.id
107             )
108             for key, val in role_abstractor.items():
109                 role_abstractor[key] = users_role["role_id__name"] == val
110         return role_abstractor
111 
112     class Meta:
113         model = Project
114         fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image',
115                   'updated_at', 'randomize_document_order', 'collaborative_annotation', 'single_class_classification')
116         read_only_fields = ('image', 'updated_at', 'users', 'current_users_role')
117 
118 
119 class TextClassificationProjectSerializer(ProjectSerializer):
120 
121     class Meta:
122         model = TextClassificationProject
123         fields = ProjectSerializer.Meta.fields
124         read_only_fields = ProjectSerializer.Meta.read_only_fields
125 
126 
127 class SequenceLabelingProjectSerializer(ProjectSerializer):
128 
129     class Meta:
130         model = SequenceLabelingProject
131         fields = ProjectSerializer.Meta.fields
132         read_only_fields = ProjectSerializer.Meta.read_only_fields
133 
134 
135 class Seq2seqProjectSerializer(ProjectSerializer):
136 
137     class Meta:
138         model = Seq2seqProject
139         fields = ProjectSerializer.Meta.fields
140         read_only_fields = ProjectSerializer.Meta.read_only_fields
141 
142 
143 class Speech2textProjectSerializer(ProjectSerializer):
144 
145     class Meta:
146         model = Speech2textProject
147         fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image',
148                   'updated_at', 'randomize_document_order')
149         read_only_fields = ('image', 'updated_at', 'users', 'current_users_role')
150 
151 
152 class ProjectPolymorphicSerializer(PolymorphicSerializer):
153     model_serializer_mapping = {
154         Project: ProjectSerializer,
155         TextClassificationProject: TextClassificationProjectSerializer,
156         SequenceLabelingProject: SequenceLabelingProjectSerializer,
157         Seq2seqProject: Seq2seqProjectSerializer,
158         Speech2textProject: Speech2textProjectSerializer,
159     }
160 
161 
162 class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
163 
164     def get_queryset(self):
165         view = self.context.get('view', None)
166         request = self.context.get('request', None)
167         queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
168         if not request or not queryset or not view:
169             return None
170         return queryset.filter(project=view.kwargs['project_id'])
171 
172 
173 class DocumentAnnotationSerializer(serializers.ModelSerializer):
174     # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
175     label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
176     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
177 
178     class Meta:
179         model = DocumentAnnotation
180         fields = ('id', 'prob', 'label', 'user', 'document')
181         read_only_fields = ('user', )
182 
183 
184 class SequenceAnnotationSerializer(serializers.ModelSerializer):
185     #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
186     label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
187     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
188 
189     class Meta:
190         model = SequenceAnnotation
191         fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
192         read_only_fields = ('user',)
193 
194 
195 class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
196     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
197 
198     class Meta:
199         model = Seq2seqAnnotation
200         fields = ('id', 'text', 'user', 'document', 'prob')
201         read_only_fields = ('user',)
202 
203 
204 class Speech2textAnnotationSerializer(serializers.ModelSerializer):
205     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
206 
207     class Meta:
208         model = Speech2textAnnotation
209         fields = ('id', 'prob', 'text', 'user', 'document')
210         read_only_fields = ('user',)
211 
212 
213 class RoleSerializer(serializers.ModelSerializer):
214     class Meta:
215         model = Role
216         fields = ('id', 'name')
217 
218 
219 class RoleMappingSerializer(serializers.ModelSerializer):
220     username = serializers.SerializerMethodField()
221     rolename = serializers.SerializerMethodField()
222 
223     @classmethod
224     def get_username(cls, instance):
225         user = instance.user
226         return user.username if user else None
227 
228     @classmethod
229     def get_rolename(cls, instance):
230         role = instance.role
231         return role.name if role else None
232 
233     class Meta:
234         model = RoleMapping
235         fields = ('id', 'user', 'role', 'username', 'rolename')
236
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/app/api/serializers.py b/app/api/serializers.py
--- a/app/api/serializers.py
+++ b/app/api/serializers.py
@@ -177,7 +177,7 @@
 
     class Meta:
         model = DocumentAnnotation
-        fields = ('id', 'prob', 'label', 'user', 'document')
+        fields = ('id', 'prob', 'label', 'user', 'document', 'created_at', 'updated_at')
         read_only_fields = ('user', )
 
 
@@ -188,7 +188,7 @@
 
     class Meta:
         model = SequenceAnnotation
-        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
+        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document', 'created_at', 'updated_at')
         read_only_fields = ('user',)
 
 
@@ -197,7 +197,7 @@
 
     class Meta:
         model = Seq2seqAnnotation
-        fields = ('id', 'text', 'user', 'document', 'prob')
+        fields = ('id', 'text', 'user', 'document', 'prob', 'created_at', 'updated_at')
         read_only_fields = ('user',)
 
 
@@ -206,7 +206,7 @@
 
     class Meta:
         model = Speech2textAnnotation
-        fields = ('id', 'prob', 'text', 'user', 'document')
+        fields = ('id', 'prob', 'text', 'user', 'document', 'created_at', 'updated_at')
         read_only_fields = ('user',)

{"golden_diff": "diff --git a/app/api/serializers.py b/app/api/serializers.py\n--- a/app/api/serializers.py\n+++ b/app/api/serializers.py\n@@ -177,7 +177,7 @@\n \n     class Meta:\n         model = DocumentAnnotation\n-        fields = ('id', 'prob', 'label', 'user', 'document')\n+        fields = ('id', 'prob', 'label', 'user', 'document', 'created_at', 'updated_at')\n         read_only_fields = ('user', )\n \n \n@@ -188,7 +188,7 @@\n \n     class Meta:\n         model = SequenceAnnotation\n-        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')\n+        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document', 'created_at', 'updated_at')\n         read_only_fields = ('user',)\n \n \n@@ -197,7 +197,7 @@\n \n     class Meta:\n         model = Seq2seqAnnotation\n-        fields = ('id', 'text', 'user', 'document', 'prob')\n+        fields = ('id', 'text', 'user', 'document', 'prob', 'created_at', 'updated_at')\n         read_only_fields = ('user',)\n \n \n@@ -206,7 +206,7 @@\n \n     class Meta:\n         model = Speech2textAnnotation\n-        fields = ('id', 'prob', 'text', 'user', 'document')\n+        fields = ('id', 'prob', 'text', 'user', 'document', 'created_at', 'updated_at')\n         read_only_fields = ('user',)\n", "issue": "Expose timestamps associated with annotations with the API\nThe `..Annotation` models currently stores the `created_at` and `updated_at` fields but they are not exposed by the API.\r\n\r\nI'd like to propose exposing them through the API so that downstream analysis can be conducted using simple API calls, like those made using [`doccano-client`](https://github.com/doccano/doccano-client), for instance.\n", "before_files": [{"content": "from django.conf import settings\nfrom django.contrib.auth import get_user_model\nfrom django.shortcuts import get_object_or_404\nfrom rest_framework import serializers\nfrom rest_polymorphic.serializers import PolymorphicSerializer\nfrom rest_framework.exceptions import ValidationError\n\n\nfrom .models import Label, Project, Document, RoleMapping, Role\nfrom .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject, Speech2textProject\nfrom .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation, Speech2textAnnotation\n\n\nclass UserSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = get_user_model()\n        fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')\n\n\nclass LabelSerializer(serializers.ModelSerializer):\n\n    def validate(self, attrs):\n        prefix_key = attrs.get('prefix_key')\n        suffix_key = attrs.get('suffix_key')\n\n        # In the case of user don't set any shortcut key.\n        if prefix_key is None and suffix_key is None:\n            return super().validate(attrs)\n\n        # Don't allow shortcut key not to have a suffix key.\n        if prefix_key and not suffix_key:\n            raise ValidationError('Shortcut key may not have a suffix key.')\n\n        # Don't allow to save same shortcut key when prefix_key is null.\n        try:\n            context = self.context['request'].parser_context\n            project_id = context['kwargs']['project_id']\n            label_id = context['kwargs'].get('label_id')\n        except (AttributeError, KeyError):\n            pass  # unit tests don't always have the correct context set up\n        else:\n            conflicting_labels = Label.objects.filter(\n                suffix_key=suffix_key,\n                prefix_key=prefix_key,\n                project=project_id,\n            )\n\n            if label_id is not None:\n                conflicting_labels = conflicting_labels.exclude(id=label_id)\n\n            if conflicting_labels.exists():\n                raise ValidationError('Duplicate shortcut key.')\n\n        return super().validate(attrs)\n\n    class Meta:\n        model = Label\n        fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')\n\n\nclass DocumentSerializer(serializers.ModelSerializer):\n    annotations = serializers.SerializerMethodField()\n    annotation_approver = serializers.SerializerMethodField()\n\n    def get_annotations(self, instance):\n        request = self.context.get('request')\n        project = instance.project\n        model = project.get_annotation_class()\n        serializer = project.get_annotation_serializer()\n        annotations = model.objects.filter(document=instance.id)\n        if request and not project.collaborative_annotation:\n            annotations = annotations.filter(user=request.user)\n        serializer = serializer(annotations, many=True)\n        return serializer.data\n\n    @classmethod\n    def get_annotation_approver(cls, instance):\n        approver = instance.annotations_approved_by\n        return approver.username if approver else None\n\n    class Meta:\n        model = Document\n        fields = ('id', 'text', 'annotations', 'meta', 'annotation_approver')\n\n\nclass ApproverSerializer(DocumentSerializer):\n\n    class Meta:\n        model = Document\n        fields = ('id', 'annotation_approver')\n\n\nclass ProjectSerializer(serializers.ModelSerializer):\n    current_users_role = serializers.SerializerMethodField()\n\n    def get_current_users_role(self, instance):\n        role_abstractor = {\n            \"is_project_admin\": settings.ROLE_PROJECT_ADMIN,\n            \"is_annotator\": settings.ROLE_ANNOTATOR,\n            \"is_annotation_approver\": settings.ROLE_ANNOTATION_APPROVER,\n        }\n        queryset = RoleMapping.objects.values(\"role_id__name\")\n        if queryset:\n            users_role = get_object_or_404(\n                queryset, project=instance.id, user=self.context.get(\"request\").user.id\n            )\n            for key, val in role_abstractor.items():\n                role_abstractor[key] = users_role[\"role_id__name\"] == val\n        return role_abstractor\n\n    class Meta:\n        model = Project\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image',\n                  'updated_at', 'randomize_document_order', 'collaborative_annotation', 'single_class_classification')\n        read_only_fields = ('image', 'updated_at', 'users', 'current_users_role')\n\n\nclass TextClassificationProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = TextClassificationProject\n        fields = ProjectSerializer.Meta.fields\n        read_only_fields = ProjectSerializer.Meta.read_only_fields\n\n\nclass SequenceLabelingProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = SequenceLabelingProject\n        fields = ProjectSerializer.Meta.fields\n        read_only_fields = ProjectSerializer.Meta.read_only_fields\n\n\nclass Seq2seqProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = Seq2seqProject\n        fields = ProjectSerializer.Meta.fields\n        read_only_fields = ProjectSerializer.Meta.read_only_fields\n\n\nclass Speech2textProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = Speech2textProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image',\n                  'updated_at', 'randomize_document_order')\n        read_only_fields = ('image', 'updated_at', 'users', 'current_users_role')\n\n\nclass ProjectPolymorphicSerializer(PolymorphicSerializer):\n    model_serializer_mapping = {\n        Project: ProjectSerializer,\n        TextClassificationProject: TextClassificationProjectSerializer,\n        SequenceLabelingProject: SequenceLabelingProjectSerializer,\n        Seq2seqProject: Seq2seqProjectSerializer,\n        Speech2textProject: Speech2textProjectSerializer,\n    }\n\n\nclass ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):\n\n    def get_queryset(self):\n        view = self.context.get('view', None)\n        request = self.context.get('request', None)\n        queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()\n        if not request or not queryset or not view:\n            return None\n        return queryset.filter(project=view.kwargs['project_id'])\n\n\nclass DocumentAnnotationSerializer(serializers.ModelSerializer):\n    # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = DocumentAnnotation\n        fields = ('id', 'prob', 'label', 'user', 'document')\n        read_only_fields = ('user', )\n\n\nclass SequenceAnnotationSerializer(serializers.ModelSerializer):\n    #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = SequenceAnnotation\n        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')\n        read_only_fields = ('user',)\n\n\nclass Seq2seqAnnotationSerializer(serializers.ModelSerializer):\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = Seq2seqAnnotation\n        fields = ('id', 'text', 'user', 'document', 'prob')\n        read_only_fields = ('user',)\n\n\nclass Speech2textAnnotationSerializer(serializers.ModelSerializer):\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = Speech2textAnnotation\n        fields = ('id', 'prob', 'text', 'user', 'document')\n        read_only_fields = ('user',)\n\n\nclass RoleSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = Role\n        fields = ('id', 'name')\n\n\nclass RoleMappingSerializer(serializers.ModelSerializer):\n    username = serializers.SerializerMethodField()\n    rolename = serializers.SerializerMethodField()\n\n    @classmethod\n    def get_username(cls, instance):\n        user = instance.user\n        return user.username if user else None\n\n    @classmethod\n    def get_rolename(cls, instance):\n        role = instance.role\n        return role.name if role else None\n\n    class Meta:\n        model = RoleMapping\n        fields = ('id', 'user', 'role', 'username', 'rolename')\n", "path": "app/api/serializers.py"}], "after_files": [{"content": "from django.conf import settings\nfrom django.contrib.auth import get_user_model\nfrom django.shortcuts import get_object_or_404\nfrom rest_framework import serializers\nfrom rest_polymorphic.serializers import PolymorphicSerializer\nfrom rest_framework.exceptions import ValidationError\n\n\nfrom .models import Label, Project, Document, RoleMapping, Role\nfrom .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject, Speech2textProject\nfrom .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation, Speech2textAnnotation\n\n\nclass UserSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = get_user_model()\n        fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')\n\n\nclass LabelSerializer(serializers.ModelSerializer):\n\n    def validate(self, attrs):\n        prefix_key = attrs.get('prefix_key')\n        suffix_key = attrs.get('suffix_key')\n\n        # In the case of user don't set any shortcut key.\n        if prefix_key is None and suffix_key is None:\n            return super().validate(attrs)\n\n        # Don't allow shortcut key not to have a suffix key.\n        if prefix_key and not suffix_key:\n            raise ValidationError('Shortcut key may not have a suffix key.')\n\n        # Don't allow to save same shortcut key when prefix_key is null.\n        try:\n            context = self.context['request'].parser_context\n            project_id = context['kwargs']['project_id']\n            label_id = context['kwargs'].get('label_id')\n        except (AttributeError, KeyError):\n            pass  # unit tests don't always have the correct context set up\n        else:\n            conflicting_labels = Label.objects.filter(\n                suffix_key=suffix_key,\n                prefix_key=prefix_key,\n                project=project_id,\n            )\n\n            if label_id is not None:\n                conflicting_labels = conflicting_labels.exclude(id=label_id)\n\n            if conflicting_labels.exists():\n                raise ValidationError('Duplicate shortcut key.')\n\n        return super().validate(attrs)\n\n    class Meta:\n        model = Label\n        fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')\n\n\nclass DocumentSerializer(serializers.ModelSerializer):\n    annotations = serializers.SerializerMethodField()\n    annotation_approver = serializers.SerializerMethodField()\n\n    def get_annotations(self, instance):\n        request = self.context.get('request')\n        project = instance.project\n        model = project.get_annotation_class()\n        serializer = project.get_annotation_serializer()\n        annotations = model.objects.filter(document=instance.id)\n        if request and not project.collaborative_annotation:\n            annotations = annotations.filter(user=request.user)\n        serializer = serializer(annotations, many=True)\n        return serializer.data\n\n    @classmethod\n    def get_annotation_approver(cls, instance):\n        approver = instance.annotations_approved_by\n        return approver.username if approver else None\n\n    class Meta:\n        model = Document\n        fields = ('id', 'text', 'annotations', 'meta', 'annotation_approver')\n\n\nclass ApproverSerializer(DocumentSerializer):\n\n    class Meta:\n        model = Document\n        fields = ('id', 'annotation_approver')\n\n\nclass ProjectSerializer(serializers.ModelSerializer):\n    current_users_role = serializers.SerializerMethodField()\n\n    def get_current_users_role(self, instance):\n        role_abstractor = {\n            \"is_project_admin\": settings.ROLE_PROJECT_ADMIN,\n            \"is_annotator\": settings.ROLE_ANNOTATOR,\n            \"is_annotation_approver\": settings.ROLE_ANNOTATION_APPROVER,\n        }\n        queryset = RoleMapping.objects.values(\"role_id__name\")\n        if queryset:\n            users_role = get_object_or_404(\n                queryset, project=instance.id, user=self.context.get(\"request\").user.id\n            )\n            for key, val in role_abstractor.items():\n                role_abstractor[key] = users_role[\"role_id__name\"] == val\n        return role_abstractor\n\n    class Meta:\n        model = Project\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image',\n                  'updated_at', 'randomize_document_order', 'collaborative_annotation', 'single_class_classification')\n        read_only_fields = ('image', 'updated_at', 'users', 'current_users_role')\n\n\nclass TextClassificationProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = TextClassificationProject\n        fields = ProjectSerializer.Meta.fields\n        read_only_fields = ProjectSerializer.Meta.read_only_fields\n\n\nclass SequenceLabelingProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = SequenceLabelingProject\n        fields = ProjectSerializer.Meta.fields\n        read_only_fields = ProjectSerializer.Meta.read_only_fields\n\n\nclass Seq2seqProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = Seq2seqProject\n        fields = ProjectSerializer.Meta.fields\n        read_only_fields = ProjectSerializer.Meta.read_only_fields\n\n\nclass Speech2textProjectSerializer(ProjectSerializer):\n\n    class Meta:\n        model = Speech2textProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', 'image',\n                  'updated_at', 'randomize_document_order')\n        read_only_fields = ('image', 'updated_at', 'users', 'current_users_role')\n\n\nclass ProjectPolymorphicSerializer(PolymorphicSerializer):\n    model_serializer_mapping = {\n        Project: ProjectSerializer,\n        TextClassificationProject: TextClassificationProjectSerializer,\n        SequenceLabelingProject: SequenceLabelingProjectSerializer,\n        Seq2seqProject: Seq2seqProjectSerializer,\n        Speech2textProject: Speech2textProjectSerializer,\n    }\n\n\nclass ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):\n\n    def get_queryset(self):\n        view = self.context.get('view', None)\n        request = self.context.get('request', None)\n        queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()\n        if not request or not queryset or not view:\n            return None\n        return queryset.filter(project=view.kwargs['project_id'])\n\n\nclass DocumentAnnotationSerializer(serializers.ModelSerializer):\n    # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = DocumentAnnotation\n        fields = ('id', 'prob', 'label', 'user', 'document', 'created_at', 'updated_at')\n        read_only_fields = ('user', )\n\n\nclass SequenceAnnotationSerializer(serializers.ModelSerializer):\n    #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = SequenceAnnotation\n        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document', 'created_at', 'updated_at')\n        read_only_fields = ('user',)\n\n\nclass Seq2seqAnnotationSerializer(serializers.ModelSerializer):\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = Seq2seqAnnotation\n        fields = ('id', 'text', 'user', 'document', 'prob', 'created_at', 'updated_at')\n        read_only_fields = ('user',)\n\n\nclass Speech2textAnnotationSerializer(serializers.ModelSerializer):\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = Speech2textAnnotation\n        fields = ('id', 'prob', 'text', 'user', 'document', 'created_at', 'updated_at')\n        read_only_fields = ('user',)\n\n\nclass RoleSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = Role\n        fields = ('id', 'name')\n\n\nclass RoleMappingSerializer(serializers.ModelSerializer):\n    username = serializers.SerializerMethodField()\n    rolename = serializers.SerializerMethodField()\n\n    @classmethod\n    def get_username(cls, instance):\n        user = instance.user\n        return user.username if user else None\n\n    @classmethod\n    def get_rolename(cls, instance):\n        role = instance.role\n        return role.name if role else None\n\n    class Meta:\n        model = RoleMapping\n        fields = ('id', 'user', 'role', 'username', 'rolename')\n", "path": "app/api/serializers.py"}]}