Projet

Général

Profil

0001-backoffice-ignore-only-one-limit-for-agent-submissio.patch

Frédéric Péters, 03 août 2020 21:22

Télécharger (4,52 ko)

Voir les différences:

Subject: [PATCH 1/2] backoffice: ignore "only one" limit for agent submissions
 (#45711)

 tests/test_backoffice_pages.py | 27 +++++++++++++++++++++++++++
 wcs/backoffice/submission.py   |  3 +++
 wcs/forms/root.py              | 31 +++++++++++++++++++++----------
 3 files changed, 51 insertions(+), 10 deletions(-)
tests/test_backoffice_pages.py
3380 3380
    assert formdata.status == 'wf-new'
3381 3381

  
3382 3382

  
3383
def test_backoffice_submission_only_one_check(pub, local_user):
3384
    user = create_user(pub)
3385
    create_environment(pub)
3386

  
3387
    formdef = FormDef.get_by_urlname('form-title')
3388
    formdef.fields = [formdef.fields[0]]
3389
    formdef.backoffice_submission_roles = user.roles[:]
3390
    formdef.only_allow_one = True
3391
    formdef.enable_tracking_codes = True
3392
    formdef.store()
3393

  
3394
    formdef.data_class().wipe()
3395

  
3396
    # create a formdata attached the agent
3397
    formdata = formdef.data_class()()
3398
    formdata.user_id = user.id
3399
    formdata.just_created()
3400
    formdata.store()
3401

  
3402
    app = login(get_app(pub))
3403
    resp = app.get('/backoffice/submission/form-title/')
3404
    resp.form['f1'] = 'test'
3405
    resp = resp.form.submit('submit')  # -> validation
3406
    resp = resp.form.submit('submit')  # -> submit
3407
    assert formdef.data_class().count() == 2
3408

  
3409

  
3383 3410
def test_backoffice_wscall_failure_display(http_requests, pub):
3384 3411
    user = create_user(pub)
3385 3412
    create_environment(pub)
wcs/backoffice/submission.py
146 146
        else:
147 147
            raise errors.AccessUnauthorizedError()
148 148

  
149
    def check_unique_submission(self):
150
        return None
151

  
149 152
    def get_sidebar(self, data):
150 153
        r = TemplateIO(html=True)
151 154

  
wcs/forms/root.py
600 600
                    root_url, _('Login with %s') % auth_contexts[auth_context])
601 601
        return r.getvalue()
602 602

  
603
    def check_unique_submission(self):
604
        if self.edit_mode:
605
            return None
606
        if not self.formdef.only_allow_one:
607
            return None
608
        for user_form in get_user_forms(self.formdef):
609
            if not user_form.is_draft():
610
                return user_form.id
611
        return None
612

  
603 613
    _pages = None
604 614
    @property
605 615
    def pages(self):
......
694 704
                token = randbytes(8)
695 705
                get_request().form['magictoken'] = token
696 706
                session.add_magictoken(token, self.edited_data.data)
697
        elif self.formdef.only_allow_one:
698
            user_forms = get_user_forms(self.formdef)
699
            if [x for x in user_forms if not x.is_draft()]:
700
                return redirect('%s/' % user_forms[0].id)
707

  
708
        # redirect to existing formdata if form is configured to only allow one
709
        # per user and it's already there.
710
        existing_form_id = self.check_unique_submission()
711
        if existing_form_id:
712
            return redirect('%s/' % existing_form_id)
701 713

  
702 714
        get_response().add_javascript(['jquery.js', 'qommon.forms.js'])
703 715
        form = Form()
......
1190 1202
        if get_request().get_path().startswith('/backoffice/'):
1191 1203
            filled.user_id = None
1192 1204

  
1193
        if self.formdef.only_allow_one:
1194
            # this is already checked in _q_index but it's done a second time
1195
            # just before a new form is to be stored.
1196
            user_forms = get_user_forms(self.formdef)
1197
            if [x for x in user_forms if not x.is_draft()]:
1198
                return redirect('%s/' % user_forms[0].id)
1205
        # this is already checked in _q_index but it's done a second time
1206
        # just before a new form is to be stored.
1207
        existing_form_id = self.check_unique_submission()
1208
        if existing_form_id:
1209
            return redirect('%s/' % existing_form_id)
1199 1210

  
1200 1211
        filled.store()
1201 1212
        self.set_tracking_code(filled)
1202
-