web/views/editforms.py
author sylvain.thenault@logilab.fr
Wed, 06 May 2009 09:47:50 +0200
branchtls-sprint
changeset 1703 a2b5dfdb4b62
parent 1692 56009f2101fe
child 1710 8c717cc0b353
permissions -rw-r--r--
should prefill cached values for relation in the primary or secondary category
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
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
__docformat__ = "restructuredtext en"
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    10
from copy import copy
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    11
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
from simplejson import dumps
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    14
from logilab.mtconverter import html_escape
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    15
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    16
from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    17
                                specified_etype_implements, yes)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
from cubicweb.utils import make_uid
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 1091
diff changeset
    19
from cubicweb.view import EntityView
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
from cubicweb.common import tags
1685
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
    21
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, eid_param
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents: 1459
diff changeset
    22
from cubicweb.web.form import CompositeForm, EntityFieldsForm, FormViewMixIn
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    23
from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    24
from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    25
                                        EntityCompositeFormRenderer,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    26
                                        EntityInlinedFormRenderer)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents: 1459
diff changeset
    27
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    28
_ = unicode
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);" % (
c85f4d8eff8b cleanup
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
    41
        nodeid, html_escape(dumps(eid)))
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
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
    def call(self):
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) ?'))
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    62
        form = CompositeForm(req, domid='deleteconf', copy_nav_params=True,
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    63
                             action=self.build_url('edit'), onsubmit=None,
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    64
                             form_buttons=[Button(stdmsgs.YES, cwaction='delete'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    65
                                           Button(stdmsgs.NO, cwaction='cancel')])
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    66
        done = set()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
        w(u'<ul>\n')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    68
        for entity in self.rset.entities():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    69
            if entity.eid in done:
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    70
                continue
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    71
            done.add(entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    72
            subform = EntityFieldsForm(req, entity=entity, set_error_url=False)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
            form.form_add_subform(subform)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
            # 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
    75
            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
    76
                                      href=entity.absolute_url()))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    77
        w(u'</ul>\n')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
        w(form.form_render())
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
    81
class ClickAndEditFormView(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    82
    """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
    83
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    84
    (double-click on the field to see an appropriate edition widget)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    85
    """
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
    id = 'reledit'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
    __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
    88
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
    # 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
    90
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    91
    onsubmit = ("return inlineValidateForm('%(divid)s-form', '%(rtype)s', "
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    92
                "'%(eid)s', '%(divid)s', %(reload)s);")
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    93
    ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
    94
1579
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
    95
    def cell_call(self, row, col, rtype=None, role='subject', reload=False,
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
    96
                  vid='autolimited'):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    97
        """display field to edit entity's `rtype` relation on double-click"""
1579
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
    98
        rschema = self.schema.rschema(rtype)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
        entity = self.entity(row, col)
1579
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   100
        if rschema.is_final():
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   101
            if getattr(entity, rtype) is None:
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   102
                value = self.req._('not specified')
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   103
            else:
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   104
                value = entity.printable_value(rtype)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
        else:
1579
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   106
            rset = entity.related(rtype, role)
4eea314694e2 apply on non final relation
sylvain.thenault@logilab.fr
parents: 1576
diff changeset
   107
            value = self.view(vid, rset, 'null')
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        if not entity.has_perm('update'):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
            self.w(value)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
            return
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
        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
   112
        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
   113
        event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype,
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   114
                      'reload' : dumps(reload)}
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
   115
        buttons = [SubmitButton(stdmsgs.BUTTON_OK),
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   116
                   Button(stdmsgs.BUTTON_CANCEL,
1576
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   117
                          onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % (
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   118
                              eid, rtype, divid))]
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   119
        form = self.vreg.select_object('forms', 'edition', self.req, self.rset,
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   120
                                       row=row, col=col, form_buttons=buttons,
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   121
                                       domid='%s-form' % divid, action='#',
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   122
                                       cssstyle='display: none',
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
   123
                                       onsubmit=self.onsubmit % event_data)
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
   124
        form.form_add_hidden(u'__maineid', entity.eid)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
        renderer = FormRenderer(display_label=False, display_help=False,
1576
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   126
                                display_fields=[(rtype, role)],
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1559
diff changeset
   127
                                button_bar_class='buttonbar',
1366
fd5bd1a63c3f no progress div in reledit, else we get duplicated ids
sylvain.thenault@logilab.fr
parents: 1324
diff changeset
   128
                                display_progress_div=False)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
        self.w(tags.div(value, klass='editableField', id=divid,
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
   130
                        ondblclick=self.ondblclick % event_data))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   131
        self.w(form.form_render(renderer=renderer))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   132
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
   134
class EditionFormView(FormViewMixIn, EntityView):
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   135
    """display primary entity edition form"""
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
    id = 'edition'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   137
    # add yes() so it takes precedence over deprecated views in baseforms,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   138
    # though not baseforms based customized view
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   139
    __select__ = one_line_rset() & non_final_entity() & yes()
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
    title = _('edition')
