From bb3e011beaec6dddedc0a30698e03e74f8f95bb3 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 22 Oct 2020 11:59:31 +0200 Subject: [PATCH] agendas: do not import exception from settings when duplicating (#47916) --- chrono/agendas/models.py | 6 +++--- tests/test_agendas.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index 30f65de7..d5f445ae 100644 --- a/chrono/agendas/models.py +++ b/chrono/agendas/models.py @@ -1132,13 +1132,13 @@ class Desk(models.Model): ordering = ['label'] unique_together = ['agenda', 'slug'] - def save(self, *args, **kwargs): + def save(self, *args, import_exceptions=True, **kwargs): assert self.agenda.kind != 'virtual', "a desk can't reference a virtual agenda" first_created = not self.pk if not self.slug: self.slug = generate_slug(self, agenda=self.agenda) super(Desk, self).save(*args, **kwargs) - if first_created: + if first_created and import_exceptions: self.import_timeperiod_exceptions_from_settings(enable=True) @property @@ -1179,7 +1179,7 @@ class Desk(models.Model): if agenda_target: new_desk.agenda = agenda_target # store new desk - new_desk.save() + new_desk.save(import_exceptions=False) # clone related objects for time_period in self.timeperiod_set.all(): diff --git a/tests/test_agendas.py b/tests/test_agendas.py index 15e525f1..4a781df3 100644 --- a/tests/test_agendas.py +++ b/tests/test_agendas.py @@ -991,6 +991,29 @@ def test_desk_duplicate_exception_sources(): assert not new_desk.timeperiodexception_set.exists() +@override_settings( + EXCEPTIONS_SOURCES={'holidays': {'class': 'workalendar.europe.France', 'label': 'Holidays'},} +) +def test_desk_duplicate_exception_source_from_settings(): + agenda = Agenda.objects.create(label='Agenda') + desk = Desk.objects.create(label='Desk', agenda=agenda) + + source = desk.timeperiodexceptionsource_set.get(settings_slug='holidays') + assert source.enabled + exceptions_count = desk.timeperiodexception_set.count() + + new_desk = desk.duplicate(label="New Desk") + assert new_desk.timeperiodexceptionsource_set.filter(settings_slug='holidays').count() == 1 + assert new_desk.timeperiodexceptionsource_set.get(settings_slug='holidays').enabled + assert new_desk.timeperiodexception_set.count() == exceptions_count + + source.disable() + + new_desk = desk.duplicate(label="New Desk") + assert not new_desk.timeperiodexceptionsource_set.get(settings_slug='holidays').enabled + assert not new_desk.timeperiodexception_set.exists() + + def test_agenda_meetings_duplicate(): group = Group(name=u'Group') group.save()