agendas: import/export end time event field (#88615)
gitea/chrono/pipeline/head This commit looks good Details

This commit is contained in:
Valentin Deniaud 2024-03-25 12:25:34 +01:00
parent f7e224ba9b
commit 620fce0914
3 changed files with 33 additions and 2 deletions

View File

@ -2698,6 +2698,12 @@ class Event(WithInspectMixin, models.Model):
except ValueError:
raise AgendaImportError(_('Bad datetime format "%s"') % data['start_datetime'])
if data.get('end_time'):
try:
data['end_time'] = datetime.datetime.strptime(data['end_time'], '%H:%M').time()
except ValueError:
raise AgendaImportError(_('Bad time format "%s"') % data['end_time'])
if data.get('recurrence_days'):
# keep stable weekday numbering after switch to ISO in db
data['recurrence_days'] = [i + 1 for i in data['recurrence_days']]
@ -2717,6 +2723,7 @@ class Event(WithInspectMixin, models.Model):
update_fields = {
field: getattr(event, field)
for field in [
'end_time',
'label',
'duration',
'publication_datetime',
@ -2736,6 +2743,7 @@ class Event(WithInspectMixin, models.Model):
)
return {
'start_datetime': make_naive(self.start_datetime).strftime('%Y-%m-%d %H:%M:%S'),
'end_time': self.end_time.strftime('%H:%M') if self.end_time else None,
'publication_datetime': make_naive(self.publication_datetime).strftime('%Y-%m-%d %H:%M:%S')
if self.publication_datetime
else None,

View File

@ -57,7 +57,7 @@ def test_agenda_history(settings, app, admin_user):
assert resp.text.count('<del>') == 0
else:
assert resp.text.count('diff_sub') == 1
assert resp.text.count('diff_add') == 16
assert resp.text.count('diff_add') == 17
assert resp.text.count('diff_chg') == 0
resp = app.get(
'/manage/agendas/%s/history/compare/?version1=%s&version2=%s'
@ -66,7 +66,7 @@ def test_agenda_history(settings, app, admin_user):
assert 'Snapshot (%s)' % (snapshot1.pk) in resp
assert 'Snapshot (%s) - (Version 42.0)' % (snapshot2.pk) in resp
assert resp.text.count('diff_sub') == 1
assert resp.text.count('diff_add') == 16
assert resp.text.count('diff_add') == 17
assert resp.text.count('diff_chg') == 0
# check compare on application version number

View File

@ -181,6 +181,25 @@ def test_import_export_bad_date_format(app):
assert '%s' % excinfo.value == 'Bad datetime format "17-05-22 08:00:00"'
def test_import_export_bad_end_time_format(app):
agenda_events = Agenda.objects.create(label='Events Agenda', kind='events')
Desk.objects.create(agenda=agenda_events, slug='_exceptions_holder')
Event.objects.create(
agenda=agenda_events,
start_datetime=make_aware(datetime.datetime(2020, 7, 21, 16, 42, 35)),
places=10,
end_time=datetime.time(20, 00),
)
output = get_output_of_command('export_site')
payload = json.loads(output)
assert len(payload['agendas']) == 1
payload['agendas'][0]['events'][0]['end_time'] = 'xxx20:00'
with pytest.raises(AgendaImportError) as excinfo:
import_site(payload)
assert '%s' % excinfo.value == 'Bad time format "xxx20:00"'
def test_import_export_events_agenda_options(app):
agenda = Agenda.objects.create(
label='Foo Bar',
@ -256,6 +275,7 @@ def test_import_export_event_details(app):
publication_datetime=make_aware(datetime.datetime(2020, 5, 11)),
places=42,
start_datetime=now(),
end_time=datetime.time(20, 00),
duration=30,
)
# check event (agenda, slug) unicity
@ -287,6 +307,7 @@ def test_import_export_event_details(app):
assert str(first_imported_event.publication_datetime) == '2020-05-10 22:00:00+00:00'
assert str(first_imported_event.publication_datetime.tzinfo) == 'UTC'
assert first_imported_event.duration == 30
assert first_imported_event.end_time == datetime.time(20, 00)
assert Agenda.objects.get(label='Foo Bar 2').event_set.first().slug == 'event'
@ -297,6 +318,7 @@ def test_import_export_recurring_event(app, freezer):
event = Event.objects.create(
agenda=agenda,
start_datetime=now(),
end_time=datetime.time(20, 00),
recurrence_days=[now().isoweekday()],
recurrence_week_interval=2,
places=10,
@ -353,6 +375,7 @@ def test_import_export_recurring_event(app, freezer):
event = Event.objects.get(slug='test')
assert event.places == 42
assert event.end_time == datetime.time(20, 00)
assert Event.objects.filter(primary_event=event, places=42).count() == 1