1324
a82f83188695 ease renderer overriding
sylvain.thenault@logilab.fr
parents: 1322
diff changeset
   142
    renderer = EntityFormRenderer()
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   143
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
    def cell_call(self, row, col, **kwargs):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
        entity = self.complete_entity(row, col)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   146
        self.render_form(entity)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   147
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   148
    def render_form(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   149
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   150
        self.form_title(entity)
1286
cb68c8af3858 use entity's rset and row/col info
sylvain.thenault@logilab.fr
parents: 1285
diff changeset
   151
        form = self.vreg.select_object('forms', 'edition', self.req, entity.rset,
cb68c8af3858 use entity's rset and row/col info
sylvain.thenault@logilab.fr
parents: 1285
diff changeset
   152
                                       row=entity.row, col=entity.col, entity=entity,
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   153
                                       submitmsg=self.submited_message())
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   154
        self.init_form(form, entity)
1324
a82f83188695 ease renderer overriding
sylvain.thenault@logilab.fr
parents: 1322
diff changeset
   155
        self.w(form.form_render(renderer=self.renderer, formvid=u'edition'))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   157
    def init_form(self, form, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   158
        """customize your form before rendering here"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   159
        form.form_add_hidden(u'__maineid', entity.eid)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   160
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   161
    def form_title(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   162
        """the form view title"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   163
        ptitle = self.req._(self.title)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   164
        self.w(u'<div class="formTitle"><span>%s %s</span></div>' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   165
            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
   166
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   167
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   168
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   169
        return self.req._('entity edited')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   170
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   171
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   172
class CreationFormView(EditionFormView):
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   173
    """display primary entity creation form"""
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   174
    id = 'creation'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   175
    __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
   176
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   177
    title = _('creation')
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   178
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   179
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   180
        """creation view for an entity"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   181
        etype = kwargs.pop('etype', self.req.form.get('etype'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   182
        try:
1435
6cd6172718bb allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents: 1423
diff changeset
   183
            entity = self.vreg.etype_class(etype)(self.req)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   184
        except:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   185
            self.w(self.req._('no such entity type %s') % etype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   186
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   187
            self.initialize_varmaker()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   188
            entity.eid = self.varmaker.next()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   189
            self.render_form(entity)
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   190
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   191
    def form_title(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   192
        """the form view title"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   193
        if '__linkto' in self.req.form:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   194
            if isinstance(self.req.form['__linkto'], list):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   195
                # XXX which one should be considered (case: add a ticket to a
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   196
                # version in jpl)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   197
                rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   198
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   199
                rtype, linkto_eid, role = self.req.form['__linkto'].split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   200
            linkto_rset = self.req.eid_rset(linkto_eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   201
            linkto_type = linkto_rset.description[0][0]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   202
            if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   203
                title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   204
                    entity.e_schema, entity.e_schema, rtype, linkto_type))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   205
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   206
                title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   207
                    entity.e_schema, linkto_type, rtype, entity.e_schema))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   208
            msg = title % {'linkto' : self.view('incontext', linkto_rset)}
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   209
            self.w(u'<div class="formTitle notransform"><span>%s</span></div>' % msg)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   210
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   211
            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
   212
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   213
    def url(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   214
        """return the url associated with this view"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   215
        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
   216
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   217
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   218
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   219
        return self.req._('entity created')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   220
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   221
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   222
class CopyFormView(EditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   223
    """display primary entity creation form initialized with values from another
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   224
    entity
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   225
    """
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   226
    id = 'copy'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   227
    def render_form(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   228
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   229
        # 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
   230
        # request's cache.
1685
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   231
        entity.complete()
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   232
        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
   233
        self.copying = entity
1685
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   234
        self.initialize_varmaker()
3c59ae0e6548 fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1629
diff changeset
   235
        self.newentity.eid = self.varmaker.next()
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   236
        self.w(u'<script type="text/javascript">updateMessage("%s");</script>\n'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   237
               % 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
   238
        super(CopyFormView, self).render_form(self.newentity)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   239
        del self.newentity
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 init_form(self, form, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   242
        """customize your form before rendering here"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   243
        super(CopyFormView, self).init_form(form, entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   244
        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
   245
            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
   246
                                 self.copying.eid)
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   247
        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
   248
                                                           'add'):
a2b5dfdb4b62 should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents: 1692
diff changeset
   249
            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
   250
                # 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
   251
                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
   252
                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
   253
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   254
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   255
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   256
        return self.req._('entity copied')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   257
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
class TableEditForm(CompositeForm):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   260
    id = 'muledit'
1692
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   261
    domid = 'entityForm'
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   262
    onsubmit = "return validateForm('%s', null);" % domid
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   263
    form_buttons = [SubmitButton(_('validate modifications on selected items')),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   264
                    ResetButton(_('revert changes'))]
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   265
1692
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   266
    def __init__(self, req, rset, **kwargs):
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   267
        kwargs.setdefault('__redirectrql', rset.printable_rql())
