Projet

Général

Profil

0003-lingo-import-export-agenda-config-65361.patch

Lauréline Guérin, 23 mai 2022 10:57

Télécharger (5,98 ko)

Voir les différences:

Subject: [PATCH 3/3] lingo: import/export agenda config (#65361)

 lingo/pricing/urls.py         |  5 ++++
 lingo/pricing/views.py        | 33 +++++++++++++++++++++---
 tests/pricing/test_manager.py | 48 +++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 4 deletions(-)
lingo/pricing/urls.py
138 138
        staff_member_required(views.agenda_detail),
139 139
        name='lingo-manager-agenda-detail',
140 140
    ),
141
    url(
142
        r'^agenda/(?P<pk>\d+)/export/$',
143
        staff_member_required(views.agenda_export),
144
        name='lingo-manager-agenda-export',
145
    ),
141 146
    url(
142 147
        r'^agenda/(?P<pk>\d+)/pricing/add/$',
143 148
        staff_member_required(views.agenda_pricing_add),
lingo/pricing/views.py
142 142
                global_noop = False
143 143
                count = len(obj_results['created'])
144 144
                if not count:
145
                    message1 = import_messages[obj_name]['create_noop']
145
                    message1 = import_messages[obj_name].get('create_noop')
146 146
                else:
147 147
                    message1 = import_messages[obj_name]['create'](count) % {'count': count}
148 148

  
......
154 154

  
155 155
                obj_results['messages'] = "%s %s" % (message1, message2)
156 156

  
157
        pc_count, pm_count = (
157
        a_count, pc_count, pm_count = (
158
            len(results['agendas']['all']),
158 159
            len(results['pricing_categories']['all']),
159 160
            len(results['pricing_models']['all']),
160 161
        )
161
        if (pc_count, pm_count) == (1, 0):
162
        if (a_count, pc_count, pm_count) == (1, 0, 0):
163
            # only one agenda imported, redirect to agenda page
164
            return HttpResponseRedirect(
165
                reverse(
166
                    'lingo-manager-agenda-detail',
167
                    kwargs={'pk': results['agendas']['all'][0].pk},
168
                )
169
            )
170
        if (a_count, pc_count, pm_count) == (0, 1, 0):
162 171
            # only one criteria category imported, redirect to criteria page
163 172
            return HttpResponseRedirect(reverse('lingo-manager-pricing-criteria-list'))
164
        if (pc_count, pm_count) == (0, 1):
173
        if (a_count, pc_count, pm_count) == (0, 0, 1):
165 174
            # only one pricing imported, redirect to pricing page
166 175
            return HttpResponseRedirect(
167 176
                reverse(
......
619 628
agenda_detail = AgendaDetailView.as_view()
620 629

  
621 630

  
631
class AgendaExport(AgendaMixin, DetailView):
632
    model = Agenda
633

  
634
    def get(self, request, *args, **kwargs):
635
        response = HttpResponse(content_type='application/json')
636
        today = datetime.date.today()
637
        response['Content-Disposition'] = 'attachment; filename="export_pricing_agenda_{}_{}.json"'.format(
638
            self.get_object().slug, today.strftime('%Y%m%d')
639
        )
640
        json.dump({'agendas': [self.get_object().export_json()]}, response, indent=2)
641
        return response
642

  
643

  
644
agenda_export = AgendaExport.as_view()
645

  
646

  
622 647
class AgendaPricingAddView(AgendaMixin, CreateView):
623 648
    template_name = 'lingo/pricing/manager_agenda_pricing_form.html'
624 649
    model = AgendaPricing
tests/pricing/test_manager.py
697 697
    assert mock_refresh.call_args_list == [mock.call()]
698 698

  
699 699

  
700
@pytest.mark.freeze_time('2021-07-08')
701
def test_import_agenda(app, admin_user):
702
    agenda = Agenda.objects.create(label='Foo Bar')
703

  
704
    app = login(app)
705
    resp = app.get('/manage/pricing/agenda/%s/export/' % agenda.pk)
706
    assert resp.headers['content-type'] == 'application/json'
707
    assert (
708
        resp.headers['content-disposition']
709
        == 'attachment; filename="export_pricing_agenda_foo-bar_20210708.json"'
710
    )
711
    agenda_export = resp.text
712

  
713
    # existing agenda
714
    resp = app.get('/manage/pricing/', status=200)
715
    resp = resp.click('Import')
716
    resp.form['config_json'] = Upload('export.json', agenda_export.encode('utf-8'), 'application/json')
717
    resp = resp.form.submit()
718
    assert resp.location.endswith('/manage/pricing/agenda/%s/' % agenda.pk)
719
    resp = resp.follow()
720
    assert 'An agenda has been updated.' not in resp.text
721
    assert Agenda.objects.count() == 1
722

  
723
    # unknown agenda
724
    Agenda.objects.all().delete()
725
    resp = app.get('/manage/pricing/', status=200)
726
    resp = resp.click('Import')
727
    resp.form['config_json'] = Upload('export.json', agenda_export.encode('utf-8'), 'application/json')
728
    resp = resp.form.submit()
729
    assert resp.context['form'].errors['config_json'] == ['Missing "foo-bar" agenda']
730

  
731
    # multiple pricing
732
    Agenda.objects.create(label='Foo Bar')
733
    Agenda.objects.create(label='Foo Bar 2')
734
    agendas = json.loads(agenda_export)
735
    agendas['agendas'].append(copy.copy(agendas['agendas'][0]))
736
    agendas['agendas'][1]['slug'] = 'foo-bar-2'
737

  
738
    resp = app.get('/manage/pricing/', status=200)
739
    resp = resp.click('Import')
740
    resp.form['config_json'] = Upload('export.json', json.dumps(agendas).encode('utf-8'), 'application/json')
741
    resp = resp.form.submit()
742
    assert resp.location.endswith('/manage/pricing/')
743
    resp = resp.follow()
744
    assert '2 agendas have been updated.' in resp.text
745
    assert Agenda.objects.count() == 2
746

  
747

  
700 748
def test_add_agenda_pricing(app, admin_user):
701 749
    agenda = Agenda.objects.create(label='Foo Bar')
702 750
    pricing = Pricing.objects.create(label='Model')
703
-