Projet

Général

Profil

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

Lauréline Guérin, 31 juillet 2020 15:25

Télécharger (9,43 ko)

Voir les différences:

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

 tests/test_backoffice_pages.py | 120 +++++++++++++++++++++++++++++++++
 wcs/backoffice/management.py   |  29 ++++++--
 wcs/formdata.py                |  19 ++++++
 3 files changed, 162 insertions(+), 6 deletions(-)
tests/test_backoffice_pages.py
5126 5126

  
5127 5127

  
5128 5128
def test_inspect_page(pub, local_user):
5129
    if not pub.is_using_postgresql():
5130
        pytest.skip('this requires SQL')
5131
        return
5132

  
5129 5133
    create_user(pub)
5130 5134
    create_environment(pub)
5131 5135

  
......
5282 5286
    assert 'Invalid block tag' in resp.text
5283 5287

  
5284 5288

  
5289
def test_inspect_page_with_related_objects(pub):
5290
    if not pub.is_using_postgresql():
5291
        pytest.skip('this requires SQL')
5292
        return
5293

  
5294
    user = create_user(pub, is_admin=True)
5295

  
5296
    formdef1 = FormDef()
5297
    formdef1.name = 'form 1'
5298
    formdef1.fields = []
5299
    formdef1.store()
5300
    formdef1.data_class().wipe()
5301
    formdata = formdef1.data_class()()
5302
    formdata.store()
5303
    formdef1.data_class()().store()
5304

  
5305
    formdef2 = FormDef()
5306
    formdef2.name = 'form 2'
5307
    formdef2.fields = []
5308
    formdef2.store()
5309
    formdef2.data_class().wipe()
5310
    formdef2.data_class()().store()
5311

  
5312
    carddef1 = CardDef()
5313
    carddef1.name = 'card 1'
5314
    carddef1.fields = []
5315
    carddef1.store()
5316
    carddef1.data_class().wipe()
5317
    carddata = carddef1.data_class()()
5318
    carddata.store()
5319

  
5320
    carddef2 = CardDef()
5321
    carddef2.name = 'card 2'
5322
    carddef2.fields = []
5323
    carddef2.backoffice_submission_roles = user.roles
5324
    carddef2.store()
5325
    carddef2.data_class().wipe()
5326
    carddef2.data_class()().store()
5327

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

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

  
5358
    assert [f.get_display_id() for f in formdata.get_children()] == [related_formdata1.get_display_id(), related_carddata2.get_display_id()]
5359
    assert [c.get_display_id() for c in carddata.get_children()] == [related_formdata2.get_display_id(), related_carddata1.get_display_id()]
5360

  
5361
    app = login(get_app(pub))
5362
    resp = app.get('%sinspect' % formdata.get_url(backoffice=True), status=200)
5363
    assert 'Related Forms/Cards' in resp.text
5364
    assert related_formdata1.get_url(backoffice=True) in resp.text
5365
    assert related_carddata2.get_url(backoffice=True) in resp.text
5366

  
5367
    resp = app.get('%sinspect' % carddata.get_url(backoffice=True), status=200)
5368
    assert 'Related Forms/Cards' in resp.text
5369
    assert related_formdata2.get_url(backoffice=True) in resp.text
5370
    assert related_carddata1.get_url(backoffice=True) in resp.text
5371

  
5372
    resp = app.get(related_formdata1.get_url(backoffice=True), status=200)
5373
    assert 'Original form' in resp.text
5374
    assert '(deleted)' not in resp.text
5375
    assert formdata.get_url(backoffice=True) in resp.text
5376

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

  
5382
    resp = app.get(related_carddata1.get_url(backoffice=True), status=200)
5383
    assert 'Original card' in resp.text
5384
    assert '(deleted)' not in resp.text
5385
    assert carddata.get_url(backoffice=True) in resp.text
5386

  
5387
    resp = app.get(related_carddata2.get_url(backoffice=True), status=200)
5388
    assert 'Original form' in resp.text
5389
    assert '(deleted)' not in resp.text
5390
    assert formdata.get_url(backoffice=True) in resp.text
5391

  
5392

  
5285 5393
def test_workflow_jump_previous(pub):
5394
    if not pub.is_using_postgresql():
5395
        pytest.skip('this requires SQL')
5396
        return
5397

  
5286 5398
    user = create_user(pub)
5287 5399
    create_environment(pub)
5288 5400

  
......
6459 6571

  
6460 6572
@pytest.fixture(params=[{'attach_to_history': True}, {}])
6461 6573
def create_formdata(request, pub):
6574
    if not pub.is_using_postgresql():
6575
        pytest.skip('this requires SQL')
6576
        return
6577

  
6462 6578
    admin = create_user(pub, is_admin=True)
6463 6579

  
6464 6580
    FormDef.wipe()
......
6716 6832

  
6717 6833

  
6718 6834
def test_backoffice_create_carddata_from_formdata(pub, studio):
6835
    if not pub.is_using_postgresql():
6836
        pytest.skip('this requires SQL')
6837
        return
6838

  
6719 6839
    CardDef.wipe()
6720 6840
    FormDef.wipe()
6721 6841

  
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
-