Projet

Général

Profil

0001-misc-keep-track-of-first-formdata-when-running-a-mas.patch

Frédéric Péters, 02 novembre 2020 16:03

Télécharger (6,36 ko)

Voir les différences:

Subject: [PATCH] misc: keep track of first formdata when running a mass action
 (#47878)

 tests/backoffice_pages/test_all.py | 56 ++++++++++++++++++++++++++++++
 wcs/backoffice/management.py       | 14 +++++++-
 wcs/formdata.py                    |  7 ++--
 wcs/qommon/substitution.py         |  3 ++
 4 files changed, 77 insertions(+), 3 deletions(-)
tests/backoffice_pages/test_all.py
1846 1846
        assert formdef.data_class().get(id).status == 'wf-accepted'
1847 1847

  
1848 1848

  
1849
def test_backoffice_multi_actions_first_form(pub):
1850
    create_superuser(pub)
1851
    create_environment(pub)
1852
    formdef = FormDef.get_by_urlname('form-title')
1853

  
1854
    app = login(get_app(pub))
1855
    resp = app.get('/backoffice/management/form-title/')
1856
    assert 'id="multi-actions"' in resp.text  # always there
1857

  
1858
    workflow = Workflow.get_default_workflow()
1859
    workflow.id = '2'
1860
    action = workflow.add_global_action('Mark as duplicates')
1861
    jump = action.append_item('jump')
1862
    jump.condition = {'type': 'django', 'value': "mass_action_index != 0"}
1863
    jump.status = 'rejected'
1864

  
1865
    jump2 = action.append_item('jump')
1866
    jump2.condition = {'type': 'django', 'value': "mass_action_index == 0"}
1867
    jump2.status = 'accepted'
1868

  
1869
    register_comment = RegisterCommenterWorkflowStatusItem()
1870
    register_comment.id = '_comment'
1871
    register_comment.comment = '<p>Original form: {{ first_form_number }}.</p>'
1872
    assert workflow.possible_status[2].id == 'rejected'
1873
    workflow.possible_status[2].items.append(register_comment)
1874
    register_comment.parent = workflow.possible_status[2]
1875

  
1876
    trigger = action.triggers[0]
1877
    trigger.roles = [x.id for x in Role.select() if x.name == 'foobar']
1878

  
1879
    workflow.store()
1880
    formdef.workflow_id = workflow.id
1881
    formdef.store()
1882

  
1883
    resp = app.get('/backoffice/management/form-title/')
1884
    assert 'id="multi-actions"' in resp.text
1885
    ids = []
1886
    for checkbox in resp.forms[0].fields['select[]'][1:6]:
1887
        ids.append(checkbox._value)
1888
        checkbox.checked = True
1889
    resp = resp.forms[0].submit('button-action-1')
1890
    assert '?job=' in resp.location
1891
    resp = resp.follow()
1892
    assert 'Executing task &quot;Mark as duplicates&quot; on forms' in resp.text
1893
    assert '>completed<' in resp.text
1894
    first_formdata = None
1895
    for i, id in enumerate(sorted(ids, key=lambda x: int(x))):
1896
        if i == 0:
1897
            first_formdata = formdef.data_class().get(id)
1898
            assert formdef.data_class().get(id).status == 'wf-accepted'
1899
        else:
1900
            assert formdef.data_class().get(id).status == 'wf-rejected'
1901
            assert (formdef.data_class().get(id).evolution[-1].parts[0].content ==
1902
                    '<p>Original form: %s.</p>' % first_formdata.get_display_id())
1903

  
1904

  
1849 1905
def test_backoffice_statistics_with_no_formdefs(pub):
1850 1906
    create_user(pub)
1851 1907
    create_environment(pub)
wcs/backoffice/management.py
1949 1949
                self.user = get_request().user
1950 1950

  
1951 1951
            def execute(self, job=None):
1952
                formdatas = self.formdef.data_class().get_ids(self.item_ids)
1952
                # sort formdata IDs so we know for sure the processing order
1953
                # (they are also converted as integers as it's required for
1954
                # keep_order when using postgresql).
1955
                item_ids = sorted([int(x) for x in self.item_ids])
1956
                formdatas = self.formdef.data_class().get_ids(item_ids, keep_order=True)
1953 1957
                job.completion_status = '{}/{}'.format(0, len(formdatas))
1954 1958
                job.store()
1955 1959
                publisher = get_publisher()
1960
                # conserver initial_formdata = formdatas[0]
1961
                if formdatas:
1962
                    first_lazy_form = formdatas[0].get_as_lazy()
1956 1963
                for i, formdata in enumerate(formdatas):
1957 1964
                    publisher.substitutions.reset()
1958 1965
                    publisher.substitutions.feed(publisher)
1959 1966
                    publisher.substitutions.feed(self.formdef)
1960 1967
                    publisher.substitutions.feed(formdata)
1968
                    publisher.substitutions.feed({
1969
                        'first_form': first_lazy_form,
1970
                        'mass_action_index': i,
1971
                        'mass_action_length': len(formdatas),
1972
                    })
1961 1973
                    if getattr(self.action['action'], 'status_action', False):
1962 1974
                        # manual jump action
1963 1975
                        from wcs.wf.jump import jump_and_perform
wcs/formdata.py
840 840

  
841 841
        return d
842 842

  
843
    def get_as_lazy(self):
844
        from wcs.variables import LazyFormData
845
        return LazyFormData(self)
846

  
843 847
    def get_substitution_variables(self, minimal=False):
844 848
        from .qommon.substitution import CompatibilityNamesDict
845
        from wcs.variables import LazyFormData
846 849
        from wcs.workflows import AttachmentsSubstitutionProxy
847 850
        variables = CompatibilityNamesDict({
848
            'form': LazyFormData(self),
851
            'form': self.get_as_lazy(),
849 852
            'attachments': AttachmentsSubstitutionProxy(self),
850 853
        })
851 854
        if self.formdef.category:
wcs/qommon/substitution.py
119 119
            return d
120 120
        d = CompatibilityNamesDict()
121 121
        for source in self.sources:
122
            if isinstance(source, dict):
123
                d.update(source)
124
                continue
122 125
            d.update(source.get_substitution_variables())
123 126
            if not lazy and hasattr(source, 'get_static_substitution_variables'):
124 127
                d.update(source.get_static_substitution_variables())
125
-