From 2bbe00c58aae002ad1e3f4977188bc827f85e320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 10 Nov 2015 15:17:05 +0100 Subject: [PATCH] backoffice: prefill with form user data (#8940) --- tests/test_backoffice_pages.py | 51 ++++++++++++++++++++++++++++++++++++++++-- tests/test_prefill.py | 4 ++-- wcs/api.py | 1 + wcs/backoffice/management.py | 10 +++++---- wcs/fields.py | 5 ++--- wcs/forms/root.py | 14 +++++++----- wcs/wf/form.py | 12 +++++++++- 7 files changed, 79 insertions(+), 18 deletions(-) diff --git a/tests/test_backoffice_pages.py b/tests/test_backoffice_pages.py index 51da877..7b8320e 100644 --- a/tests/test_backoffice_pages.py +++ b/tests/test_backoffice_pages.py @@ -460,10 +460,10 @@ def test_backoffice_submission_context(pub): assert not 'Channel' in resp.body number31.submission_channel = 'mail' + number31.user_id = user.id number31.submission_context = { 'mail_url': 'http://www.example.com/test.pdf', 'thumbnail_url': 'http://www.example.com/thumbnail.png', - 'user_id': user.id, 'comments': 'test_backoffice_submission_context', 'summary_url': 'http://www.example.com/summary', } @@ -805,6 +805,51 @@ def test_backoffice_submission_sections(pub): assert 'Running submission' in resp.body assert '>#%s' % formdata.id in resp.body +def test_backoffice_submission_prefill_user(pub): + user = create_user(pub) + create_environment(pub) + other_user = pub.user_class(name='other user') + other_user.email = 'other@example.net' + other_user.store() + + formdef = FormDef.get_by_urlname('form-title') + formdef.fields[0].prefill = {'type': 'user', 'value': 'email'} + formdef.backoffice_submission_roles = user.roles[:] + formdef.store() + + formdata = formdef.data_class()() + formdata.backoffice_submission = True + formdata.status = 'draft' + formdata.data = {} + formdata.submission_channel = 'mail' + formdata.user_id = other_user.id + formdata.submission_context = {} + formdata.store() + + formdata2 = formdef.data_class()() + formdata2.backoffice_submission = True + formdata2.status = 'draft' + formdata2.data = {} + formdata.submission_channel = 'mail' + formdata.user_id = None + formdata2.submission_context = {} + formdata2.store() + + app = login(get_app(pub)) + resp = app.get('/backoffice/submission/form-title/') + assert resp.form['f1'].value == '' + + # restore a draft + resp = app.get('/backoffice/submission/form-title/%s' % formdata.id) + resp = resp.follow() + # and check it got prefilled with the user from context + assert resp.form['f1'].value == 'other@example.net' + + # restore another, without user id + resp = app.get('/backoffice/submission/form-title/%s' % formdata2.id) + resp = resp.follow() + # and check it was not prefilled + assert resp.form['f1'].value == '' def test_backoffice_submission_prefill_user_via_formula(pub): user = create_user(pub) @@ -823,7 +868,8 @@ def test_backoffice_submission_prefill_user_via_formula(pub): formdata.status = 'draft' formdata.data = {} formdata.submission_channel = 'mail' - formdata.submission_context = {'user_id': other_user.id} + formdata.user_id = other_user.id + formdata.submission_context = {} formdata.store() formdata2 = formdef.data_class()() @@ -831,6 +877,7 @@ def test_backoffice_submission_prefill_user_via_formula(pub): formdata2.status = 'draft' formdata2.data = {} formdata.submission_channel = 'mail' + formdata.user_id = None formdata2.submission_context = {} formdata2.store() diff --git a/tests/test_prefill.py b/tests/test_prefill.py index 9e35ec8..aaa5a81 100644 --- a/tests/test_prefill.py +++ b/tests/test_prefill.py @@ -1,7 +1,7 @@ import sys import shutil -from quixote import cleanup +from quixote import cleanup, get_request from wcs.qommon.http_request import HTTPRequest from wcs import fields @@ -39,7 +39,7 @@ def test_prefill_user(): field = fields.Field() field.prefill = {'type': 'user', 'value': 'email'} - assert field.get_prefill_value() == 'test@example.net' + assert field.get_prefill_value(user=get_request().user) == 'test@example.net' def test_prefill_formula(): diff --git a/wcs/api.py b/wcs/api.py index f4cfe2e..1b02e6c 100644 --- a/wcs/api.py +++ b/wcs/api.py @@ -235,6 +235,7 @@ class ApiFormdefDirectory(Directory): if json_input.get('context'): formdata.submission_context = json_input['context'] formdata.submission_channel = formdata.submission_context.pop('channel', None) + formdata.user_id = formdata.submission_context.pop('user_id', None) formdata.store() if self.formdef.enable_tracking_codes: code = get_publisher().tracking_code_class() diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py index 44f1e28..cbded38 100644 --- a/wcs/backoffice/management.py +++ b/wcs/backoffice/management.py @@ -1323,10 +1323,6 @@ class FormBackOfficeStatusPage(FormStatusPage): if extra_context.get('mail_url'): r += htmltext('

%s

') % ( extra_context.get('mail_url'), _('Open')) - if extra_context.get('user_id'): - r += htmltext('

%s

') % _('Associated User') - r += htmltext('

%s

') % get_publisher().user_class.get( - extra_context.get('user_id')).display_name if extra_context.get('comments'): r += htmltext('

%s

') % _('Comments') r += htmltext('

%s

') % extra_context.get('comments') @@ -1335,6 +1331,12 @@ class FormBackOfficeStatusPage(FormStatusPage): (extra_context.get('summary_url'))) r += htmltext('') + if formdata.user_id: + r += htmltext('
') + r += htmltext('

%s

') % _('Associated User') + r += htmltext('

%s

') % formdata.get_user().display_name + r += htmltext('
') + if formdata.user_id and get_publisher().is_using_postgresql(): # display list of open formdata for the same user user_roles = [logged_users_role().id] + (get_request().user.roles or []) diff --git a/wcs/fields.py b/wcs/fields.py index 9c9b388..936a26b 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -236,14 +236,13 @@ class Field(object): def get_csv_value(self, element, hint=None): return [] - def get_prefill_value(self): + def get_prefill_value(self, user=None): t = self.prefill.get('type') if t == 'string': return self.prefill.get('value') - elif t == 'user' and get_request().user: + elif t == 'user' and user: x = self.prefill.get('value') - user = get_request().user if x == 'email': return user.email elif user.form_data: diff --git a/wcs/forms/root.py b/wcs/forms/root.py index d84d619..9ec6213 100644 --- a/wcs/forms/root.py +++ b/wcs/forms/root.py @@ -391,11 +391,15 @@ class FormPage(Directory): if data.has_key(k): v = data[k] elif field.prefill: - v = field.get_prefill_value() + prefill_user = get_request().user + if get_request().is_in_backoffice(): + prefill_user = get_publisher().substitutions.get_context_variables( + ).get('form_user') + v = field.get_prefill_value(user=prefill_user) if get_request().is_in_backoffice() and ( - field.prefill and field.prefill.get('type') in ('user', 'geoloc')): - # turn off prefilling from user and geolocation - # attributes if the form is filled from the backoffice + field.prefill and field.prefill.get('type') == 'geoloc'): + # turn off prefilling from geolocation attributes if + # the form is filled from the backoffice v = None if v: prefilled = True @@ -487,8 +491,6 @@ class FormPage(Directory): draft_formdata_id = formdata.data.get('draft_formdata_id') if draft_formdata_id: formdata = self.formdef.data_class().get(draft_formdata_id) - extra_context = formdata.submission_context or {} - formdata.user_id = extra_context.get('user_id') formdata.status = str('') get_publisher().substitutions.feed(formdata) diff --git a/wcs/wf/form.py b/wcs/wf/form.py index af26244..af2f792 100644 --- a/wcs/wf/form.py +++ b/wcs/wf/form.py @@ -140,7 +140,17 @@ class FormWorkflowStatusItem(WorkflowStatusItem): continue if not field.prefill or field.prefill.get('type') == 'none': continue - v = field.get_prefill_value() + # FIXME: this code duplicates code from wcs/forms/root.py :/ + prefill_user = get_request().user + if get_request().is_in_backoffice(): + prefill_user = get_publisher().substitutions.get_context_variables( + ).get('form_user') + v = field.get_prefill_value(user=prefill_user) + if get_request().is_in_backoffice() and ( + field.prefill and field.prefill.get('type') == 'geoloc'): + # turn off prefilling from geolocation attributes if + # the form is filled from the backoffice + v = None if v: form.get_widget('f%s' % field.id).set_value(v) req.form['f%s' % field.id] = v -- 2.6.2