From 6b87c5b212a283b4594e3935ca4a3b6cd481a4fe Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 22 Oct 2020 11:59:31 +0200 Subject: [PATCH 2/3] agendas: do not import exception from settings when duplicating (#47916) --- chrono/agendas/models.py | 6 +++--- tests/test_agendas.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index f9c46d6..c8f5c42 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 15e525f..e554cd5 100644 --- a/tests/test_agendas.py +++ b/tests/test_agendas.py @@ -991,6 +991,30 @@ 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.enabled = False + source.save() + + 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() -- 2.20.1