cubicweb/web/test/unittest_views_forms.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 20 Jan 2017 10:31:04 +0100
changeset 11910 af969080e7e6
parent 11870 3a84a79c4ed5
permissions -rw-r--r--
[autoform] Go through guess_field even when field class is specified using uicfg guess_field does not only find the proper field class for some attribute/relation, but also do some basic configuration depending on e.g. constraints. Before this patch, if one was specifying explicitly the field class using uicfg.autoform_field, this automatic configuration wasn't happening. Change form.field_by_name and ff.guess_field so explicit class are also automatically configured now. Closes #14474840
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11869
diff changeset
     1
# copyright 2014-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
#
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
#
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
# any later version.
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
#
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
# details.
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
#
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
10519
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    19
from logilab.common import tempattr, attrdict
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    20
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
from cubicweb.devtools.testlib import CubicWebTC
10519
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    22
from cubicweb.web.views.autoform import InlinedFormField
11910
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    23
from cubicweb.web.views.forms import EntityFieldsForm
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    24
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
class InlinedFormTC(CubicWebTC):
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
    def test_linked_to(self):
10088
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    29
        with self.admin_access.web_request() as req:
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    30
            formview = req.vreg['views'].select(
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    31
                'inline-creation', req,
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    32
                etype='File', rtype='described_by_test', role='subject',
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    33
                peid=123,
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    34
                petype='Salesterm')
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    35
            self.assertEqual({('described_by_test', 'object'): [123]},
ac63f7ec5af0 [test/view forms] use the official, undeprecated API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9873
diff changeset
    36
                             formview.form.linked_to)
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
10142
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    38
    def test_linked_to_parent_being_created(self):
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    39
        with self.admin_access.web_request() as req:
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    40
            formview = req.vreg['views'].select(
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    41
                'inline-creation', req,
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    42
                etype='File', rtype='described_by_test', role='subject',
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    43
                peid='A',
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    44
                petype='Salesterm')
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    45
            self.assertEqual(formview.form.linked_to, {})
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    46
10519
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    47
    def test_remove_js_depending_on_cardinality(self):
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    48
        with self.admin_access.web_request() as req:
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    49
            formview = req.vreg['views'].select(
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    50
                'inline-creation', req,
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    51
                etype='File', rtype='described_by_test', role='subject',
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    52
                peid='A',
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    53
                petype='Salesterm')
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    54
            # cardinality is 1, can't remove
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    55
            self.assertIsNone(formview._get_removejs())
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    56
            rdef = self.schema['Salesterm'].rdef('described_by_test')
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    57
            with tempattr(rdef, 'cardinality', '?*'):
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    58
                self.assertTrue(formview._get_removejs())
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    59
            with tempattr(rdef, 'cardinality', '+*'):
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    60
                # formview has no parent info (pform). This is what happens
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    61
                # when an inline form is requested through AJAX.
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    62
                self.assertTrue(formview._get_removejs())
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    63
                fakeview = attrdict(dict(rtype='described_by_test', role='subject'))
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    64
                # formview is first, can't be removed
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    65
                formview.pform = attrdict(fields=[InlinedFormField(view=formview),
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    66
                                                  InlinedFormField(view=fakeview)])
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    67
                self.assertIsNone(formview._get_removejs())
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    68
                # formview isn't first, can be removed
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    69
                formview.pform = attrdict(fields=[InlinedFormField(view=fakeview),
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    70
                                                  InlinedFormField(view=formview)])
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    71
                self.assertTrue(formview._get_removejs())
0019b7888dd7 [forms] don't display 'remove' link of inlined form for the first entity related using a '+' cardinality to its parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10142
diff changeset
    72
11910
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    73
    def test_field_by_name_consider_aff(self):
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    74
        class MyField(object):
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    75
            def __init__(self, *args, **kwargs):
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    76
                pass
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    77
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    78
        EntityFieldsForm.uicfg_aff.tag_attribute(('CWUser', 'firstname'), MyField)
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    79
        try:
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    80
            with self.admin_access.web_request() as req:
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    81
                form = req.vreg['forms'].select('base', req, entity=req.user)
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    82
                self.assertIsInstance(form.field_by_name('firstname', 'subject', req.user.e_schema),
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    83
                                      MyField)
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    84
        finally:
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    85
            EntityFieldsForm.uicfg_aff.del_rtag('CWUser', 'firstname', '*', 'subject')
af969080e7e6 [autoform] Go through guess_field even when field class is specified using uicfg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
    86
10142
f4a4556f23da [views/forms] Fix EntityFieldsForm.link_to when parent entity is being created
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10088
diff changeset
    87
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
if __name__ == '__main__':
11869
d8b66e3fd335 [test] Use plain unittest in unittest_views_forms.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    89
    import unittest
d8b66e3fd335 [test] Use plain unittest in unittest_views_forms.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    90
    unittest.main()