From de9ecb6f0357e805d826f3691e0b4b0793a5ba65 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 21 Oct 2020 13:54:34 +0200 Subject: [PATCH 1/2] agendas: fix duplication of desk with external exceptions (#47916) --- chrono/agendas/models.py | 10 +++++++--- tests/test_agendas.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index b8c2baf..e129327 100644 --- a/chrono/agendas/models.py +++ b/chrono/agendas/models.py @@ -1137,8 +1137,9 @@ class Desk(models.Model): first_created = not self.pk if not self.slug: self.slug = generate_slug(self, agenda=self.agenda) + import_exceptions = kwargs.pop('import_exceptions', True) 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,12 +1180,12 @@ 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(): time_period.duplicate(desk_target=new_desk) - for time_period_exception in self.timeperiodexception_set.all(): + for time_period_exception in self.timeperiodexception_set.exclude(external=True): time_period_exception.duplicate(desk_target=new_desk) for time_period_exception_source in self.timeperiodexceptionsource_set.all(): time_period_exception_source.duplicate(desk_target=new_desk) @@ -1451,6 +1452,9 @@ class TimePeriodExceptionSource(models.Model): new_source.ics_file.save(self.ics_filename, ics_file, save=False) # store new source new_source.save() + # create related exceptions + if self.settings_slug and self.enabled: + new_source.enable() return new_source diff --git a/tests/test_agendas.py b/tests/test_agendas.py index 1dfde6d..f35db3f 100644 --- a/tests/test_agendas.py +++ b/tests/test_agendas.py @@ -970,6 +970,30 @@ def test_desk_duplicate(): assert new_desk.slug == 'new-desk-1' +@override_settings( + EXCEPTIONS_SOURCES={'holidays': {'class': 'workalendar.europe.France', 'label': 'Holidays'},} +) +def test_desk_duplicate_external_exception_sources(): + 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