From 14e1ff56da3f276d28b14588dfb551a999e42ee8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 25 Aug 2019 23:13:49 +0200
Subject: [PATCH] manager: fix pluralization of imported agendas messages
 (#35571)

---
 chrono/manager/views.py |  6 ++++--
 tests/test_manager.py   | 31 ++++++++++++++++++++++++++++---
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index 80a59e4..a4d0c32 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -109,13 +109,15 @@ class AgendasImportView(FormView):
                 message1 = _('No agenda created.')
             else:
                 message1 = ungettext('An agenda has been created.',
-                                '%(count)d agendas have been created.', results['created'])
+                        '%(count)d agendas have been created.', results['created']) % {
+                                'count': results['created']}
 
             if results.get('updated') == 0:
                 message2 = _('No agenda updated.')
             else:
                 message2 = ungettext('An agenda has been updated.',
-                                '%(count)d agendas have been updated.', results['updated'])
+                        '%(count)d agendas have been updated.', results['updated']) % {
+                                'count': results['updated']}
             messages.info(self.request, u'%s %s' % (message1, message2))
 
         return super(AgendasImportView, self).form_valid(form)
diff --git a/tests/test_manager.py b/tests/test_manager.py
index be6efb7..8f015ff 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from __future__ import unicode_literals
+import copy
 import json
 
 from django.contrib.auth.models import User, Group
@@ -1546,7 +1547,7 @@ def test_import_agenda(app, admin_user):
     resp = app.get('/manage/agendas/%s/settings' % agenda.id)
     resp = resp.click('Export')
     assert resp.headers['content-type'] == 'application/json'
-    agenda_export = resp.body
+    agenda_export = resp.text
 
     # invalid json
     resp = app.get('/manage/', status=200)
@@ -1565,7 +1566,7 @@ def test_import_agenda(app, admin_user):
     # existing agenda
     resp = app.get('/manage/', status=200)
     resp = resp.click('Import')
-    resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json')
+    resp.form['agendas_json'] = Upload('export.json', agenda_export.encode('utf-8'), 'application/json')
     resp = resp.form.submit().follow()
     assert 'No agenda created. An agenda has been updated.' in resp.text
     assert Agenda.objects.count() == 1
@@ -1574,11 +1575,35 @@ def test_import_agenda(app, admin_user):
     Agenda.objects.all().delete()
     resp = app.get('/manage/', status=200)
     resp = resp.click('Import')
-    resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json')
+    resp.form['agendas_json'] = Upload('export.json', agenda_export.encode('utf-8'), 'application/json')
     resp = resp.form.submit().follow()
     assert 'An agenda has been created. No agenda updated.' in resp.text
     assert Agenda.objects.count() == 1
 
+    # multiple agendas
+    agendas = json.loads(agenda_export)
+    agendas['agendas'].append(copy.copy(agendas['agendas'][0]))
+    agendas['agendas'].append(copy.copy(agendas['agendas'][0]))
+    agendas['agendas'][1]['label'] = 'Foo bar 2'
+    agendas['agendas'][1]['slug'] = 'foo-bar-2'
+    agendas['agendas'][2]['label'] = 'Foo bar 3'
+    agendas['agendas'][2]['slug'] = 'foo-bar-3'
+
+    resp = app.get('/manage/', status=200)
+    resp = resp.click('Import')
+    resp.form['agendas_json'] = Upload('export.json', json.dumps(agendas).encode('utf-8'), 'application/json')
+    resp = resp.form.submit().follow()
+    assert '2 agendas have been created. An agenda has been updated.' in resp.text
+    assert Agenda.objects.count() == 3
+
+    Agenda.objects.all().delete()
+    resp = app.get('/manage/', status=200)
+    resp = resp.click('Import')
+    resp.form['agendas_json'] = Upload('export.json', json.dumps(agendas).encode('utf-8'), 'application/json')
+    resp = resp.form.submit().follow()
+    assert '3 agendas have been created. No agenda updated.' in resp.text
+    assert Agenda.objects.count() == 3
+
     # reference to unknown group
     agenda_export_dict = json.loads(force_text(agenda_export))
     agenda_export_dict['agendas'][0]['permissions']['view'] = u'gé1'
-- 
2.23.0

