manager: publication_date in csv event import (#44177)

This commit is contained in:
Lauréline Guérin 2020-06-18 10:39:04 +02:00
parent 82d1a53b24
commit 18ce6713b4
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 38 additions and 8 deletions

View File

@ -348,6 +348,16 @@ class ImportEventsForm(forms.Form):
setattr(event, more_attr, csvline[column_index - 1])
column_index += 1
if len(csvline) >= 10 and csvline[9]: # publication date is optional
for date_fmt in ('%Y-%m-%d', '%d/%m/%Y'):
try:
event.publication_date = datetime.datetime.strptime(csvline[9], date_fmt).date()
break
except ValueError:
continue
else:
raise ValidationError(_('Invalid file format. (date format, line %d)') % (i + 1))
try:
event.full_clean(exclude=exclude)
except ValidationError as e:

View File

@ -1,2 +1,2 @@
{% load i18n %}{% trans 'date' %},{% trans 'time' %},{% trans 'number of places' %},{% trans 'number of places in waiting list' %},{% trans 'label' %},{% trans 'identifier' %},{% trans 'description' %},{% trans 'pricing' %},{% trans 'URL' %}
{{ some_future_date|date:"Y-m-d" }},{{ some_future_date|date:"H:i" }},15,0,{% trans "example event" as label %}{{ label }},{{ label|slugify }},,,https://www.example.net
{% load i18n %}{% trans 'date' %},{% trans 'time' %},{% trans 'number of places' %},{% trans 'number of places in waiting list' %},{% trans 'label' %},{% trans 'identifier' %},{% trans 'description' %},{% trans 'pricing' %},{% trans 'URL' %},{% trans 'publication date' %}
{{ some_future_date|date:"Y-m-d" }},{{ some_future_date|date:"H:i" }},15,0,{% trans "example event" as label %}{{ label }},{{ label|slugify }},,,https://www.example.net,{{ some_future_date|date:"Y-m-d" }}

View File

@ -1206,7 +1206,7 @@ def test_import_events(app, admin_user):
resp = resp.click('Import Events')
sample_csv_resp = resp.click('Download sample file')
assert sample_csv_resp.content_type == 'text/csv'
assert sample_csv_resp.text.startswith('date,time,')
assert sample_csv_resp.text.startswith('date,time')
resp.form['events_csv_file'] = Upload('t.csv', sample_csv_resp.content, 'text/csv')
resp = resp.form.submit(status=302)
@ -1344,19 +1344,39 @@ def test_import_events(app, admin_user):
resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,10,5,label,slug', 'text/csv')
resp = resp.form.submit(status=200)
assert 'Event with this Agenda and Identifier already exists.' in resp.text
assert not '__all__' in resp.text
assert '__all__' not in resp.text
# additional optional attributes
Event.objects.all().delete()
resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,10,5,label,slug,,,,', 'text/csv')
resp = resp.form.submit(status=302)
assert Event.objects.count() == 1
event = Event.objects.get()
assert event.description == ''
assert event.pricing == ''
assert event.url == ''
assert event.publication_date is None
Event.objects.all().delete()
resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
resp.form['events_csv_file'] = Upload(
't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url', 'text/csv'
't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url,2016-10-16', 'text/csv'
)
resp = resp.form.submit(status=302)
assert Event.objects.count() == 1
assert Event.objects.all()[0].description == 'description'
assert Event.objects.all()[0].pricing == 'pricing'
assert Event.objects.all()[0].url == 'url'
event = Event.objects.get()
assert event.description == 'description'
assert event.pricing == 'pricing'
assert event.url == 'url'
assert event.publication_date == datetime.date(2016, 10, 16)
# publication date bad format
resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
resp.form['events_csv_file'] = Upload(
't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url,foobar', 'text/csv'
)
resp = resp.form.submit(status=200)
assert 'Invalid file format. (date format' in resp.text
# import events with empty slugs
Event.objects.all().delete()