56009f2101fe fix multiple edit
sylvain.thenault@logilab.fr
parents: 1685
diff changeset
   268
        super(TableEditForm, self).__init__(req, rset, **kwargs)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   269
        for row in xrange(len(self.rset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   270
            form = self.vreg.select_object('forms', 'edition', self.req, self.rset,
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   271
                                           row=row, attrcategories=('primary',),
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   272
                                           set_error_url=False)
1324
a82f83188695 ease renderer overriding
sylvain.thenault@logilab.fr
parents: 1322
diff changeset
   273
            # XXX rely on the EntityCompositeFormRenderer to put the eid input
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   274
            form.remove_field(form.field_by_name('eid'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   275
            self.form_add_subform(form)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   276
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   277
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
   278
class TableEditFormView(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   279
    id = 'muledit'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   280
    __select__ = EntityView.__select__ & yes()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   281
    title = _('multiple edit')
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   282
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   283
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   284
        """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
   285
        should be the eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   286
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   287
        #self.form_title(entity)
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   288
        form = self.vreg.select_object('forms', self.id, self.req, self.rset)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   289
        self.w(form.form_render(renderer=EntityCompositeFormRenderer()))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   290
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   291
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1313
diff changeset
   292
class InlineEntityEditionFormView(FormViewMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   293
    id = 'inline-edition'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   294
    __select__ = non_final_entity() & match_kwargs('peid', 'rtype')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   295
    removejs = "removeInlinedEntity('%s', '%s', '%s')"
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 call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   298
        """redefine default call() method to avoid automatic
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   299
        insertions of <div class="section"> between each row of
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   300
        the resultset
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
        rset = self.rset
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   303
        for i in xrange(len(rset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   304
            self.wview(self.id, rset, row=i, **kwargs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   305
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   306
    def cell_call(self, row, col, peid, rtype, role='subject', **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   307
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   308
        :param peid: the parent entity's eid hosting the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   309
        :param rtype: the relation bridging `etype` and `peid`
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   310
        :param role: the role played by the `peid` in the relation
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   311
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   312
        entity = self.entity(row, col)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   313
        divonclick = "restoreInlinedEntity('%s', '%s', '%s')" % (peid, rtype,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   314
                                                                 entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   315
        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
   316
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   317
    def render_form(self, entity, peid, rtype, role, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   318
        """fetch and render the form"""
1297
174ebf2a1459 should give None as rset
sylvain.thenault@logilab.fr
parents: 1296
diff changeset
   319
        form = self.vreg.select_object('forms', 'edition', self.req, None,
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   320
                                       entity=entity, set_error_url=False)
1396
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   321
        self.add_hiddens(form, entity, peid, rtype, role)
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   322
        divid = '%s-%s-%s' % (peid, rtype, entity.eid)
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   323
        title = self.schema.rschema(rtype).display_name(self.req, role)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   324
        removejs = self.removejs % (peid, rtype,entity.eid)
1396
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   325
        self.w(form.form_render(renderer=EntityInlinedFormRenderer(), divid=divid,
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   326
                                title=title, removejs=removejs,**kwargs))
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   327
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   328
    def add_hiddens(self, form, entity, peid, rtype, role):
daaebf6b9922 refactor to ease overriding
sylvain.thenault@logilab.fr
parents: 1366
diff changeset
   329
        # to ease overriding (see cubes.vcsfile.views.forms for instance)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   330
        if self.keep_entity(entity, peid, rtype):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   331
            if entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   332
                rval = entity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   333
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   334
                rval = INTERNAL_FIELD_VALUE
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   335
            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
   336
        form.form_add_hidden(name='%s:%s' % (rtype, peid), value=entity.eid,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   337
                             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
   338
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   339
    def keep_entity(self, entity, peid, rtype):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   340
        if not entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   341
            return True
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   342
        # are we regenerating form because of a validation error ?
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   343
        erroneous_post = self.req.data.get('formvalues')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   344
        if erroneous_post:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   345
            cdvalues = self.req.list_form_param('%s:%s' % (rtype, peid),
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   346
                                                erroneous_post)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   347
            if unicode(entity.eid) not in cdvalues:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   348
                return False
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   349
        return True
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   350
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   351
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   352
class InlineEntityCreationFormView(InlineEntityEditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   353
    id = 'inline-creation'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   354
    __select__ = (match_kwargs('peid', 'rtype')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   355
                  & specified_etype_implements('Any'))
1423
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   356
    removejs = "removeInlineForm('%s', '%s', '%s')"
39320a58faac missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents: 1396
diff changeset
   357
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   358
    def call(self, etype, peid, rtype, role='subject', **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   359
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   360
        :param etype: the entity type being created in the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   361
        :param peid: the parent entity's eid hosting the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   362
        :param rtype: the relation bridging `etype` and `peid`
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   363
        :param role: the role played by the `peid` in the relation
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   364
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   365
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   366
            entity = self.vreg.etype_class(etype)(self.req, None, None)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   367
        except:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   368
            self.w(self.req._('no such entity type %s') % etype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   369
            return
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   370
        self.initialize_varmaker()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   371
        entity.eid = self.varmaker.next()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   372
        self.render_form(entity, peid, rtype, role)