web/views/ajaxedit.py
author David Douard <david.douard@logilab.fr>
Wed, 25 May 2016 17:19:14 +0200
changeset 11263 9ae85b069325
parent 9897 fa44db7da2dc
permissions -rw-r--r--
[ldapfeed] ldap3 do not raise an exception in case of failure of cnx.bind() but return 'False' instead.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6927
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""Set of views allowing edition of entities/relations using ajax"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
1622
1a1c494b88e2 xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    22
from cubicweb import role
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    23
from cubicweb.view import View
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    24
from cubicweb.predicates import match_form_params, match_kwargs
6924
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    25
from cubicweb.web import component, stdmsgs, formwidgets as fw
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
6924
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    27
class AddRelationView(component.EditRelationMixIn, View):
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    28
    """base class for view which let add entities linked by a given relation
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    30
    subclasses should define at least id, rtype and target class attributes.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    __registry__ = 'views'
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2867
diff changeset
    33
    __regid__ = 'xaddrelation'
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
    34
    __select__ = (match_form_params('rtype', 'target')
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
    35
                  | match_kwargs('rtype', 'target'))
2799
b703639614e7 refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
    36
    cw_property_defs = {} # don't want to inherit this from Box
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    expected_kwargs = form_params = ('rtype', 'target')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    def cell_call(self, row, col, rtype=None, target=None, etype=None):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    40
        self.rtype = rtype or self._cw.form['rtype']
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    41
        self.target = target or self._cw.form['target']
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    42
        self.etype = etype or self._cw.form.get('etype')
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    43
        entity = self.cw_rset.get_entity(row, col)
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3451
diff changeset
    44
        rschema = self._cw.vreg.schema.rschema(self.rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        if not self.etype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
            if self.target == 'object':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
                etypes = rschema.objects(entity.e_schema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
                etypes = rschema.subjects(entity.e_schema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
            if len(etypes) == 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
                self.etype = etypes[0]
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    52
        self.w(u'<div id="%s">' % self.domid)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    53
        self.w(u'<h1>%s</h1>' % self._cw._('relation %(relname)s of %(ent)s')
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    54
               % {'relname': rschema.display_name(self._cw, role(self)),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
                  'ent': entity.view('incontext')})
9870
d30ff49d4a94 [views] Replace poorly named "invisible" class with "list-unstyled"
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8190
diff changeset
    56
        self.w(u'<ul class="list-unstyled">')
2866
4c804711548d B bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    57
        for boxitem in self.unrelated_boxitems(entity):
9870
d30ff49d4a94 [views] Replace poorly named "invisible" class with "list-unstyled"
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8190
diff changeset
    58
            self.w('<li>%s</li>' % boxitem)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        self.w(u'</ul></div>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    def unrelated_entities(self, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        """returns the list of unrelated entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        if etype is not defined on the Box's class, the default
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        behaviour is to use the entity's appropraite vocabulary function
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        # use entity.unrelated if we've been asked for a particular etype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        if getattr(self, 'etype', None):
1622
1a1c494b88e2 xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    69
            rset = entity.unrelated(self.rtype, self.etype, role(self),
1a1c494b88e2 xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    70
                                    ordermethod='fetch_order')
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6927
diff changeset
    71
            self.paginate(self._cw, rset=rset, w=self.w)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
            return rset.entities()
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    73
        super(AddRelationView, self).unrelated_entities(self)
6924
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    74
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    75
6927
340aba0c8c9e ooops :D
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6924
diff changeset
    76
def ajax_composite_form(container, entity, rtype, okjs, canceljs,
340aba0c8c9e ooops :D
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6924
diff changeset
    77
                        entityfkwargs=None):
6924
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    78
    """
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    79
    * if entity is None, edit container (assert container.has_eid())
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    80
    * if entity has not eid, will be created
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    81
    * if container has not eid, will be created (see vcreview InsertionPoint)
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    82
    """
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    83
    req = container._cw
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    84
    parentexists = entity is None or container.has_eid()
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    85
    buttons = [fw.Button(onclick=okjs),
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    86
               fw.Button(stdmsgs.BUTTON_CANCEL, onclick=canceljs)]
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    87
    freg = req.vreg['forms']
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    88
    # main form kwargs
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    89
    mkwargs = dict(action='#', domid='%sForm%s' % (rtype, container.eid),
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    90
                   form_buttons=buttons,
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    91
                   onsubmit='javascript: %s; return false' % okjs)
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    92
    # entity form kwargs
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    93
    # use formtype=inlined to skip the generic relations edition section
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    94
    fkwargs = dict(entity=entity or container, formtype='inlined')
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    95
    if entityfkwargs is not None:
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    96
        fkwargs.update(entityfkwargs)
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    97
    # form values
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    98
    formvalues = {}
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
    99
    if entity is not None: # creation
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   100
        formvalues[rtype] = container.eid
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   101
    if parentexists: # creation / edition
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   102
        mkwargs.update(fkwargs)
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   103
        # use formtype=inlined to avoid viewing the relation edition section
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   104
        form = freg.select('edition', req, **mkwargs)
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   105
    else: # creation of both container and comment entities
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   106
        form = freg.select('composite', req, form_renderer_id='default',
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   107
                            **mkwargs)
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   108
        form.add_subform(freg.select('edition', req, entity=container,
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   109
                                      mainform=False, mainentity=True))
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   110
        form.add_subform(freg.select('edition', req, mainform=False, **fkwargs))
b88221afe491 [js utils] backport some generic code from comments cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6497
diff changeset
   111
    return form, formvalues