Projet

Général

Profil

0001-backoffice-prefill-with-form-user-data-8940.patch

Frédéric Péters, 10 novembre 2015 17:23

Télécharger (9,94 ko)

Voir les différences:

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(-)
tests/test_backoffice_pages.py
460 460
    assert not 'Channel' in resp.body
461 461

  
462 462
    number31.submission_channel = 'mail'
463
    number31.user_id = user.id
463 464
    number31.submission_context = {
464 465
            'mail_url': 'http://www.example.com/test.pdf',
465 466
            'thumbnail_url': 'http://www.example.com/thumbnail.png',
466
            'user_id': user.id,
467 467
            'comments': 'test_backoffice_submission_context',
468 468
            'summary_url': 'http://www.example.com/summary',
469 469
    }
......
805 805
    assert 'Running submission' in resp.body
806 806
    assert '>#%s' % formdata.id in resp.body
807 807

  
808
def test_backoffice_submission_prefill_user(pub):
809
    user = create_user(pub)
810
    create_environment(pub)
811
    other_user = pub.user_class(name='other user')
812
    other_user.email = 'other@example.net'
813
    other_user.store()
814

  
815
    formdef = FormDef.get_by_urlname('form-title')
816
    formdef.fields[0].prefill = {'type': 'user', 'value': 'email'}
817
    formdef.backoffice_submission_roles = user.roles[:]
818
    formdef.store()
819

  
820
    formdata = formdef.data_class()()
821
    formdata.backoffice_submission = True
822
    formdata.status = 'draft'
823
    formdata.data = {}
824
    formdata.submission_channel = 'mail'
825
    formdata.user_id = other_user.id
826
    formdata.submission_context = {}
827
    formdata.store()
828

  
829
    formdata2 = formdef.data_class()()
830
    formdata2.backoffice_submission = True
831
    formdata2.status = 'draft'
832
    formdata2.data = {}
833
    formdata.submission_channel = 'mail'
834
    formdata.user_id = None
835
    formdata2.submission_context = {}
836
    formdata2.store()
837

  
838
    app = login(get_app(pub))
839
    resp = app.get('/backoffice/submission/form-title/')
840
    assert resp.form['f1'].value == ''
841

  
842
    # restore a draft
843
    resp = app.get('/backoffice/submission/form-title/%s' % formdata.id)
844
    resp = resp.follow()
845
    # and check it got prefilled with the user from context
846
    assert resp.form['f1'].value == 'other@example.net'
847

  
848
    # restore another, without user id
849
    resp = app.get('/backoffice/submission/form-title/%s' % formdata2.id)
850
    resp = resp.follow()
851
    # and check it was not prefilled
852
    assert resp.form['f1'].value == ''
808 853

  
809 854
def test_backoffice_submission_prefill_user_via_formula(pub):
810 855
    user = create_user(pub)
......
823 868
    formdata.status = 'draft'
824 869
    formdata.data = {}
825 870
    formdata.submission_channel = 'mail'
826
    formdata.submission_context = {'user_id': other_user.id}
871
    formdata.user_id = other_user.id
872
    formdata.submission_context = {}
827 873
    formdata.store()
828 874

  
829 875
    formdata2 = formdef.data_class()()
......
831 877
    formdata2.status = 'draft'
832 878
    formdata2.data = {}
833 879
    formdata.submission_channel = 'mail'
880
    formdata.user_id = None
834 881
    formdata2.submission_context = {}
835 882
    formdata2.store()
836 883

  
tests/test_prefill.py
1 1
import sys
2 2
import shutil
3 3

  
4
from quixote import cleanup
4
from quixote import cleanup, get_request
5 5
from wcs.qommon.http_request import HTTPRequest
6 6
from wcs import fields
7 7

  
......
39 39
    field = fields.Field()
40 40
    field.prefill = {'type': 'user', 'value': 'email'}
41 41

  
42
    assert field.get_prefill_value() == 'test@example.net'
42
    assert field.get_prefill_value(user=get_request().user) == 'test@example.net'
43 43

  
44 44

  
45 45
def test_prefill_formula():
wcs/api.py
235 235
        if json_input.get('context'):
236 236
            formdata.submission_context = json_input['context']
237 237
            formdata.submission_channel = formdata.submission_context.pop('channel', None)
238
            formdata.user_id = formdata.submission_context.pop('user_id', None)
