Projet

Général

Profil

0001-misc-use-an-afterjob-to-update-form-digests-23919.patch

Frédéric Péters, 18 mai 2018 11:37

Télécharger (4,48 ko)

Voir les différences:

Subject: [PATCH] misc: use an afterjob to update form digests (#23919)

 tests/test_admin_pages.py | 21 ++++-----------------
 wcs/admin/forms.py        | 17 +++++++----------
 wcs/sql.py                | 18 ------------------
 3 files changed, 11 insertions(+), 45 deletions(-)
tests/test_admin_pages.py
771 771
    formdef = FormDef.get(formdef.id)
772 772
    assert formdef.digest_template == 'X{{form_var_test}}Y'
773 773

  
774
    if pub.is_using_postgresql():
775
        assert 'Existing forms will be updated in the background.' in resp.body
776
        from wcs import sql
777
        conn, cur = sql.get_connection_and_cursor()
778
        assert sql.is_any_reindex_needed('formdef', conn=conn, cur=cur) is True
779
        conn.commit()
780
        cur.close()
781

  
782
        # run "background" reindexing
783
        sql.reindex()
784
        assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
785

  
786
    else:
787
        # no automatic background reindexing for pickle storage
788
        assert 'This change will be applied to new and modified forms.' in resp.body
789
        formdef.data_class().get(formdata.id).store()
790
        assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
774
    assert 'Existing forms will be updated in the background.' in resp.body
775
    # afterjobs are actually run synchronously during tests; we don't have
776
    # to wait to check the digest has been updated:
777
    assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
791 778

  
792 779
def test_form_delete(pub):
793 780
    create_role()
wcs/admin/forms.py
253 253
        form.add(StringWidget, 'digest_template', title=_('Digest'),
254 254
                value=self.formdef.digest_template, size=50)
255 255
        result = self.handle(form, _('Templates'))
256
        if self.changed:
257
            if self.formdef.data_class().count():
258
                if get_publisher().is_using_postgresql():
259
                    from wcs import sql
260
                    sql.set_reindex('formdef:%s' % self.formdef.id, 'needed')
261
                    get_session().message = ('info',
262
                            _('Existing forms will be updated in the background.'))
263
                else:
264
                    get_session().message = ('info',
265
                            _('This change will be applied to new and modified forms.'))
256
        if self.changed and self.formdef.data_class().count():
257
            def update(job=None):
258
                for formdata in self.formdef.data_class().select():
259
                    formdata.store()
260
            job = get_response().add_after_job(N_('Updading digests'), update)
261
            get_session().message = ('info',
262
                    _('Existing forms will be updated in the background.'))
266 263
        return result
267 264

  
268 265
    def handle(self, form, title):
wcs/sql.py
2170 2170
        return False
2171 2171
    return row[0] == 'needed'
2172 2172

  
2173
@guard_postgres
2174
def is_any_reindex_needed(index_prefix, conn, cur):
2175
    do_meta_table(conn, cur, insert_current_sql_level=False)
2176
    key_name = 'reindex_%s:%%' % index_prefix
2177
    cur.execute('''SELECT value FROM wcs_meta
2178
                    WHERE key LIKE %s
2179
                      AND value = %s
2180
                    LIMIT 1''', (key_name, 'needed'))
2181
    row = cur.fetchone()
2182
    return bool(row is not None)
2183

  
2184 2173
@guard_postgres
2185 2174
def set_reindex(index, value, conn=None, cur=None):
2186 2175
    own_conn = False
......
2291 2280
                formdata.store()
2292 2281
        set_reindex('formdata', 'done', conn=conn, cur=cur)
2293 2282

  
2294
    if is_any_reindex_needed('formdef', conn=conn, cur=cur):
2295
        for formdef in FormDef.select():
2296
            if is_reindex_needed('formdef:%s' % formdef.id, conn=conn, cur=cur):
2297
                for formdata in formdef.data_class().select(iterator=True):
2298
                    formdata.store()
2299
                set_reindex('formdef:%s' % formdef.id, 'done', conn=conn, cur=cur)
2300

  
2301 2283
    conn.commit()
2302 2284
    cur.close()
2303
-