We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add end_time of an event when getting all events with get request
I want to be able to get the end time of an event when getting all events. I know I can get the end time when getting a specific event, but it is a bit cumbersome.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `lego/apps/events/serializers/events.py`
Content:
```
1 from django.db import transaction
2 from rest_framework import serializers
3 from rest_framework.fields import BooleanField, CharField
4 
5 from lego.apps.comments.serializers import CommentSerializer
6 from lego.apps.companies.fields import CompanyField
7 from lego.apps.companies.models import Company
8 from lego.apps.content.fields import ContentSerializerField
9 from lego.apps.events.constants import PRESENT
10 from lego.apps.events.fields import ActivationTimeField, SpotsLeftField
11 from lego.apps.events.models import Event, Pool
12 from lego.apps.events.serializers.pools import (
13     PoolAdministrateSerializer,
14     PoolCreateAndUpdateSerializer,
15     PoolReadAuthSerializer,
16     PoolReadSerializer,
17 )
18 from lego.apps.events.serializers.registrations import (
19     RegistrationReadDetailedSerializer,
20     RegistrationReadSerializer,
21 )
22 from lego.apps.files.fields import ImageField
23 from lego.apps.tags.serializers import TagSerializerMixin
24 from lego.apps.users.constants import GROUP_GRADE
25 from lego.apps.users.fields import AbakusGroupField
26 from lego.apps.users.models import AbakusGroup
27 from lego.apps.users.serializers.users import PublicUserSerializer
28 from lego.utils.serializers import BasisModelSerializer
29 
30 
31 class EventPublicSerializer(BasisModelSerializer):
32 
33     thumbnail = ImageField(
34         source="cover",
35         required=False,
36         options={"height": 500, "width": 500, "smart": True},
37     )
38 
39     class Meta:
40         model = Event
41         fields = ("id", "title", "description", "event_type", "location", "thumbnail")
42         read_only = True
43 
44 
45 class EventReadSerializer(TagSerializerMixin, BasisModelSerializer):
46     company = CompanyField(queryset=Company.objects.all())
47     cover = ImageField(required=False, options={"height": 500})
48     thumbnail = ImageField(
49         source="cover",
50         required=False,
51         options={"height": 500, "width": 500, "smart": True},
52     )
53     activation_time = ActivationTimeField()
54 
55     class Meta:
56         model = Event
57         fields = (
58             "id",
59             "title",
60             "description",
61             "cover",
62             "event_type",
63             "location",
64             "start_time",
65             "thumbnail",
66             "total_capacity",
67             "company",
68             "registration_count",
69             "tags",
70             "activation_time",
71         )
72         read_only = True
73 
74 
75 class EventReadDetailedSerializer(TagSerializerMixin, BasisModelSerializer):
76     comments = CommentSerializer(read_only=True, many=True)
77     comment_target = CharField(read_only=True)
78     cover = ImageField(required=False, options={"height": 500})
79     company = CompanyField(queryset=Company.objects.all())
80     responsible_group = AbakusGroupField(
81         queryset=AbakusGroup.objects.all(), required=False, allow_null=True
82     )
83     pools = PoolReadSerializer(many=True)
84     active_capacity = serializers.ReadOnlyField()
85     text = ContentSerializerField()
86     created_by = PublicUserSerializer()
87 
88     registration_close_time = serializers.DateTimeField(read_only=True)
89 
90     class Meta:
91         model = Event
92         fields = (
93             "id",
94             "title",
95             "description",
96             "cover",
97             "text",
98             "event_type",
99             "location",
100             "comments",
101             "comment_target",
102             "start_time",
103             "end_time",
104             "merge_time",
105             "pools",
106             "registration_close_time",
107             "registration_deadline_hours",
108             "unregistration_deadline",
109             "company",
110             "responsible_group",
111             "active_capacity",
112             "feedback_description",
113             "feedback_required",
114             "is_priced",
115             "price_member",
116             "price_guest",
117             "use_stripe",
118             "payment_due_date",
119             "use_captcha",
120             "waiting_registration_count",
121             "tags",
122             "is_merged",
123             "heed_penalties",
124             "created_by",
125             "is_abakom_only",
126             "registration_count",
127             "survey",
128             "use_consent",
129         )
130         read_only = True
131 
132 
133 class EventForSurveySerializer(EventReadSerializer):
134     attended_count = serializers.SerializerMethodField()
135 
136     class Meta:
137         model = Event
138         fields = EventReadSerializer.Meta.fields + (
139             "registration_count",
140             "waiting_registration_count",
141             "attended_count",
142         )
143         read_only = True
144 
145     def get_attended_count(self, event):
146         return event.registrations.filter(presence=PRESENT).count()
147 
148 
149 class EventUserRegSerializer(EventReadSerializer):
150     user_reg = serializers.SerializerMethodField()
151 
152     class Meta:
153         model = Event
154         fields = EventReadSerializer.Meta.fields + ("user_reg",)
155         read_only = True
156 
157     def get_user_reg(self, event):
158         return RegistrationReadSerializer(event.user_reg[0]).data
159 
160 
161 class EventReadUserDetailedSerializer(EventReadDetailedSerializer):
162     """ User specfic event serializer that appends data based on request.user """
163 
164     activation_time = ActivationTimeField()
165     spots_left = SpotsLeftField()
166     price = serializers.SerializerMethodField()
167 
168     class Meta(EventReadDetailedSerializer.Meta):
169         fields = EventReadDetailedSerializer.Meta.fields + (
170             "price",
171             "activation_time",
172             "spots_left",
173         )
174 
175     def get_price(self, obj):
176         request = self.context.get("request", None)
177         if request:
178             return obj.get_price(user=request.user)
179 
180 
181 class EventReadAuthUserDetailedSerializer(EventReadUserDetailedSerializer):
182     pools = PoolReadAuthSerializer(many=True)
183     waiting_registrations = RegistrationReadSerializer(many=True)
184     unanswered_surveys = serializers.SerializerMethodField()
185 
186     class Meta(EventReadUserDetailedSerializer.Meta):
187         fields = EventReadUserDetailedSerializer.Meta.fields + (
188             "waiting_registrations",
189             "unanswered_surveys",
190         )
191 
192     def get_unanswered_surveys(self, obj):
193         request = self.context.get("request", None)
194         return request.user.unanswered_surveys()
195 
196 
197 class EventAdministrateSerializer(EventReadSerializer):
198     pools = PoolAdministrateSerializer(many=True)
199     unregistered = RegistrationReadDetailedSerializer(many=True)
200     waiting_registrations = RegistrationReadDetailedSerializer(many=True)
201 
202     class Meta(EventReadSerializer.Meta):
203         fields = EventReadSerializer.Meta.fields + (
204             "pools",
205             "unregistered",
206             "waiting_registrations",
207             "use_consent",
208         )
209 
210 
211 class EventCreateAndUpdateSerializer(TagSerializerMixin, BasisModelSerializer):
212     cover = ImageField(required=False, options={"height": 500})
213     responsible_group = AbakusGroupField(
214         queryset=AbakusGroup.objects.all(), required=False, allow_null=True
215     )
216     pools = PoolCreateAndUpdateSerializer(many=True, required=False)
217     text = ContentSerializerField()
218     is_abakom_only = BooleanField(required=False, default=False)
219 
220     registration_close_time = serializers.DateTimeField(read_only=True)
221 
222     class Meta:
223         model = Event
224         fields = (
225             "id",
226             "title",
227             "cover",
228             "description",
229             "text",
230             "company",
231             "responsible_group",
232             "feedback_description",
233             "feedback_required",
234             "event_type",
235             "location",
236             "is_priced",
237             "price_member",
238             "price_guest",
239             "use_stripe",
240             "payment_due_date",
241             "start_time",
242             "end_time",
243             "merge_time",
244             "use_captcha",
245             "tags",
246             "pools",
247             "unregistration_deadline",
248             "pinned",
249             "use_consent",
250             "heed_penalties",
251             "is_abakom_only",
252             "registration_deadline_hours",
253             "registration_close_time",
254         )
255 
256     def validate(self, data):
257         """
258         Check that start is before finish.
259         """
260         if hasattr(data, "start_time") and hasattr(data, "end_time"):
261             if data["start_time"] > data["end_time"]:
262                 raise serializers.ValidationError(
263                     {
264                         "end_time": "User does not have the required permissions for time travel"
265                     }
266                 )
267         return data
268 
269     def create(self, validated_data):
270         pools = validated_data.pop("pools", [])
271         is_abakom_only = validated_data.pop("is_abakom_only", False)
272         with transaction.atomic():
273             event = super().create(validated_data)
274             for pool in pools:
275                 permission_groups = pool.pop("permission_groups")
276                 created_pool = Pool.objects.create(event=event, **pool)
277                 created_pool.permission_groups.set(permission_groups)
278             event.set_abakom_only(is_abakom_only)
279             return event
280 
281     def update(self, instance, validated_data):
282         pools = validated_data.pop("pools", None)
283         is_abakom_only = validated_data.pop("is_abakom_only", False)
284         with transaction.atomic():
285             if pools is not None:
286                 existing_pools = list(instance.pools.all().values_list("id", flat=True))
287                 for pool in pools:
288                     pool_id = pool.get("id", None)
289                     if pool_id in existing_pools:
290                         existing_pools.remove(pool_id)
291                     permission_groups = pool.pop("permission_groups")
292                     created_pool = Pool.objects.update_or_create(
293                         event=instance,
294                         id=pool_id,
295                         defaults={
296                             "name": pool.get("name"),
297                             "capacity": pool.get("capacity", 0),
298                             "activation_date": pool.get("activation_date"),
299                         },
300                     )[0]
301                     created_pool.permission_groups.set(permission_groups)
302                 for pool_id in existing_pools:
303                     Pool.objects.get(id=pool_id).delete()
304             instance.set_abakom_only(is_abakom_only)
305             return super().update(instance, validated_data)
306 
307 
308 class EventSearchSerializer(serializers.ModelSerializer):
309     cover = ImageField(required=False, options={"height": 500})
310     thumbnail = ImageField(
311         source="cover",
312         required=False,
313         options={"height": 500, "width": 500, "smart": True},
314     )
315     text = ContentSerializerField()
316     activation_time = ActivationTimeField()
317 
318     class Meta:
319         model = Event
320         fields = (
321             "id",
322             "title",
323             "description",
324             "cover",
325             "text",
326             "event_type",
327             "location",
328             "start_time",
329             "thumbnail",
330             "end_time",
331             "total_capacity",
332             "company",
333             "registration_count",
334             "tags",
335             "activation_time",
336             "pinned",
337         )
338         read_only = True
339 
340 
341 def populate_event_registration_users_with_grade(event_dict):
342     """
343     Populates every user in registrations in a serialized event with `grade`.
344     Mainly used in the administrate endpoint
345     :param event_dict:
346     :return:
347     """
348     grades = AbakusGroup.objects.filter(type=GROUP_GRADE).values("id", "name")
349     grade_dict = {item["id"]: item for item in grades}
350     for pool in event_dict.get("pools", []):
351         for registration in pool.get("registrations", []):
352             user = registration.get("user", {})
353             abakus_groups = user.get("abakus_groups", [])
354             user["grade"] = None
355             for id in abakus_groups:
356                 grade = grade_dict.get(id, None)
357                 if grade:
358                     user["grade"] = grade
359     return event_dict
360
```
--- 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/lego/apps/events/serializers/events.py b/lego/apps/events/serializers/events.py
--- a/lego/apps/events/serializers/events.py
+++ b/lego/apps/events/serializers/events.py
@@ -62,6 +62,7 @@
             "event_type",
             "location",
             "start_time",