238 239
        formdata.store()
239 240
        if self.formdef.enable_tracking_codes:
240 241
            code = get_publisher().tracking_code_class()
wcs/backoffice/management.py
1323 1323
            if extra_context.get('mail_url'):
1324 1324
                r += htmltext('<p><a href="%s">%s</a></p>') % (
1325 1325
                        extra_context.get('mail_url'), _('Open'))
1326
            if extra_context.get('user_id'):
1327
                r += htmltext('<h3>%s</h3>') % _('Associated User')
1328
                r += htmltext('<p>%s</p>') % get_publisher().user_class.get(
1329
                        extra_context.get('user_id')).display_name
1330 1326
            if extra_context.get('comments'):
1331 1327
                r += htmltext('<h3>%s</h3>') % _('Comments')
1332 1328
                r += htmltext('<p>%s</p>') % extra_context.get('comments')
......
1335 1331
                        (extra_context.get('summary_url')))
1336 1332
            r += htmltext('</div>')
1337 1333

  
1334
        if formdata.user_id:
1335
            r += htmltext('<div class="extra-context">')
1336
            r += htmltext('<h3>%s</h3>') % _('Associated User')
1337
            r += htmltext('<p>%s</p>') % formdata.get_user().display_name
1338
            r += htmltext('</div>')
1339

  
1338 1340
        if formdata.user_id and get_publisher().is_using_postgresql():
1339 1341
            # display list of open formdata for the same user
1340 1342
            user_roles = [logged_users_role().id] + (get_request().user.roles or [])
wcs/fields.py
236 236
    def get_csv_value(self, element, hint=None):
237 237
        return []
238 238

  
239
    def get_prefill_value(self):
239
    def get_prefill_value(self, user=None):
240 240
        t = self.prefill.get('type')
241 241
        if t == 'string':
242 242
            return self.prefill.get('value')
243 243

  
244
        elif t == 'user' and get_request().user:
244
        elif t == 'user' and user:
245 245
            x = self.prefill.get('value')
246
            user = get_request().user
247 246
            if x == 'email':
248 247
                return user.email
249 248
            elif user.form_data:
wcs/forms/root.py
391 391
                if data.has_key(k):
392 392
                    v = data[k]
393 393
                elif field.prefill:
394
                    v = field.get_prefill_value()
394
                    prefill_user = get_request().user
395
                    if get_request().is_in_backoffice():
396
                        prefill_user = get_publisher().substitutions.get_context_variables(
397
                                ).get('form_user')
398
                    v = field.get_prefill_value(user=prefill_user)
395 399
                    if get_request().is_in_backoffice() and (
396
                            field.prefill and field.prefill.get('type') in ('user', 'geoloc')):
397
                        # turn off prefilling from user and geolocation
398
                        # attributes if the form is filled from the backoffice
400
                            field.prefill and field.prefill.get('type') == 'geoloc'):
401
                        # turn off prefilling from geolocation attributes if
402
                        # the form is filled from the backoffice
399 403
                        v = None
400 404
                    if v:
401 405
                        prefilled = True
......
487 491
            draft_formdata_id = formdata.data.get('draft_formdata_id')
488 492
            if draft_formdata_id:
489 493
                formdata = self.formdef.data_class().get(draft_formdata_id)
490
                extra_context = formdata.submission_context or {}
491
                formdata.user_id = extra_context.get('user_id')
492 494
        formdata.status = str('')
493 495
        get_publisher().substitutions.feed(formdata)
494 496

  
wcs/wf/form.py
140 140
                continue
141 141
            if not field.prefill or field.prefill.get('type') == 'none':
142 142
                continue
143
            v = field.get_prefill_value()
143
            # FIXME: this code duplicates code from wcs/forms/root.py :/
144
            prefill_user = get_request().user
145
            if get_request().is_in_backoffice():
146
                prefill_user = get_publisher().substitutions.get_context_variables(
147
                        ).get('form_user')
148
            v = field.get_prefill_value(user=prefill_user)
149
            if get_request().is_in_backoffice() and (
150
                    field.prefill and field.prefill.get('type') == 'geoloc'):
151
                # turn off prefilling from geolocation attributes if
152
                # the form is filled from the backoffice
153
                v = None
144 154
            if v:
145 155
                form.get_widget('f%s' % field.id).set_value(v)
146 156
                req.form['f%s' % field.id] = v
147
-