From 80b2b7065454845a2a70cf3d8d0ec6624858d0d5 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 25 May 2023 16:45:01 +0200 Subject: [PATCH] manager: allow exporting agendas by category (#77790) --- chrono/manager/forms.py | 8 +++++++- chrono/manager/utils.py | 13 ++++++------- tests/manager/test_import_export.py | 21 ++++++++++++++++++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py index 9211d2ed..dc078413 100644 --- a/chrono/manager/forms.py +++ b/chrono/manager/forms.py @@ -37,6 +37,7 @@ from django.utils.encoding import force_str from django.utils.formats import date_format from django.utils.html import format_html, mark_safe from django.utils.translation import gettext_lazy as _ +from django.utils.translation import pgettext from chrono.agendas.models import ( WEEK_CHOICES, @@ -46,6 +47,7 @@ from chrono.agendas.models import ( AgendaNotificationsSettings, AgendaReminderSettings, Booking, + Category, Desk, Event, EventsType, @@ -1505,7 +1507,7 @@ class AgendaReminderTestForm(forms.Form): class AgendasExportForm(forms.Form): - agendas = forms.BooleanField(label=_('Agendas'), required=False, initial=True) + agendas = forms.ChoiceField(label=_('Agendas'), required=True) resources = forms.BooleanField(label=_('Resources'), required=False, initial=True) unavailability_calendars = forms.BooleanField( label=_('Unavailability calendars'), required=False, initial=True @@ -1520,6 +1522,10 @@ class AgendasExportForm(forms.Form): self.fields['shared_custody'].initial = False self.fields['shared_custody'].widget = forms.HiddenInput() + self.fields['agendas'].choices = [('all', pgettext('agendas', 'All')), ('none', _('None'))] + [ + (x.id, x.label) for x in Category.objects.all() + ] + class SharedCustodyRuleForm(forms.ModelForm): guardian = forms.ModelChoiceField(label=_('Guardian'), queryset=Person.objects.none()) diff --git a/chrono/manager/utils.py b/chrono/manager/utils.py index 6ec1ba03..993c383c 100644 --- a/chrono/manager/utils.py +++ b/chrono/manager/utils.py @@ -15,12 +15,10 @@ # along with this program. If not, see . import collections -import itertools from django.contrib.auth.models import Group from django.core.exceptions import FieldDoesNotExist from django.db import transaction -from django.db.models import Q from chrono.agendas.models import ( Agenda, @@ -34,7 +32,7 @@ from chrono.agendas.models import ( def export_site( - agendas=True, + agendas='all', unavailability_calendars=True, events_types=True, resources=True, @@ -51,10 +49,11 @@ def export_site( data['events_types'] = [x.export_json() for x in EventsType.objects.all()] if unavailability_calendars: data['unavailability_calendars'] = [x.export_json() for x in UnavailabilityCalendar.objects.all()] - if agendas: - qs1 = Agenda.objects.filter(~Q(kind='virtual')) - qs2 = Agenda.objects.filter(kind='virtual') - data['agendas'] = [x.export_json() for x in itertools.chain(qs1, qs2)] + if agendas != 'none': + qs = Agenda.objects.all() + if agendas != 'all': + qs = qs.filter(category=agendas) + data['agendas'] = [x.export_json() for x in sorted(qs, key=lambda x: x == 'virtual')] if shared_custody: data['shared_custody_settings'] = SharedCustodySettings.get_singleton().export_json() return data diff --git a/tests/manager/test_import_export.py b/tests/manager/test_import_export.py index eb513666..52a39ed5 100644 --- a/tests/manager/test_import_export.py +++ b/tests/manager/test_import_export.py @@ -9,6 +9,7 @@ from webtest import Upload from chrono.agendas.models import ( Agenda, Booking, + Category, Desk, Event, MeetingType, @@ -54,7 +55,7 @@ def test_export_site(app, admin_user): assert len(site_json['categories']) == 0 resp = app.get('/manage/agendas/export/') - resp.form['agendas'] = False + resp.form['agendas'] = 'none' resp.form['events_types'] = False resp.form['resources'] = False resp.form['categories'] = False @@ -302,3 +303,21 @@ def test_export_site_shared_custody_settings(app, admin_user): site_json = json.loads(resp.text) assert 'management_role' in site_json['shared_custody_settings'] + + +def test_export_site_agendas_of_category(app, admin_user): + category_a = Category.objects.create(label='Category A') + category_b = Category.objects.create(label='Category B') + Agenda.objects.create(label='Foo A', kind='meetings', category=category_a) + Agenda.objects.create(label='Bar A', kind='meetings', category=category_a) + Agenda.objects.create(label='Foo B', kind='meetings', category=category_b) + Agenda.objects.create(label='Bar', kind='meetings') + + login(app) + resp = app.get('/manage/agendas/export/') + resp.form['agendas'].select(text='Category A') + resp = resp.form.submit() + + site_json = json.loads(resp.text) + assert len(site_json['agendas']) == 2 + assert [x['label'] for x in site_json['agendas']] == ['Bar A', 'Foo A']