Skip to content
Snippets Groups Projects
Commit 6979b037 authored by Benjamin Hättasch's avatar Benjamin Hättasch
Browse files

Make sure post_save signal is sent after update of availabilities

Manually trigger post_save signal after bulk create of replacing availabilities
This fixes #132
parent 26cc3f0f
No related branches found
No related tags found
No related merge requests found
Pipeline #81169 passed
...@@ -7,6 +7,7 @@ import json ...@@ -7,6 +7,7 @@ import json
from django import forms from django import forms
from django.db import transaction from django.db import transaction
from django.db.models.signals import post_save
from django.utils.dateparse import parse_datetime from django.utils.dateparse import parse_datetime
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
...@@ -142,11 +143,16 @@ class AvailabilitiesFormMixin(forms.Form): ...@@ -142,11 +143,16 @@ class AvailabilitiesFormMixin(forms.Form):
for avail in availabilities: for avail in availabilities:
setattr(avail, reference_name, instance.id) setattr(avail, reference_name, instance.id)
def _replace_availabilities(self, instance, availabilities): def _replace_availabilities(self, instance, availabilities: [Availability]):
with transaction.atomic(): with transaction.atomic():
# TODO: do not recreate objects unnecessarily, give the client the IDs, so we can track modifications and leave unchanged objects alone # TODO: do not recreate objects unnecessarily, give the client the IDs, so we can track modifications and leave unchanged objects alone
instance.availabilities.all().delete() instance.availabilities.all().delete()
Availability.objects.bulk_create(availabilities) Availability.objects.bulk_create(availabilities)
# Trigger post save signal manually to make sure constraints are updated accordingly
# Doing this one time is sufficient, since this will nevertheless update all availability constraint
# violations of the corresponding AK
if len(availabilities) > 0:
post_save.send(Availability, instance=availabilities[0], created=True)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs) instance = super().save(*args, **kwargs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment