manager: extend CSV import support to new attributes (#39128)

This commit is contained in:
Frédéric Péters 2020-01-20 20:03:51 +01:00
parent 63853d67e8
commit 35bdf92dec
3 changed files with 24 additions and 5 deletions

View File

@ -167,8 +167,9 @@ class ImportEventsForm(forms.Form):
required=True,
help_text=_(
'CSV file with date, time, number of places, '
'number of places in waiting list, and label '
'as columns.'
'number of places in waiting list, label, and '
'optionally, identifier, description, pricing '
'and URL as columns.'
),
)
events = None
@ -236,9 +237,15 @@ class ImportEventsForm(forms.Form):
event.label = force_text(csvline[4])
exclude = ['desk', 'meeting_type']
if len(csvline) >= 6:
event.slug = ' '.join([force_text(x) for x in csvline[5:]])
event.slug = force_text(csvline[5])
else:
exclude += ['slug']
column_index = 7
for more_attr in ('description', 'pricing', 'url'):
if len(csvline) >= column_index:
setattr(event, more_attr, csvline[column_index - 1])
column_index += 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' %}
{{ some_future_date|date:"Y-m-d" }},{{ some_future_date|date:"H:i" }},15,0,{% trans "example event" as label %}{{ label }},{{ label|slugify }}
{% 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

View File

@ -716,6 +716,18 @@ def test_import_events(app, admin_user):
resp = resp.form.submit(status=200)
assert 'Invalid file format. (__all__: Event with this Agenda and Identifier already exists.' 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,description,pricing,url', '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'
def test_add_meetings_agenda(app, admin_user):
app = login(app)