Projet

Général

Profil

0001-backoffice-list-related-forms-and-cards-in-inspect-v.patch

Lauréline Guérin, 31 juillet 2020 14:51

Télécharger (8,47 ko)

Voir les différences:

Subject: [PATCH] backoffice: list related forms and cards in inspect view
 (#43359)

 tests/test_backoffice_pages.py | 104 +++++++++++++++++++++++++++++++++
 wcs/backoffice/management.py   |  29 +++++++--
 wcs/formdata.py                |  19 ++++++
 3 files changed, 146 insertions(+), 6 deletions(-)
tests/test_backoffice_pages.py
5282 5282
    assert 'Invalid block tag' in resp.text
5283 5283

  
5284 5284

  
5285
def test_inspect_page_with_related_objects(pub):
5286
    if not pub.is_using_postgresql():
5287
        pytest.skip('this requires SQL')
5288
        return
5289

  
5290
    user = create_user(pub, is_admin=True)
5291

  
5292
    formdef1 = FormDef()
5293
    formdef1.name = 'form 1'
5294
    formdef1.fields = []
5295
    formdef1.store()
5296
    formdef1.data_class().wipe()
5297
    formdata = formdef1.data_class()()
5298
    formdata.store()
5299
    formdef1.data_class()().store()
5300

  
5301
    formdef2 = FormDef()
5302
    formdef2.name = 'form 2'
5303
    formdef2.fields = []
5304
    formdef2.store()
5305
    formdef2.data_class().wipe()
5306
    formdef2.data_class()().store()
5307

  
5308
    carddef1 = CardDef()
5309
    carddef1.name = 'card 1'
5310
    carddef1.fields = []
5311
    carddef1.store()
5312
    carddef1.data_class().wipe()
5313
    carddata = carddef1.data_class()()
5314
    carddata.store()
5315

  
5316
    carddef2 = CardDef()
5317
    carddef2.name = 'card 2'
5318
    carddef2.fields = []
5319
    carddef2.backoffice_submission_roles = user.roles
5320
    carddef2.store()
5321
    carddef2.data_class().wipe()
5322
    carddef2.data_class()().store()
5323

  
5324
    related_formdata1 = formdef2.data_class()()
5325
    related_formdata1.submission_context = {
5326
        'orig_object_type': 'formdef',
5327
        'orig_formdata_id': str(formdata.id),
5328
        'orig_formdef_id': str(formdef1.id),
5329
    }
5330
    related_formdata1.store()
5331
    related_formdata2 = formdef2.data_class()()
5332
    related_formdata2.submission_context = {
5333
        'orig_object_type': 'carddef',
5334
        'orig_formdata_id': str(carddata.id),
5335
        'orig_formdef_id': str(carddef1.id),
5336
    }
5337
    related_formdata2.store()
5338

  
5339
    related_carddata1 = carddef2.data_class()()
5340
    related_carddata1.submission_context = {
5341
        'orig_object_type': 'carddef',
5342
        'orig_formdata_id': str(carddata.id),
5343
        'orig_formdef_id': str(carddef1.id),
5344
    }
5345
    related_carddata1.store()
5346
    related_carddata2 = carddef2.data_class()()
5347
    related_carddata2.submission_context = {
5348
        'orig_object_type': 'formdef',
5349
        'orig_formdata_id': str(formdata.id),
5350
        'orig_formdef_id': str(formdef1.id),
5351
    }
5352
    related_carddata2.store()
5353

  
5354
    assert [f.get_display_id() for f in formdata.get_children()] == [related_formdata1.get_display_id(), related_carddata2.get_display_id()]
5355
    assert [c.get_display_id() for c in carddata.get_children()] == [related_formdata2.get_display_id(), related_carddata1.get_display_id()]
5356

  
5357
    app = login(get_app(pub))
5358
    resp = app.get('%sinspect' % formdata.get_url(backoffice=True), status=200)
5359
    assert 'Related Forms/Cards' in resp.text
5360
    assert related_formdata1.get_url(backoffice=True) in resp.text
5361
    assert related_carddata2.get_url(backoffice=True) in resp.text
5362

  
5363
    resp = app.get('%sinspect' % carddata.get_url(backoffice=True), status=200)
5364
    assert 'Related Forms/Cards' in resp.text
5365
    assert related_formdata2.get_url(backoffice=True) in resp.text
5366
    assert related_carddata1.get_url(backoffice=True) in resp.text
5367

  
5368
    resp = app.get(related_formdata1.get_url(backoffice=True), status=200)
5369
    assert 'Original form' in resp.text
5370
    assert '(deleted)' not in resp.text
5371
    assert formdata.get_url(backoffice=True) in resp.text
5372

  
5373
    resp = app.get(related_formdata2.get_url(backoffice=True), status=200)
5374
    assert 'Original card' in resp.text
5375
    assert '(deleted)' not in resp.text
5376
    assert carddata.get_url(backoffice=True) in resp.text
5377

  
5378
    resp = app.get(related_carddata1.get_url(backoffice=True), status=200)
5379
    assert 'Original card' in resp.text
5380
    assert '(deleted)' not in resp.text
5381
    assert carddata.get_url(backoffice=True) in resp.text
5382

  
5383
    resp = app.get(related_carddata2.get_url(backoffice=True), status=200)
5384
    assert 'Original form' in resp.text
5385
    assert '(deleted)' not in resp.text
5386
    assert formdata.get_url(backoffice=True) in resp.text
5387

  
5388

  
5285 5389
def test_workflow_jump_previous(pub):
5286 5390
    user = create_user(pub)
5287 5391
    create_environment(pub)
wcs/backoffice/management.py
2729 2729
            extra_context = formdata.submission_context or {}
2730 2730
            r += htmltext('<div class="extra-context">')
2731 2731
            if extra_context.get('orig_formdef_id'):
2732
                r += htmltext('<h3>%s</h3>') % _('Original form')
2732
                object_type = extra_context.get('orig_object_type', 'formdef')
2733
                if object_type == 'formdef':
2734
                    r += htmltext('<h3>%s</h3>') % _('Original form')
2735
                    object_class = FormDef
2736
                else:
2737
                    r += htmltext('<h3>%s</h3>') % _('Original card')
2738
                    object_class = CardDef
2733 2739
                try:
2734
                    orig_formdata = FormDef.get(extra_context.get('orig_formdef_id')
2735
                            ).data_class().get(extra_context.get('orig_formdata_id'))
2740
                    orig_formdata = object_class.get(
2741
                        extra_context.get('orig_formdef_id')
2742
                    ).data_class().get(extra_context.get('orig_formdata_id'))
2736 2743
                except KeyError:
2737 2744
                    r += htmltext('<p>%s</p>') % _('(deleted)')
2738 2745
                else:
2739
                    r += htmltext('<p><a href="%s">%s %s</a></p>') % (
2746
                    r += htmltext('<p><a href="%s">%s</a></p>') % (
2740 2747
                             orig_formdata.get_url(backoffice=True),
2741
                             orig_formdata.formdef.name,
2742
                             orig_formdata.get_display_id())
2748
                             orig_formdata.get_display_name())
2743 2749
            if formdata.submission_channel:
2744 2750
                r += htmltext('<h3>%s</h3>') % '%s: %s' % (
2745 2751
                        _('Channel'), formdata.get_submission_channel_label())
......
3130 3136
            r += htmltext('</ul>')
3131 3137
            r += htmltext('</div>')
3132 3138

  
3139
        children = self.filled.get_children()
3140
        if children:
3141
            r += htmltext('<div id="inspect-related" class="section">')
3142
            r += htmltext('<h2>%s</h2>') % _('Related Forms/Cards')
3143
            r += htmltext('<ul class="form-inspector biglist">')
3144
            for child in children:
3145
                r += htmltext('<li><a href="%s">%s</a></li>') % (
3146
                    child.get_url(backoffice=True), child.get_display_name())
3147
            r += htmltext('</ul>')
3148
            r += htmltext('</div>')
3149

  
3133 3150
        return r.getvalue()
3134 3151

  
3135 3152
    def inspect_tool(self):
wcs/formdata.py
382 382
        except KeyError:
383 383
            return None
384 384

  
385
    def get_children(self):
386
        from .carddef import CardDef
387
        from .formdef import FormDef
388
        from .sql import NotNull
389
        children = []
390

  
391
        for objclass in [FormDef, CardDef]:
392
            for objdef in objclass.select():
393
                for objdata in objdef.data_class().select(clause=[NotNull('submission_context')]):
394
                    if objdata.submission_context.get('orig_object_type', 'formdef') != self.formdef.__class__.__name__.lower():
395
                        continue
396
                    if objdata.submission_context.get('orig_formdef_id') != str(self.formdef.id):
397
                        continue
398
                    if objdata.submission_context.get('orig_formdata_id') != str(self.id):
399
                        continue
400
                    children.append(objdata)
401

  
402
        return children
403

  
385 404
    def just_created(self):
386 405
        self.receipt_time = time.localtime()
387 406
        self.status = 'wf-%s' % self.formdef.workflow.possible_status[0].id
388
-