+            "end_time",
             "thumbnail",
             "total_capacity",
             "company",

{"golden_diff": "diff --git a/lego/apps/events/serializers/events.py b/lego/apps/events/serializers/events.py\n--- a/lego/apps/events/serializers/events.py\n+++ b/lego/apps/events/serializers/events.py\n@@ -62,6 +62,7 @@\n             \"event_type\",\n             \"location\",\n             \"start_time\",\n+            \"end_time\",\n             \"thumbnail\",\n             \"total_capacity\",\n             \"company\",\n", "issue": "Add end_time of an event when getting all events with get request\nI want to be able to get the end time of an event when getting all events. I know I can get the end time when getting a specific event, but it is a bit cumbersome.\n", "before_files": [{"content": "from django.db import transaction\nfrom rest_framework import serializers\nfrom rest_framework.fields import BooleanField, CharField\n\nfrom lego.apps.comments.serializers import CommentSerializer\nfrom lego.apps.companies.fields import CompanyField\nfrom lego.apps.companies.models import Company\nfrom lego.apps.content.fields import ContentSerializerField\nfrom lego.apps.events.constants import PRESENT\nfrom lego.apps.events.fields import ActivationTimeField, SpotsLeftField\nfrom lego.apps.events.models import Event, Pool\nfrom lego.apps.events.serializers.pools import (\n    PoolAdministrateSerializer,\n    PoolCreateAndUpdateSerializer,\n    PoolReadAuthSerializer,\n    PoolReadSerializer,\n)\nfrom lego.apps.events.serializers.registrations import (\n    RegistrationReadDetailedSerializer,\n    RegistrationReadSerializer,\n)\nfrom lego.apps.files.fields import ImageField\nfrom lego.apps.tags.serializers import TagSerializerMixin\nfrom lego.apps.users.constants import GROUP_GRADE\nfrom lego.apps.users.fields import AbakusGroupField\nfrom lego.apps.users.models import AbakusGroup\nfrom lego.apps.users.serializers.users import PublicUserSerializer\nfrom lego.utils.serializers import BasisModelSerializer\n\n\nclass EventPublicSerializer(BasisModelSerializer):\n\n    thumbnail = ImageField(\n        source=\"cover\",\n        required=False,\n        options={\"height\": 500, \"width\": 500, \"smart\": True},\n    )\n\n    class Meta:\n        model = Event\n        fields = (\"id\", \"title\", \"description\", \"event_type\", \"location\", \"thumbnail\")\n        read_only = True\n\n\nclass EventReadSerializer(TagSerializerMixin, BasisModelSerializer):\n    company = CompanyField(queryset=Company.objects.all())\n    cover = ImageField(required=False, options={\"height\": 500})\n    thumbnail = ImageField(\n        source=\"cover\",\n        required=False,\n        options={\"height\": 500, \"width\": 500, \"smart\": True},\n    )\n    activation_time = ActivationTimeField()\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"description\",\n            \"cover\",\n            \"event_type\",\n            \"location\",\n            \"start_time\",\n            \"thumbnail\",\n            \"total_capacity\",\n            \"company\",\n            \"registration_count\",\n            \"tags\",\n            \"activation_time\",\n        )\n        read_only = True\n\n\nclass EventReadDetailedSerializer(TagSerializerMixin, BasisModelSerializer):\n    comments = CommentSerializer(read_only=True, many=True)\n    comment_target = CharField(read_only=True)\n    cover = ImageField(required=False, options={\"height\": 500})\n    company = CompanyField(queryset=Company.objects.all())\n    responsible_group = AbakusGroupField(\n        queryset=AbakusGroup.objects.all(), required=False, allow_null=True\n    )\n    pools = PoolReadSerializer(many=True)\n    active_capacity = serializers.ReadOnlyField()\n    text = ContentSerializerField()\n    created_by = PublicUserSerializer()\n\n    registration_close_time = serializers.DateTimeField(read_only=True)\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"description\",\n            \"cover\",\n            \"text\",\n            \"event_type\",\n            \"location\",\n            \"comments\",\n            \"comment_target\",\n            \"start_time\",\n            \"end_time\",\n            \"merge_time\",\n            \"pools\",\n            \"registration_close_time\",\n            \"registration_deadline_hours\",\n            \"unregistration_deadline\",\n            \"company\",\n            \"responsible_group\",\n            \"active_capacity\",\n            \"feedback_description\",\n            \"feedback_required\",\n            \"is_priced\",\n            \"price_member\",\n            \"price_guest\",\n            \"use_stripe\",\n            \"payment_due_date\",\n            \"use_captcha\",\n            \"waiting_registration_count\",\n            \"tags\",\n            \"is_merged\",\n            \"heed_penalties\",\n            \"created_by\",\n            \"is_abakom_only\",\n            \"registration_count\",\n            \"survey\",\n            \"use_consent\",\n        )\n        read_only = True\n\n\nclass EventForSurveySerializer(EventReadSerializer):\n    attended_count = serializers.SerializerMethodField()\n\n    class Meta:\n        model = Event\n        fields = EventReadSerializer.Meta.fields + (\n            \"registration_count\",\n            \"waiting_registration_count\",\n            \"attended_count\",\n        )\n        read_only = True\n\n    def get_attended_count(self, event):\n        return event.registrations.filter(presence=PRESENT).count()\n\n\nclass EventUserRegSerializer(EventReadSerializer):\n    user_reg = serializers.SerializerMethodField()\n\n    class Meta:\n        model = Event\n        fields = EventReadSerializer.Meta.fields + (\"user_reg\",)\n        read_only = True\n\n    def get_user_reg(self, event):\n        return RegistrationReadSerializer(event.user_reg[0]).data\n\n\nclass EventReadUserDetailedSerializer(EventReadDetailedSerializer):\n    \"\"\" User specfic event serializer that appends data based on request.user \"\"\"\n\n    activation_time = ActivationTimeField()\n    spots_left = SpotsLeftField()\n    price = serializers.SerializerMethodField()\n\n    class Meta(EventReadDetailedSerializer.Meta):\n        fields = EventReadDetailedSerializer.Meta.fields + (\n            \"price\",\n            \"activation_time\",\n            \"spots_left\",\n        )\n\n    def get_price(self, obj):\n        request = self.context.get(\"request\", None)\n        if request:\n            return obj.get_price(user=request.user)\n\n\nclass EventReadAuthUserDetailedSerializer(EventReadUserDetailedSerializer):\n    pools = PoolReadAuthSerializer(many=True)\n    waiting_registrations = RegistrationReadSerializer(many=True)\n    unanswered_surveys = serializers.SerializerMethodField()\n\n    class Meta(EventReadUserDetailedSerializer.Meta):\n        fields = EventReadUserDetailedSerializer.Meta.fields + (\n            \"waiting_registrations\",\n            \"unanswered_surveys\",\n        )\n\n    def get_unanswered_surveys(self, obj):\n        request = self.context.get(\"request\", None)\n        return request.user.unanswered_surveys()\n\n\nclass EventAdministrateSerializer(EventReadSerializer):\n    pools = PoolAdministrateSerializer(many=True)\n    unregistered = RegistrationReadDetailedSerializer(many=True)\n    waiting_registrations = RegistrationReadDetailedSerializer(many=True)\n\n    class Meta(EventReadSerializer.Meta):\n        fields = EventReadSerializer.Meta.fields + (\n            \"pools\",\n            \"unregistered\",\n            \"waiting_registrations\",\n            \"use_consent\",\n        )\n\n\nclass EventCreateAndUpdateSerializer(TagSerializerMixin, BasisModelSerializer):\n    cover = ImageField(required=False, options={\"height\": 500})\n    responsible_group = AbakusGroupField(\n        queryset=AbakusGroup.objects.all(), required=False, allow_null=True\n    )\n    pools = PoolCreateAndUpdateSerializer(many=True, required=False)\n    text = ContentSerializerField()\n    is_abakom_only = BooleanField(required=False, default=False)\n\n    registration_close_time = serializers.DateTimeField(read_only=True)\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"cover\",\n            \"description\",\n            \"text\",\n            \"company\",\n            \"responsible_group\",\n            \"feedback_description\",\n            \"feedback_required\",\n            \"event_type\",\n            \"location\",\n            \"is_priced\",\n            \"price_member\",\n            \"price_guest\",\n            \"use_stripe\",\n            \"payment_due_date\",\n            \"start_time\",\n            \"end_time\",\n            \"merge_time\",\n            \"use_captcha\",\n            \"tags\",\n            \"pools\",\n            \"unregistration_deadline\",\n            \"pinned\",\n            \"use_consent\",\n            \"heed_penalties\",\n            \"is_abakom_only\",\n            \"registration_deadline_hours\",\n            \"registration_close_time\",\n        )\n\n    def validate(self, data):\n        \"\"\"\n        Check that start is before finish.\n        \"\"\"\n        if hasattr(data, \"start_time\") and hasattr(data, \"end_time\"):\n            if data[\"start_time\"] > data[\"end_time\"]:\n                raise serializers.ValidationError(\n                    {\n                        \"end_time\": \"User does not have the required permissions for time travel\"\n                    }\n                )\n        return data\n\n    def create(self, validated_data):\n        pools = validated_data.pop(\"pools\", [])\n        is_abakom_only = validated_data.pop(\"is_abakom_only\", False)\n        with transaction.atomic():\n            event = super().create(validated_data)\n            for pool in pools:\n                permission_groups = pool.pop(\"permission_groups\")\n                created_pool = Pool.objects.create(event=event, **pool)\n                created_pool.permission_groups.set(permission_groups)\n            event.set_abakom_only(is_abakom_only)\n            return event\n\n    def update(self, instance, validated_data):\n        pools = validated_data.pop(\"pools\", None)\n        is_abakom_only = validated_data.pop(\"is_abakom_only\", False)\n        with transaction.atomic():\n            if pools is not None:\n                existing_pools = list(instance.pools.all().values_list(\"id\", flat=True))\n                for pool in pools:\n                    pool_id = pool.get(\"id\", None)\n                    if pool_id in existing_pools:\n                        existing_pools.remove(pool_id)\n                    permission_groups = pool.pop(\"permission_groups\")\n                    created_pool = Pool.objects.update_or_create(\n                        event=instance,\n                        id=pool_id,\n                        defaults={\n                            \"name\": pool.get(\"name\"),\n                            \"capacity\": pool.get(\"capacity\", 0),\n                            \"activation_date\": pool.get(\"activation_date\"),\n                        },\n                    )[0]\n                    created_pool.permission_groups.set(permission_groups)\n                for pool_id in existing_pools:\n                    Pool.objects.get(id=pool_id).delete()\n            instance.set_abakom_only(is_abakom_only)\n            return super().update(instance, validated_data)\n\n\nclass EventSearchSerializer(serializers.ModelSerializer):\n    cover = ImageField(required=False, options={\"height\": 500})\n    thumbnail = ImageField(\n        source=\"cover\",\n        required=False,\n        options={\"height\": 500, \"width\": 500, \"smart\": True},\n    )\n    text = ContentSerializerField()\n    activation_time = ActivationTimeField()\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"description\",\n            \"cover\",\n            \"text\",\n            \"event_type\",\n            \"location\",\n            \"start_time\",\n            \"thumbnail\",\n            \"end_time\",\n            \"total_capacity\",\n            \"company\",\n            \"registration_count\",\n            \"tags\",\n            \"activation_time\",\n            \"pinned\",\n        )\n        read_only = True\n\n\ndef populate_event_registration_users_with_grade(event_dict):\n    \"\"\"\n    Populates every user in registrations in a serialized event with `grade`.\n    Mainly used in the administrate endpoint\n    :param event_dict:\n    :return:\n    \"\"\"\n    grades = AbakusGroup.objects.filter(type=GROUP_GRADE).values(\"id\", \"name\")\n    grade_dict = {item[\"id\"]: item for item in grades}\n    for pool in event_dict.get(\"pools\", []):\n        for registration in pool.get(\"registrations\", []):\n            user = registration.get(\"user\", {})\n            abakus_groups = user.get(\"abakus_groups\", [])\n            user[\"grade\"] = None\n            for id in abakus_groups:\n                grade = grade_dict.get(id, None)\n                if grade:\n                    user[\"grade\"] = grade\n    return event_dict\n", "path": "lego/apps/events/serializers/events.py"}], "after_files": [{"content": "from django.db import transaction\nfrom rest_framework import serializers\nfrom rest_framework.fields import BooleanField, CharField\n\nfrom lego.apps.comments.serializers import CommentSerializer\nfrom lego.apps.companies.fields import CompanyField\nfrom lego.apps.companies.models import Company\nfrom lego.apps.content.fields import ContentSerializerField\nfrom lego.apps.events.constants import PRESENT\nfrom lego.apps.events.fields import ActivationTimeField, SpotsLeftField\nfrom lego.apps.events.models import Event, Pool\nfrom lego.apps.events.serializers.pools import (\n    PoolAdministrateSerializer,\n    PoolCreateAndUpdateSerializer,\n    PoolReadAuthSerializer,\n    PoolReadSerializer,\n)\nfrom lego.apps.events.serializers.registrations import (\n    RegistrationReadDetailedSerializer,\n    RegistrationReadSerializer,\n)\nfrom lego.apps.files.fields import ImageField\nfrom lego.apps.tags.serializers import TagSerializerMixin\nfrom lego.apps.users.constants import GROUP_GRADE\nfrom lego.apps.users.fields import AbakusGroupField\nfrom lego.apps.users.models import AbakusGroup\nfrom lego.apps.users.serializers.users import PublicUserSerializer\nfrom lego.utils.serializers import BasisModelSerializer\n\n\nclass EventPublicSerializer(BasisModelSerializer):\n\n    thumbnail = ImageField(\n        source=\"cover\",\n        required=False,\n        options={\"height\": 500, \"width\": 500, \"smart\": True},\n    )\n\n    class Meta:\n        model = Event\n        fields = (\"id\", \"title\", \"description\", \"event_type\", \"location\", \"thumbnail\")\n        read_only = True\n\n\nclass EventReadSerializer(TagSerializerMixin, BasisModelSerializer):\n    company = CompanyField(queryset=Company.objects.all())\n    cover = ImageField(required=False, options={\"height\": 500})\n    thumbnail = ImageField(\n        source=\"cover\",\n        required=False,\n        options={\"height\": 500, \"width\": 500, \"smart\": True},\n    )\n    activation_time = ActivationTimeField()\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"description\",\n            \"cover\",\n            \"event_type\",\n            \"location\",\n            \"start_time\",\n            \"end_time\",\n            \"thumbnail\",\n            \"total_capacity\",\n            \"company\",\n            \"registration_count\",\n            \"tags\",\n            \"activation_time\",\n        )\n        read_only = True\n\n\nclass EventReadDetailedSerializer(TagSerializerMixin, BasisModelSerializer):\n    comments = CommentSerializer(read_only=True, many=True)\n    comment_target = CharField(read_only=True)\n    cover = ImageField(required=False, options={\"height\": 500})\n    company = CompanyField(queryset=Company.objects.all())\n    responsible_group = AbakusGroupField(\n        queryset=AbakusGroup.objects.all(), required=False, allow_null=True\n    )\n    pools = PoolReadSerializer(many=True)\n    active_capacity = serializers.ReadOnlyField()\n    text = ContentSerializerField()\n    created_by = PublicUserSerializer()\n\n    registration_close_time = serializers.DateTimeField(read_only=True)\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"description\",\n            \"cover\",\n            \"text\",\n            \"event_type\",\n            \"location\",\n            \"comments\",\n            \"comment_target\",\n            \"start_time\",\n            \"end_time\",\n            \"merge_time\",\n            \"pools\",\n            \"registration_close_time\",\n            \"registration_deadline_hours\",\n            \"unregistration_deadline\",\n            \"company\",\n            \"responsible_group\",\n            \"active_capacity\",\n            \"feedback_description\",\n            \"feedback_required\",\n            \"is_priced\",\n            \"price_member\",\n            \"price_guest\",\n            \"use_stripe\",\n            \"payment_due_date\",\n            \"use_captcha\",\n            \"waiting_registration_count\",\n            \"tags\",\n            \"is_merged\",\n            \"heed_penalties\",\n            \"created_by\",\n            \"is_abakom_only\",\n            \"registration_count\",\n            \"survey\",\n            \"use_consent\",\n        )\n        read_only = True\n\n\nclass EventForSurveySerializer(EventReadSerializer):\n    attended_count = serializers.SerializerMethodField()\n\n    class Meta:\n        model = Event\n        fields = EventReadSerializer.Meta.fields + (\n            \"registration_count\",\n            \"waiting_registration_count\",\n            \"attended_count\",\n        )\n        read_only = True\n\n    def get_attended_count(self, event):\n        return event.registrations.filter(presence=PRESENT).count()\n\n\nclass EventUserRegSerializer(EventReadSerializer):\n    user_reg = serializers.SerializerMethodField()\n\n    class Meta:\n        model = Event\n        fields = EventReadSerializer.Meta.fields + (\"user_reg\",)\n        read_only = True\n\n    def get_user_reg(self, event):\n        return RegistrationReadSerializer(event.user_reg[0]).data\n\n\nclass EventReadUserDetailedSerializer(EventReadDetailedSerializer):\n    \"\"\" User specfic event serializer that appends data based on request.user \"\"\"\n\n    activation_time = ActivationTimeField()\n    spots_left = SpotsLeftField()\n    price = serializers.SerializerMethodField()\n\n    class Meta(EventReadDetailedSerializer.Meta):\n        fields = EventReadDetailedSerializer.Meta.fields + (\n            \"price\",\n            \"activation_time\",\n            \"spots_left\",\n        )\n\n    def get_price(self, obj):\n        request = self.context.get(\"request\", None)\n        if request:\n            return obj.get_price(user=request.user)\n\n\nclass EventReadAuthUserDetailedSerializer(EventReadUserDetailedSerializer):\n    pools = PoolReadAuthSerializer(many=True)\n    waiting_registrations = RegistrationReadSerializer(many=True)\n    unanswered_surveys = serializers.SerializerMethodField()\n\n    class Meta(EventReadUserDetailedSerializer.Meta):\n        fields = EventReadUserDetailedSerializer.Meta.fields + (\n            \"waiting_registrations\",\n            \"unanswered_surveys\",\n        )\n\n    def get_unanswered_surveys(self, obj):\n        request = self.context.get(\"request\", None)\n        return request.user.unanswered_surveys()\n\n\nclass EventAdministrateSerializer(EventReadSerializer):\n    pools = PoolAdministrateSerializer(many=True)\n    unregistered = RegistrationReadDetailedSerializer(many=True)\n    waiting_registrations = RegistrationReadDetailedSerializer(many=True)\n\n    class Meta(EventReadSerializer.Meta):\n        fields = EventReadSerializer.Meta.fields + (\n            \"pools\",\n            \"unregistered\",\n            \"waiting_registrations\",\n            \"use_consent\",\n        )\n\n\nclass EventCreateAndUpdateSerializer(TagSerializerMixin, BasisModelSerializer):\n    cover = ImageField(required=False, options={\"height\": 500})\n    responsible_group = AbakusGroupField(\n        queryset=AbakusGroup.objects.all(), required=False, allow_null=True\n    )\n    pools = PoolCreateAndUpdateSerializer(many=True, required=False)\n    text = ContentSerializerField()\n    is_abakom_only = BooleanField(required=False, default=False)\n\n    registration_close_time = serializers.DateTimeField(read_only=True)\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"cover\",\n            \"description\",\n            \"text\",\n            \"company\",\n            \"responsible_group\",\n            \"feedback_description\",\n            \"feedback_required\",\n            \"event_type\",\n            \"location\",\n            \"is_priced\",\n            \"price_member\",\n            \"price_guest\",\n            \"use_stripe\",\n            \"payment_due_date\",\n            \"start_time\",\n            \"end_time\",\n            \"merge_time\",\n            \"use_captcha\",\n            \"tags\",\n            \"pools\",\n            \"unregistration_deadline\",\n            \"pinned\",\n            \"use_consent\",\n            \"heed_penalties\",\n            \"is_abakom_only\",\n            \"registration_deadline_hours\",\n            \"registration_close_time\",\n        )\n\n    def validate(self, data):\n        \"\"\"\n        Check that start is before finish.\n        \"\"\"\n        if hasattr(data, \"start_time\") and hasattr(data, \"end_time\"):\n            if data[\"start_time\"] > data[\"end_time\"]:\n                raise serializers.ValidationError(\n                    {\n                        \"end_time\": \"User does not have the required permissions for time travel\"\n                    }\n                )\n        return data\n\n    def create(self, validated_data):\n        pools = validated_data.pop(\"pools\", [])\n        is_abakom_only = validated_data.pop(\"is_abakom_only\", False)\n        with transaction.atomic():\n            event = super().create(validated_data)\n            for pool in pools:\n                permission_groups = pool.pop(\"permission_groups\")\n                created_pool = Pool.objects.create(event=event, **pool)\n                created_pool.permission_groups.set(permission_groups)\n            event.set_abakom_only(is_abakom_only)\n            return event\n\n    def update(self, instance, validated_data):\n        pools = validated_data.pop(\"pools\", None)\n        is_abakom_only = validated_data.pop(\"is_abakom_only\", False)\n        with transaction.atomic():\n            if pools is not None:\n                existing_pools = list(instance.pools.all().values_list(\"id\", flat=True))\n                for pool in pools:\n                    pool_id = pool.get(\"id\", None)\n                    if pool_id in existing_pools:\n                        existing_pools.remove(pool_id)\n                    permission_groups = pool.pop(\"permission_groups\")\n                    created_pool = Pool.objects.update_or_create(\n                        event=instance,\n                        id=pool_id,\n                        defaults={\n                            \"name\": pool.get(\"name\"),\n                            \"capacity\": pool.get(\"capacity\", 0),\n                            \"activation_date\": pool.get(\"activation_date\"),\n                        },\n                    )[0]\n                    created_pool.permission_groups.set(permission_groups)\n                for pool_id in existing_pools:\n                    Pool.objects.get(id=pool_id).delete()\n            instance.set_abakom_only(is_abakom_only)\n            return super().update(instance, validated_data)\n\n\nclass EventSearchSerializer(serializers.ModelSerializer):\n    cover = ImageField(required=False, options={\"height\": 500})\n    thumbnail = ImageField(\n        source=\"cover\",\n        required=False,\n        options={\"height\": 500, \"width\": 500, \"smart\": True},\n    )\n    text = ContentSerializerField()\n    activation_time = ActivationTimeField()\n\n    class Meta:\n        model = Event\n        fields = (\n            \"id\",\n            \"title\",\n            \"description\",\n            \"cover\",\n            \"text\",\n            \"event_type\",\n            \"location\",\n            \"start_time\",\n            \"thumbnail\",\n            \"end_time\",\n            \"total_capacity\",\n            \"company\",\n            \"registration_count\",\n            \"tags\",\n            \"activation_time\",\n            \"pinned\",\n        )\n        read_only = True\n\n\ndef populate_event_registration_users_with_grade(event_dict):\n    \"\"\"\n    Populates every user in registrations in a serialized event with `grade`.\n    Mainly used in the administrate endpoint\n    :param event_dict:\n    :return:\n    \"\"\"\n    grades = AbakusGroup.objects.filter(type=GROUP_GRADE).values(\"id\", \"name\")\n    grade_dict = {item[\"id\"]: item for item in grades}\n    for pool in event_dict.get(\"pools\", []):\n        for registration in pool.get(\"registrations\", []):\n            user = registration.get(\"user\", {})\n            abakus_groups = user.get(\"abakus_groups\", [])\n            user[\"grade\"] = None\n            for id in abakus_groups:\n                grade = grade_dict.get(id, None)\n                if grade:\n                    user[\"grade\"] = grade\n    return event_dict\n", "path": "lego/apps/events/serializers/events.py"}]}