web/views/editforms.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 17 Jul 2009 16:50:50 +0200
changeset 2389 c399bd6a9c38
parent 2381 caad2367d940
child 2390 c0d09a4ca3dc
permissions -rw-r--r--
[reledit] separate value from landing zone
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
"""Set of HTML automatic forms to create, delete, copy or edit a single entity
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
or a list of entities of the same type
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
:organization: Logilab
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1965
diff changeset
     5
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1965
diff changeset
     7
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
__docformat__ = "restructuredtext en"
1965
34f57246330d cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1852
diff changeset
    10
_ = unicode
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    12
from copy import copy
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    13
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
from simplejson import dumps
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
    16
from logilab.mtconverter import xml_escape
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    17
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    18
from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    19
                                specified_etype_implements, yes)
2361
8f00836580f1 [reledit] do NOT propose to edit composite-related entities (unlinking would destroy the other side)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2345
diff changeset
    20
from cubicweb.utils import make_uid, compute_cardinality, get_schema_property
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 1091
diff changeset
    21
from cubicweb.view import EntityView
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
from cubicweb.common import tags
2370
1e8ce077b62a consider primaryview_display_ctrl in reledit, XXX should split into reledit / autoreledit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2365
diff changeset
    23
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, eid_param, uicfg
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    24
from cubicweb.web.form import FormViewMixIn
2328
206735882b8e [reledit] use guess_field, thus fixing select widget lacking a multiple attr. in case of * cardinality
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2327
diff changeset
    25
from cubicweb.web.formfields import guess_field
206735882b8e [reledit] use guess_field, thus fixing select widget lacking a multiple attr. in case of * cardinality
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2327
diff changeset
    26
from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    27
from cubicweb.web.views import forms
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents: 1459
diff changeset
    28
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    30
def relation_id(eid, rtype, role, reid):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    31
    """return an identifier for a relation between two entities"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    32
    if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    33
        return u'%s:%s:%s' % (eid, rtype, reid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    34
    return u'%s:%s:%s' % (reid, rtype, eid)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
    35
1528
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1498
diff changeset
    36
def toggleable_relation_link(eid, nodeid, label='x'):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    37
    """return javascript snippet to delete/undelete a relation between two
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    38
    entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    39
    """
1320
c85f4d8eff8b cleanup
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
    40
    js = u"javascript: togglePendingDelete('%s', %s);" % (
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
    41
        nodeid, xml_escape(dumps(eid)))
1320
c85f4d8eff8b cleanup
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
    42
    return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (
c85f4d8eff8b cleanup
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
    43
        js, nodeid, label)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
    46
class DeleteConfForm(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    47
    """form used to confirm deletion of some entities"""
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
    id = 'deleteconf'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
    title = _('delete')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
    # don't use navigation, all entities asked to be deleted should be displayed
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
    # else we will only delete the displayed page
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
    need_navigation = False
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
    53
1852
f04da596da6c give back onsubmit customizability (oops)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1847
diff changeset
    54
    def call(self, onsubmit=None):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
        """ask for confirmation before real deletion"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    56
        req, w = self.req, self.w
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
        _ = req._
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
        w(u'<script type="text/javascript">updateMessage(\'%s\');</script>\n'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
          % _('this action is not reversible!'))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    60
        # XXX above message should have style of a warning
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
        w(u'<h4>%s</h4>\n' % _('Do you want to delete the following element(s) ?'))
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    62
        form = self.vreg.select('forms', 'composite', req, domid='deleteconf',
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    63
                                copy_nav_params=True,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    64
                                action=self.build_url('edit'), onsubmit=onsubmit,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    65
                                form_buttons=[Button(stdmsgs.YES, cwaction='delete'),
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    66
                                              Button(stdmsgs.NO, cwaction='cancel')])
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
        done = set()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
        w(u'<ul>\n')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    69
        for entity in self.rset.entities():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    70
            if entity.eid in done:
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
                continue
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    72
            done.add(entity.eid)
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    73
            subform = self.vreg.select('forms', 'base', req, entity=entity,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
    74
                                       mainform=False)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
            form.form_add_subform(subform)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
            # don't use outofcontext view or any other that may contain inline edition form
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    77
            w(u'<li>%s</li>' % tags.a(entity.view('textoutofcontext'),
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
                                      href=entity.absolute_url()))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
        w(u'</ul>\n')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
        w(form.form_render())
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
    83
class ClickAndEditFormView(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    84
    """form used to permit ajax edition of an attribute of an entity in a view
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
    85
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    86
    (double-click on the field to see an appropriate edition widget)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    87
    """
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
    id = 'reledit'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
    __select__ = non_final_entity() & match_kwargs('rtype')
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
    90
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
    # FIXME editableField class could be toggleable from userprefs
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
    92
2365
9f5e911eab07 [reledit] allow composite edition if the composite is object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2361
diff changeset
    93
    _onclick = u"showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
9f5e911eab07 [reledit] allow composite edition if the composite is object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2361
diff changeset
    94
    _defaultlandingzone = u'<img title="%(msg)s" src="data/file.gif" alt="%(msg)s"/>'
2335
b78249be8a4b [reledit] fix reload type (was a string), use simple clicks instead of doubles
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2334
diff changeset
    95
    _landingzonemsg = _('click to edit this field')
2332
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
    96
    # default relation vids according to cardinality
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
    97
    _one_rvid = 'incontext'
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
    98
    _many_rvid = 'csv'
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
    99
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   100
    def _compute_best_vid(self, entity, rtype, role):
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   101
        if compute_cardinality(entity.e_schema,
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   102
                               entity.schema.rschema(rtype),
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   103
                               role) in '+*':
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   104
            return self._many_rvid
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   105
        return self._one_rvid
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   106
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   107
    def _build_landing_zone(self, lzone):
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   108
        return lzone or self._defaultlandingzone % {'msg' : self.req._(self._landingzonemsg)}
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   109
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   110
    def _build_renderer(self, entity, rtype, role):
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   111
        return self.vreg.select_object('formrenderers', 'base', self.req,
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   112
                                       entity=entity,
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   113
                                       display_label=False, display_help=False,
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   114
                                       display_fields=[(rtype, role)],
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   115
                                       table_class='', button_bar_class='buttonbar',
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   116
                                       display_progress_div=False)
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   117
2329
8b5a1af6dc35 [reledit] small cleanup and add escape param to control xml_escapability of the value, also rename vid arg which clashes with entity.view(...) args
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2328
diff changeset
   118
    def cell_call(self, row, col, rtype=None, role='subject',
2336
a15d3f54f0f2 [reledit] left in by accident
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2335
diff changeset
   119
                  reload=False,      # controls reloading the whole page after change
2332
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   120
                  rvid=None,         # vid to be applied to other side of rtype
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   121
                  default=None,      # default value
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   122
                  landing_zone=None  # prepend value with a separate html element to click onto
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   123
                                     # (esp. needed when values are links)
2329
8b5a1af6dc35 [reledit] small cleanup and add escape param to control xml_escapability of the value, also rename vid arg which clashes with entity.view(...) args
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2328
diff changeset
   124
                  ):
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   125
        """display field to edit entity's `rtype` relation on click"""
2329
8b5a1af6dc35 [reledit] small cleanup and add escape param to control xml_escapability of the value, also rename vid arg which clashes with entity.view(...) args
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2328
diff changeset
   126
        assert rtype
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   127
        assert role in ('subject', 'object')
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   128
        if default is None:
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   129
            default = xml_escape(self.req._('<no value>'))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
        entity = self.entity(row, col)
2332
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   131
        rschema = entity.schema.rschema(rtype)
2333
1de22d3e985b [reledit] landing zone useful only for relations; cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2332
diff changeset
   132
        # compute value, checking perms, build form
1579
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   133
        if rschema.is_final():
2331
4c02a761d879 [reledit] do not strip values (we should not make judgements about field contents ...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2330
diff changeset
   134
            value = entity.printable_value(rtype) or default
2224
52041b014949 fix #344046 and #344322: check relation permission, set to default when edition allowed and no value (for final *and* non final relations)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   135
            if not entity.has_perm('update'):
52041b014949 fix #344046 and #344322: check relation permission, set to default when edition allowed and no value (for final *and* non final relations)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   136
                self.w(value)
52041b014949 fix #344046 and #344322: check relation permission, set to default when edition allowed and no value (for final *and* non final relations)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   137
                return
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   138
            self._attribute_form(entity, value, rtype, role, reload,
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   139
                                 row, col, default, landing_zone)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
        else:
2380
5d980ba57632 [reledit] restore primaryview_display_ctrl usage by default in reledit (including bugfix over last implementation)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2376
diff changeset
   141
            dispctrl = uicfg.primaryview_display_ctrl.etype_get(entity.e_schema,
5d980ba57632 [reledit] restore primaryview_display_ctrl usage by default in reledit (including bugfix over last implementation)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2376
diff changeset
   142
                                                                rtype, role)
5d980ba57632 [reledit] restore primaryview_display_ctrl usage by default in reledit (including bugfix over last implementation)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2376
diff changeset
   143
            vid = dispctrl.get('vid', 'reledit')
5d980ba57632 [reledit] restore primaryview_display_ctrl usage by default in reledit (including bugfix over last implementation)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2376
diff changeset
   144
            if vid != 'reledit': # reledit explicitly disabled
5d980ba57632 [reledit] restore primaryview_display_ctrl usage by default in reledit (including bugfix over last implementation)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2376
diff changeset
   145
                self.wview(vid, entity.related(rtype, role))
5d980ba57632 [reledit] restore primaryview_display_ctrl usage by default in reledit (including bugfix over last implementation)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2376
diff changeset
   146
                return
2332
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   147
            if rvid is None:
b04d80f19075 [reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2331
diff changeset
   148
                rvid = self._compute_best_vid(entity, rtype, role)
1579
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   149
            rset = entity.related(rtype, role)
2331
4c02a761d879 [reledit] do not strip values (we should not make judgements about field contents ...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2330
diff changeset
   150
            candidate = self.view(rvid, rset, 'null')
2329
8b5a1af6dc35 [reledit] small cleanup and add escape param to control xml_escapability of the value, also rename vid arg which clashes with entity.view(...) args
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2328
diff changeset
   151
            value = candidate or default
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   152
            if role == 'subject' and not rschema.has_perm(self.req, 'add',
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   153
                                                          fromeid=entity.eid):
2333
1de22d3e985b [reledit] landing zone useful only for relations; cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2332
diff changeset
   154
                return self.w(value)
1de22d3e985b [reledit] landing zone useful only for relations; cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2332
diff changeset
   155
            elif role == 'object' and not rschema.has_perm(self.req, 'add',
1de22d3e985b [reledit] landing zone useful only for relations; cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2332
diff changeset
   156
                                                           toeid=entity.eid):
1de22d3e985b [reledit] landing zone useful only for relations; cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2332
diff changeset
   157
                return self.w(value)
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   158
            elif get_schema_property(entity.e_schema, rschema,
2365
9f5e911eab07 [reledit] allow composite edition if the composite is object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2361
diff changeset
   159
                                     role, 'composite') == role:
2372
cca71dcbeb70 a warning for attemps to reledit the wrong side of a composite
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2371
diff changeset
   160
                self.warning('reledit cannot be applied : (... %s %s [composite])'
cca71dcbeb70 a warning for attemps to reledit the wrong side of a composite
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2371
diff changeset
   161
                             % (rtype, entity.e_schema))
2361
8f00836580f1 [reledit] do NOT propose to edit composite-related entities (unlinking would destroy the other side)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2345
diff changeset
   162
                return self.w(value)
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   163
            self._relation_form(entity, value, rtype, role, reload, row, col,
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   164
                                rvid, default, landing_zone)
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1750
diff changeset
   165
2365
9f5e911eab07 [reledit] allow composite edition if the composite is object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2361
diff changeset
   166
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   167
    def _relation_form(self, entity, value, rtype, role, row, col, reload, rvid, default, lzone):
2365
9f5e911eab07 [reledit] allow composite edition if the composite is object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2361
diff changeset
   168
        lzone = self._build_landing_zone(lzone)
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1750
diff changeset
   169
        divid = 'd%s' % make_uid('%s-%s' % (rtype, entity.eid))
2330
8c70ca715fe9 [reledit] have a link-free landing zone for mouse-clicks (closes #343544)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2329
diff changeset
   170
        event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : rvid,
2334
464c896bee65 [reledit] reload support for relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2333
diff changeset
   171
                      'reload' : reload, 'default' : default, 'role' : role,
2345
16e3d0e47ee6 [reledit] there is nothing to escape, also cleanup lzone for attributes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2336
diff changeset
   172
                      'lzone' : lzone}
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   173
        onsubmit = ("return inlineValidateRelationForm('%(rtype)s', '%(role)s', '%(eid)s', "
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   174
                    "'%(divid)s', %(reload)s, '%(vid)s', '%(default)s', '%(lzone)s');"
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   175
                    % event_data)
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   176
        cancelclick = "hideInlineEdit(%s,\'%s\',\'%s\')" % (
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   177
            entity.eid, rtype, divid)
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   178
        form = self.vreg.select_object('forms', 'base', self.req, entity=entity,
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   179
                                       domid='%s-form' % divid, cssstyle='display: none',
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   180
                                       onsubmit=onsubmit, action='#',
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   181
                                       form_buttons=[SubmitButton(),
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   182
                                                     Button(stdmsgs.BUTTON_CANCEL,
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   183
                                                            onclick=cancelclick)])
2328
206735882b8e [reledit] use guess_field, thus fixing select widget lacking a multiple attr. in case of * cardinality
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2327
diff changeset
   184
        field = guess_field(entity.e_schema, entity.schema.rschema(rtype), role)
206735882b8e [reledit] use guess_field, thus fixing select widget lacking a multiple attr. in case of * cardinality
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2327
diff changeset
   185
        form.append_field(field)
2389
c399bd6a9c38 [reledit] separate value from landing zone
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2381
diff changeset
   186
        self.w(u'<div id="%s-reledit" class="field">%s&nbsp;' % (divid, value))
c399bd6a9c38 [reledit] separate value from landing zone
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2381
diff changeset
   187
        self.w(tags.div(lzone, klass='editableField', id=divid,
2335
b78249be8a4b [reledit] fix reload type (was a string), use simple clicks instead of doubles
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2334
diff changeset
   188
                        onclick=self._onclick % event_data))
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   189
        renderer = self._build_renderer(entity, rtype, role)
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   190
        self.w(form.form_render(renderer=renderer))
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   191
        self.w(u'</div>')
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1750
diff changeset
   192
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   193
    def _attribute_form(self, entity, value, rtype, role, reload, row, col, default, lzone):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   194
        eid = entity.eid
1559
c4d4851bd18b [editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1528
diff changeset
   195
        divid = 'd%s' % make_uid('%s-%s' % (rtype, eid))
1576
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   196
        event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype,
2330
8c70ca715fe9 [reledit] have a link-free landing zone for mouse-clicks (closes #343544)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2329
diff changeset
   197
                      'reload' : dumps(reload), 'default' : default, 'lzone' : lzone}
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   198
        onsubmit = ("return inlineValidateAttributeForm('%(rtype)s', '%(eid)s', '%(divid)s', "
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   199
                    "%(reload)s, '%(default)s', '%(lzone)s');")
1559
c4d4851bd18b [editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1528
diff changeset
   200
        buttons = [SubmitButton(stdmsgs.BUTTON_OK),
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   201
                   Button(stdmsgs.BUTTON_CANCEL,
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   202
                          onclick="hideInlineEdit(%s,\'%s\',\'%s\')" % (
1576
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   203
                              eid, rtype, divid))]
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   204
        form = self.vreg.select_object('forms', 'edition', self.req, self.rset,
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   205
                                       row=row, col=col, form_buttons=buttons,
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   206
                                       domid='%s-form' % divid, action='#',
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   207
                                       cssstyle='display: none',
2327
2aa28ac13d64 [reledit] onsubmit event is not common to both cases, move from class
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2312
diff changeset
   208
                                       onsubmit=onsubmit % event_data)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   209
        self.w(tags.div(value, klass='editableField', id=divid,
2335
b78249be8a4b [reledit] fix reload type (was a string), use simple clicks instead of doubles
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2334
diff changeset
   210
                        onclick=self._onclick % event_data))
2371
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   211
        renderer = self._build_renderer(entity, rtype, role)
76bf522c27be [reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2365
diff changeset
   212
        self.w(form.form_render(renderer=renderer))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   213
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   214
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
   215
class EditionFormView(FormViewMixIn, EntityView):
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   216
    """display primary entity edition form"""
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   217
    id = 'edition'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   218
    # add yes() so it takes precedence over deprecated views in baseforms,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   219
    # though not baseforms based customized view
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   220
    __select__ = one_line_rset() & non_final_entity() & yes()
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   221
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   222
    title = _('edition')
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   223
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   224
    def cell_call(self, row, col, **kwargs):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   225
        entity = self.complete_entity(row, col)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   226
        self.render_form(entity)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   227
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   228
    def render_form(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   229
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   230
        self.form_title(entity)
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   231
        form = self.vreg.select('forms', 'edition', self.req, rset=entity.rset,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   232
                                row=entity.row, col=entity.col, entity=entity,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   233
                                submitmsg=self.submited_message())
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   234
        self.init_form(form, entity)
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   235
        self.w(form.form_render(formvid=u'edition'))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   236
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   237
    def init_form(self, form, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   238
        """customize your form before rendering here"""
2049
b9baedffeb8b set __maineid in EntityFieldsForm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2048
diff changeset
   239
        pass
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   240
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   241
    def form_title(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   242
        """the form view title"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   243
        ptitle = self.req._(self.title)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   244
        self.w(u'<div class="formTitle"><span>%s %s</span></div>' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   245
            entity.dc_type(), ptitle and '(%s)' % ptitle))
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   246
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   247
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   248
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   249
        return self.req._('entity edited')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   250
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   251
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   252
class CreationFormView(EditionFormView):
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   253
    """display primary entity creation form"""
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   254
    id = 'creation'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   255
    __select__ = specified_etype_implements('Any') & yes()
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   256
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   257
    title = _('creation')
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   258
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   259
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   260
        """creation view for an entity"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   261
        etype = kwargs.pop('etype', self.req.form.get('etype'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   262
        try:
1435
6cd6172718bb allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents: 1423
diff changeset
   263
            entity = self.vreg.etype_class(etype)(self.req)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   264
        except:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   265
            self.w(self.req._('no such entity type %s') % etype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   266
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   267
            self.initialize_varmaker()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   268
            entity.eid = self.varmaker.next()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   269
            self.render_form(entity)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   270
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   271
    def form_title(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   272
        """the form view title"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   273
        if '__linkto' in self.req.form:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   274
            if isinstance(self.req.form['__linkto'], list):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   275
                # XXX which one should be considered (case: add a ticket to a
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   276
                # version in jpl)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   277
                rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   278
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   279
                rtype, linkto_eid, role = self.req.form['__linkto'].split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   280
            linkto_rset = self.req.eid_rset(linkto_eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   281
            linkto_type = linkto_rset.description[0][0]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   282
            if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   283
                title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   284
                    entity.e_schema, entity.e_schema, rtype, linkto_type))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   285
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   286
                title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   287
                    entity.e_schema, linkto_type, rtype, entity.e_schema))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   288
            msg = title % {'linkto' : self.view('incontext', linkto_rset)}
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   289
            self.w(u'<div class="formTitle notransform"><span>%s</span></div>' % msg)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   290
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   291
            super(CreationFormView, self).form_title(entity)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   292
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   293
    def url(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   294
        """return the url associated with this view"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   295
        return self.create_url(self.req.form.get('etype'))
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   296
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   297
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   298
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   299
        return self.req._('entity created')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   300
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   301
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   302
class CopyFormView(EditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   303
    """display primary entity creation form initialized with values from another
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   304
    entity
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   305
    """
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   306
    id = 'copy'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   307
    def render_form(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   308
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   309
        # make a copy of entity to avoid altering the entity in the
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   310
        # request's cache.
1685
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   311
        entity.complete()
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   312
        self.newentity = copy(entity)
1703
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   313
        self.copying = entity
1685
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   314
        self.initialize_varmaker()
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   315
        self.newentity.eid = self.varmaker.next()
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   316
        self.w(u'<script type="text/javascript">updateMessage("%s");</script>\n'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   317
               % self.req._('Please note that this is only a shallow copy'))
1685
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   318
        super(CopyFormView, self).render_form(self.newentity)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   319
        del self.newentity
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   320
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   321
    def init_form(self, form, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   322
        """customize your form before rendering here"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   323
        super(CopyFormView, self).init_form(form, entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   324
        if entity.eid == self.newentity.eid:
1703
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   325
            form.form_add_hidden(eid_param('__cloned_eid', entity.eid),
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   326
                                 self.copying.eid)
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   327
        for rschema, _, role in form.relations_by_category(form.attrcategories,
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   328
                                                           'add'):
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   329
            if not rschema.is_final():
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   330
                # ensure relation cache is filed
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   331
                rset = self.copying.related(rschema, role)
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   332
                self.newentity.set_related_cache(rschema, role, rset)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   333
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   334
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   335
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   336
        return self.req._('entity copied')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   337
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   338
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   339
class TableEditForm(forms.CompositeForm):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   340
    id = 'muledit'
1692
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   341
    domid = 'entityForm'
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   342
    onsubmit = "return validateForm('%s', null);" % domid
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   343
    form_buttons = [SubmitButton(_('validate modifications on selected items')),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   344
                    ResetButton(_('revert changes'))]
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   345
1692
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   346
    def __init__(self, req, rset, **kwargs):
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   347
        kwargs.setdefault('__redirectrql', rset.printable_rql())
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   348
        super(TableEditForm, self).__init__(req, rset, **kwargs)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   349
        for row in xrange(len(self.rset)):
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   350
            form = self.vreg.select('forms', 'edition', self.req, rset=self.rset,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   351
                                    row=row, attrcategories=('primary',),
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   352
                                    mainform=False)
1324
a82f83188695 ease renderer overriding
sylvain.thenault@logilab.fr
parents: 1322
diff changeset
   353
            # XXX rely on the EntityCompositeFormRenderer to put the eid input
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   354
            form.remove_field(form.field_by_name('eid'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   355
            self.form_add_subform(form)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   356
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   357
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
   358
class TableEditFormView(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   359
    id = 'muledit'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   360
    __select__ = EntityView.__select__ & yes()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   361
    title = _('multiple edit')
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   362
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   363
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   364
        """a view to edit multiple entities of the same type the first column
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   365
        should be the eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   366
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   367
        #self.form_title(entity)
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   368
        form = self.vreg.select('forms', self.id, self.req, rset=self.rset)
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   369
        self.w(form.form_render())
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   370
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   371
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
   372
class InlineEntityEditionFormView(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   373
    id = 'inline-edition'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   374
    __select__ = non_final_entity() & match_kwargs('peid', 'rtype')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   375
    removejs = "removeInlinedEntity('%s', '%s', '%s')"
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   376
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   377
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   378
        """redefine default call() method to avoid automatic
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   379
        insertions of <div class="section"> between each row of
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   380
        the resultset
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   381
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   382
        rset = self.rset
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   383
        for i in xrange(len(rset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   384
            self.wview(self.id, rset, row=i, **kwargs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   385
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   386
    def cell_call(self, row, col, peid, rtype, role='subject', **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   387
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   388
        :param peid: the parent entity's eid hosting the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   389
        :param rtype: the relation bridging `etype` and `peid`
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   390
        :param role: the role played by the `peid` in the relation
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   391
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   392
        entity = self.entity(row, col)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   393
        divonclick = "restoreInlinedEntity('%s', '%s', '%s')" % (peid, rtype,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   394
                                                                 entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   395
        self.render_form(entity, peid, rtype, role, divonclick=divonclick)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   396
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   397
    def render_form(self, entity, peid, rtype, role, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   398
        """fetch and render the form"""
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   399
        form = self.vreg.select('forms', 'edition', self.req, entity=entity,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   400
                                form_renderer_id='inline', mainform=False,
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   401
                                copy_nav_params=False)
1396
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   402
        self.add_hiddens(form, entity, peid, rtype, role)
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   403
        divid = '%s-%s-%s' % (peid, rtype, entity.eid)
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   404
        title = self.schema.rschema(rtype).display_name(self.req, role)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   405
        removejs = self.removejs % (peid, rtype,entity.eid)
2247
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2224
diff changeset
   406
        countkey = '%s_count' % rtype
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2224
diff changeset
   407
        try:
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2224
diff changeset
   408
            self.req.data[countkey] += 1
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2224
diff changeset
   409
        except:
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2224
diff changeset
   410
            self.req.data[countkey] = 1
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   411
        self.w(form.form_render(divid=divid, title=title, removejs=removejs,
2247
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2224
diff changeset
   412
                                counter=self.req.data[countkey], **kwargs))
1396
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   413
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   414
    def add_hiddens(self, form, entity, peid, rtype, role):
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   415
        # to ease overriding (see cubes.vcsfile.views.forms for instance)
1710
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1703
diff changeset
   416
        if self.keep_entity(form, entity, peid, rtype):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   417
            if entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   418
                rval = entity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   419
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   420
                rval = INTERNAL_FIELD_VALUE
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   421
            form.form_add_hidden('edit%s-%s:%s' % (role[0], rtype, peid), rval)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   422
        form.form_add_hidden(name='%s:%s' % (rtype, peid), value=entity.eid,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   423
                             id='rel-%s-%s-%s'  % (peid, rtype, entity.eid))
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   424
1710
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1703
diff changeset
   425
    def keep_entity(self, form, entity, peid, rtype):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   426
        if not entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   427
            return True
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   428
        # are we regenerating form because of a validation error ?
1750
9df5e65c5f79 fix name error
sylvain.thenault@logilab.fr
parents: 1710
diff changeset
   429
        if form.form_previous_values:
1710
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1703
diff changeset
   430
            cdvalues = self.req.list_form_param(eid_param(rtype, peid),
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1703
diff changeset
   431
                                                form.form_previous_values)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   432
            if unicode(entity.eid) not in cdvalues:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   433
                return False
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   434
        return True
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   435
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   436
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   437
class InlineEntityCreationFormView(InlineEntityEditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   438
    id = 'inline-creation'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   439
    __select__ = (match_kwargs('peid', 'rtype')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   440
                  & specified_etype_implements('Any'))
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   441
    removejs = "removeInlineForm('%s', '%s', '%s')"
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   442
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   443
    def call(self, etype, peid, rtype, role='subject', **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   444
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   445
        :param etype: the entity type being created in the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   446
        :param peid: the parent entity's eid hosting the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   447
        :param rtype: the relation bridging `etype` and `peid`
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   448
        :param role: the role played by the `peid` in the relation
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   449
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   450
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   451
            entity = self.vreg.etype_class(etype)(self.req, None, None)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   452
        except:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   453
            self.w(self.req._('no such entity type %s') % etype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   454
            return
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   455
        self.initialize_varmaker()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   456
        entity.eid = self.varmaker.next()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   457
        self.render_form(entity, peid, rtype, role)