misc: use an afterjob to update form digests (#23919)
This commit is contained in:
parent
493bc0078d
commit
4dc3d6ea3a
|
@ -771,23 +771,10 @@ def test_form_digest_template(pub):
|
|||
formdef = FormDef.get(formdef.id)
|
||||
assert formdef.digest_template == 'X{{form_var_test}}Y'
|
||||
|
||||
if pub.is_using_postgresql():
|
||||
assert 'Existing forms will be updated in the background.' in resp.body
|
||||
from wcs import sql
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
assert sql.is_any_reindex_needed('formdef', conn=conn, cur=cur) is True
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
# run "background" reindexing
|
||||
sql.reindex()
|
||||
assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
|
||||
|
||||
else:
|
||||
# no automatic background reindexing for pickle storage
|
||||
assert 'This change will be applied to new and modified forms.' in resp.body
|
||||
formdef.data_class().get(formdata.id).store()
|
||||
assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
|
||||
assert 'Existing forms will be updated in the background.' in resp.body
|
||||
# afterjobs are actually run synchronously during tests; we don't have
|
||||
# to wait to check the digest has been updated:
|
||||
assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
|
||||
|
||||
def test_form_delete(pub):
|
||||
create_role()
|
||||
|
|
|
@ -253,16 +253,13 @@ class OptionsDirectory(Directory):
|
|||
form.add(StringWidget, 'digest_template', title=_('Digest'),
|
||||
value=self.formdef.digest_template, size=50)
|
||||
result = self.handle(form, _('Templates'))
|
||||
if self.changed:
|
||||
if self.formdef.data_class().count():
|
||||
if get_publisher().is_using_postgresql():
|
||||
from wcs import sql
|
||||
sql.set_reindex('formdef:%s' % self.formdef.id, 'needed')
|
||||
get_session().message = ('info',
|
||||
_('Existing forms will be updated in the background.'))
|
||||
else:
|
||||
get_session().message = ('info',
|
||||
_('This change will be applied to new and modified forms.'))
|
||||
if self.changed and self.formdef.data_class().count():
|
||||
def update(job=None):
|
||||
for formdata in self.formdef.data_class().select(order_by='id'):
|
||||
formdata.store()
|
||||
job = get_response().add_after_job(N_('Updating digests'), update)
|
||||
get_session().message = ('info',
|
||||
_('Existing forms will be updated in the background.'))
|
||||
return result
|
||||
|
||||
def handle(self, form, title):
|
||||
|
|
18
wcs/sql.py
18
wcs/sql.py
|
@ -2170,17 +2170,6 @@ def is_reindex_needed(index, conn, cur):
|
|||
return False
|
||||
return row[0] == 'needed'
|
||||
|
||||
@guard_postgres
|
||||
def is_any_reindex_needed(index_prefix, conn, cur):
|
||||
do_meta_table(conn, cur, insert_current_sql_level=False)
|
||||
key_name = 'reindex_%s:%%' % index_prefix
|
||||
cur.execute('''SELECT value FROM wcs_meta
|
||||
WHERE key LIKE %s
|
||||
AND value = %s
|
||||
LIMIT 1''', (key_name, 'needed'))
|
||||
row = cur.fetchone()
|
||||
return bool(row is not None)
|
||||
|
||||
@guard_postgres
|
||||
def set_reindex(index, value, conn=None, cur=None):
|
||||
own_conn = False
|
||||
|
@ -2291,12 +2280,5 @@ def reindex():
|
|||
formdata.store()
|
||||
set_reindex('formdata', 'done', conn=conn, cur=cur)
|
||||
|
||||
if is_any_reindex_needed('formdef', conn=conn, cur=cur):
|
||||
for formdef in FormDef.select():
|
||||
if is_reindex_needed('formdef:%s' % formdef.id, conn=conn, cur=cur):
|
||||
for formdata in formdef.data_class().select(iterator=True):
|
||||
formdata.store()
|
||||
set_reindex('formdef:%s' % formdef.id, 'done', conn=conn, cur=cur)
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
|
Loading…
Reference in New Issue