Projet

Général

Profil

0001-manager-publication_date-in-csv-event-import-44177.patch

Lauréline Guérin, 18 juin 2020 10:39

Télécharger (5,27 ko)

Voir les différences:

Subject: [PATCH] manager: publication_date in csv event import (#44177)

 chrono/manager/forms.py                       | 10 ++++++
 .../chrono/manager_sample_events.txt          |  4 +--
 tests/test_manager.py                         | 32 +++++++++++++++----
 3 files changed, 38 insertions(+), 8 deletions(-)
chrono/manager/forms.py
348 348
                    setattr(event, more_attr, csvline[column_index - 1])
349 349
                column_index += 1
350 350

  
351
            if len(csvline) >= 10 and csvline[9]:  # publication date is optional
352
                for date_fmt in ('%Y-%m-%d', '%d/%m/%Y'):
353
                    try:
354
                        event.publication_date = datetime.datetime.strptime(csvline[9], date_fmt).date()
355
                        break
356
                    except ValueError:
357
                        continue
358
                else:
359
                    raise ValidationError(_('Invalid file format. (date format, line %d)') % (i + 1))
360

  
351 361
            try:
352 362
                event.full_clean(exclude=exclude)
353 363
            except ValidationError as e:
chrono/manager/templates/chrono/manager_sample_events.txt
1
{% 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' %}
2
{{ 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
1
{% 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' %}
2
{{ 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" }}
tests/test_manager.py
1206 1206
    resp = resp.click('Import Events')
1207 1207
    sample_csv_resp = resp.click('Download sample file')
1208 1208
    assert sample_csv_resp.content_type == 'text/csv'
1209
    assert sample_csv_resp.text.startswith('date,time,')
1209
    assert sample_csv_resp.text.startswith('date,time')
1210 1210

  
1211 1211
    resp.form['events_csv_file'] = Upload('t.csv', sample_csv_resp.content, 'text/csv')
1212 1212
    resp = resp.form.submit(status=302)
......
1344 1344
    resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,10,5,label,slug', 'text/csv')
1345 1345
    resp = resp.form.submit(status=200)
1346 1346
    assert 'Event with this Agenda and Identifier already exists.' in resp.text
1347
    assert not '__all__' in resp.text
1347
    assert '__all__' not in resp.text
1348 1348

  
1349 1349
    # additional optional attributes
1350 1350
    Event.objects.all().delete()
1351 1351
    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
1352
    resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,10,5,label,slug,,,,', 'text/csv')
1353
    resp = resp.form.submit(status=302)
1354
    assert Event.objects.count() == 1
1355
    event = Event.objects.get()
1356
    assert event.description == ''
1357
    assert event.pricing == ''
1358
    assert event.url == ''
1359
    assert event.publication_date is None
1360
    Event.objects.all().delete()
1361
    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
1352 1362
    resp.form['events_csv_file'] = Upload(
1353
        't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url', 'text/csv'
1363
        't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url,2016-10-16', 'text/csv'
1354 1364
    )
1355 1365
    resp = resp.form.submit(status=302)
1356 1366
    assert Event.objects.count() == 1
1357
    assert Event.objects.all()[0].description == 'description'
1358
    assert Event.objects.all()[0].pricing == 'pricing'
1359
    assert Event.objects.all()[0].url == 'url'
1367
    event = Event.objects.get()
1368
    assert event.description == 'description'
1369
    assert event.pricing == 'pricing'
1370
    assert event.url == 'url'
1371
    assert event.publication_date == datetime.date(2016, 10, 16)
1372

  
1373
    # publication date bad format
1374
    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
1375
    resp.form['events_csv_file'] = Upload(
1376
        't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url,foobar', 'text/csv'
1377
    )
1378
    resp = resp.form.submit(status=200)
1379
    assert 'Invalid file format. (date format' in resp.text
1360 1380

  
1361 1381
    # import events with empty slugs
1362 1382
    Event.objects.all().delete()
1363
-