web/views/editforms.py
author sylvain.thenault@logilab.fr
Wed, 08 Apr 2009 20:36:11 +0200
branchtls-sprint
changeset 1305 395ef7f2b95b
parent 1304 8975c8e520a9
child 1313 9cff1eee0208
permissions -rw-r--r--
cleanup, remove some unnecessary (sometime buggy) stuff
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
1183
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
    14
from logilab.common.decorators import iclassmethod
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    15
from logilab.mtconverter import html_escape
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    16
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    17
from cubicweb import typed_eid
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)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
from cubicweb.utils import make_uid
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
1285
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
    23
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, formwidgets, uicfg
1276
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
    24
from cubicweb.web.form import (FieldNotFound, CompositeForm, EntityFieldsForm,
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
    25
                               FormMixIn)
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 1091
diff changeset
    26
from cubicweb.web.formfields import guess_field
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    27
from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    28
from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    29
                                        EntityCompositeFormRenderer,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    30
                                        EntityInlinedFormRenderer)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    31
_ = unicode
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    33
def relation_id(eid, rtype, role, reid):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    34
    """return an identifier for a relation between two entities"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    35
    if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    36
        return u'%s:%s:%s' % (eid, rtype, reid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    37
    return u'%s:%s:%s' % (reid, rtype, eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    38
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    39
def toggable_relation_link(eid, nodeid, label='x'):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    40
    """return javascript snippet to delete/undelete a relation between two
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    41
    entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    42
    """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    43
    js = u"javascript: togglePendingDelete('%s', %s);" % (nodeid, html_escape(dumps(eid)))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    44
    return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (js, nodeid, label)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
class DeleteConfForm(EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    48
    """form used to confirm deletion of some entities"""
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
    id = 'deleteconf'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
    title = _('delete')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
    # 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
    52
    # else we will only delete the displayed page
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
    need_navigation = False
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
    
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
    def call(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    56
        """ask for confirmation before real deletion"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
        req, w = self.req, self.w
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
        _ = req._
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
        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
    60
          % _('this action is not reversible!'))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
        # XXX above message should have style of a warning
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
        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
    63
        form = CompositeForm(req, domid='deleteconf', copy_nav_params=True,
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    64
                             action=self.build_url('edit'), onsubmit=None,
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
    65
                             form_buttons=[Button(stdmsgs.YES, cwaction='delete'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
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)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    73
            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
    74
            form.form_add_subform(subform)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
            # 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
    76
            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
    77
                                      href=entity.absolute_url()))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
        w(u'</ul>\n')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
        w(form.form_render())
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
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
class ClickAndEditForm(FormMixIn, EntityView):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    83
    """form used to permit ajax edition of an attribute of an entity in a view
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    84
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    85
    (double-click on the field to see an appropriate edition widget)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    86
    """
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
    id = 'reledit'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
    __select__ = non_final_entity() & match_kwargs('rtype')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    89
    
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
    # FIXME editableField class could be toggleable from userprefs
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    91
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    92
    onsubmit = ("return inlineValidateForm('%(divid)s-form', '%(rtype)s', "
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    93
                "'%(eid)s', '%(divid)s', %(reload)s);")
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    94
    ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
    95
    
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
    def cell_call(self, row, col, rtype=None, role='subject', reload=False):
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"""
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
        entity = self.entity(row, col)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
        if getattr(entity, rtype) is None:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
            value = self.req._('not specified')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        else:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
            value = entity.printable_value(rtype)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
        if not entity.has_perm('update'):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
            self.w(value)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
            return
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
        self.req.add_js( ('cubicweb.ajax.js',) )
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
        eid = entity.eid
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        edit_key = make_uid('%s-%s' % (rtype, eid))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
        divid = 'd%s' % edit_key
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
        reload = dumps(reload)
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   111
        buttons = [SubmitButton(stdmsgs.BUTTON_OK, cwaction='apply'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   112
                   Button(stdmsgs.BUTTON_CANCEL,
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   113
                          onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % (eid, rtype, divid))]
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   114
        form = self.vreg.select_object('forms', 'edition', self.req, self.rset,
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   115
                                       row=row, col=col, form_buttons=buttons,
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   116
                                       domid='%s-form' % divid, action='#',
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   117
                                       cssstyle='display: none',
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   118
                                       onsubmit=self.onsubmit % locals())
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
        renderer = FormRenderer(display_label=False, display_help=False,
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
                                display_fields=(rtype,), button_bar_class='buttonbar')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   121
        self.w(tags.div(value, klass='editableField', id=divid,
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   122
                        ondblclick=self.ondblclick % locals()))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   123
        self.w(form.form_render(renderer=renderer))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   126
class AutomaticEntityForm(EntityFieldsForm):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   127
    """base automatic form to edit any entity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   128
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   129
    Designed to be flly generated from schema but highly configurable through:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   130
    * rtags (rcategories, rwidgets, inlined, rpermissions)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   131
    * various standard form parameters
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   132
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   133
    You can also easily customise it by adding/removing fields in
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   134
    AutomaticEntityForm instances.
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   135
    """
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
    
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
    needs_js = EntityFieldsForm.needs_js + ('cubicweb.ajax.js',)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   139
    cwtarget = 'eformframe'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   140
    cssclass = 'entityForm'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   141
    copy_nav_params = True
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   142
    form_buttons = [SubmitButton(stdmsgs.BUTTON_OK),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   143
                    Button(stdmsgs.BUTTON_APPLY, cwaction='apply'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   144
                    Button(stdmsgs.BUTTON_CANCEL, cwaction='cancel')]    
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   145
    attrcategories = ('primary', 'secondary')
1285
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   146
    # class attributes below are actually stored in the uicfg module since we
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   147
    # don't want them to be reloaded
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   148
    rcategories = uicfg.rcategories
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   149
    rwidgets = uicfg.rwidgets
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   150
    rinlined = uicfg.rinlined
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   151
    rpermissions_overrides = uicfg.rpermissions_overrides
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   152
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   153
    @classmethod
1285
d5ce82d65c2b for a correct handling of rtags, they should not ever be reloaded and they should be initialized once registration is finished
sylvain.thenault@logilab.fr
parents: 1277
diff changeset
   154
    def vreg_initialization_completed(cls):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   155
        """set default category tags for relations where it's not yet defined in
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   156
        the category relation tags
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   157
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   158
        for eschema in cls.schema.entities():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   159
            for rschema, tschemas, role in eschema.relation_definitions(True):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   160
                for tschema in tschemas:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   161
                    if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   162
                        X, Y = eschema, tschema
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   163
                        card = rschema.rproperty(X, Y, 'cardinality')[0]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   164
                        composed = rschema.rproperty(X, Y, 'composite') == 'object'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   165
                    else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   166
                        X, Y = tschema, eschema
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   167
                        card = rschema.rproperty(X, Y, 'cardinality')[1]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   168
                        composed = rschema.rproperty(X, Y, 'composite') == 'subject'
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   169
                    if not cls.rcategories.rtag(rschema, role, X, Y):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   170
                        if card in '1+':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   171
                            if not rschema.is_final() and composed:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   172
                                category = 'generated'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   173
                            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   174
                                category = 'primary'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   175
                        elif rschema.is_final():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   176
                            category = 'secondary'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   177
                        else: 
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   178
                            category = 'generic'
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   179
                        cls.rcategories.set_rtag(category, rschema, role, X, Y)
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   180
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   181
    @classmethod
1291
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   182
    def erelations_by_category(cls, entity, categories=None, permission=None, rtags=None):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   183
        """return a list of (relation schema, target schemas, role) matching
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   184
        categories and permission
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   185
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   186
        if categories is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   187
            if not isinstance(categories, (list, tuple, set, frozenset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   188
                categories = (categories,)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   189
            if not isinstance(categories, (set, frozenset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   190
                categories = frozenset(categories)
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   191
        eschema  = entity.e_schema
1291
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   192
        if rtags is None:
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   193
            rtags = cls.rcategories
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   194
        permsoverrides = cls.rpermissions_overrides
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   195
        if entity.has_eid():
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   196
            eid = entity.eid
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   197
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   198
            eid = None
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   199
        for rschema, targetschemas, role in eschema.relation_definitions(True):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   200
            # check category first, potentially lower cost than checking
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   201
            # permission which may imply rql queries
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   202
            if categories is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   203
                targetschemas = [tschema for tschema in targetschemas
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   204
                                 if rtags.etype_rtag(eschema, rschema, role, tschema) in categories]
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   205
                if not targetschemas:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   206
                    continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   207
            if permission is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   208
                # tag allowing to hijack the permission machinery when
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   209
                # permission is not verifiable until the entity is actually
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   210
                # created...
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   211
                if eid is None and '%s_on_new' % permission in permsoverrides.etype_rtags(eschema, rschema, role):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   212
                    yield (rschema, targetschemas, role)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   213
                    continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   214
                if rschema.is_final():
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   215
                    if not rschema.has_perm(entity.req, permission, eid):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   216
                        continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   217
                elif role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   218
                    if not ((eid is None and rschema.has_local_role(permission)) or
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   219
                            rschema.has_perm(entity.req, permission, fromeid=eid)):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   220
                        continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   221
                    # on relation with cardinality 1 or ?, we need delete perm as well
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   222
                    # if the relation is already set
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   223
                    if (permission == 'add'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   224
                        and rschema.cardinality(eschema, targetschemas[0], role) in '1?'
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   225
                        and eid and entity.related(rschema.type, role)
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   226
                        and not rschema.has_perm(entity.req, 'delete', fromeid=eid,
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   227
                                                 toeid=entity.related(rschema.type, role)[0][0])):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   228
                        continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   229
                elif role == 'object':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   230
                    if not ((eid is None and rschema.has_local_role(permission)) or
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   231
                            rschema.has_perm(entity.req, permission, toeid=eid)):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   232
                        continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   233
                    # on relation with cardinality 1 or ?, we need delete perm as well
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   234
                    # if the relation is already set
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   235
                    if (permission == 'add'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   236
                        and rschema.cardinality(targetschemas[0], eschema, role) in '1?'
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   237
                        and eid and entity.related(rschema.type, role)
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   238
                        and not rschema.has_perm(entity.req, 'delete', toeid=eid,
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
   239
                                                 fromeid=entity.related(rschema.type, role)[0][0])):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   240
                        continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   241
            yield (rschema, targetschemas, role)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   242
    
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   243
    @classmethod
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   244
    def esrelations_by_category(cls, entity, categories=None, permission=None):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   245
        """filter out result of relations_by_category(categories, permission) by
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   246
        removing final relations
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   247
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   248
        return a sorted list of (relation's label, relation'schema, role) 
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   249
        """
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   250
        result = []
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   251
        for rschema, ttypes, role in cls.erelations_by_category(
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   252
            entity, categories, permission):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   253
            if rschema.is_final():
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   254
                continue
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   255
            result.append((rschema.display_name(entity.req, role), rschema, role))
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   256
        return sorted(result)
1183
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   257
    
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   258
    @iclassmethod
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   259
    def field_by_name(cls_or_self, name, role='subject', eclass=None):
1276
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
   260
        """return field with the given name and role. If field is not explicitly
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
   261
        defined for the form but `eclass` is specified, guess_field will be
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
   262
        called.
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
   263
        """
1183
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   264
        try:
1234
sylvain.thenault@logilab.fr
parents: 1183
diff changeset
   265
            return super(AutomaticEntityForm, cls_or_self).field_by_name(name, role)
1276
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
   266
        except FieldNotFound: # XXX should raise more explicit exception
1183
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   267
            if eclass is None:
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   268
                raise
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   269
            field = guess_field(eclass, cls_or_self.schema.rschema(name), role,
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   270
                                eidparam=True)
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   271
            if field is None:
62afd820d3ae field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   272
                raise
1276
f213008fad2c catch FieldNotFound instead of Exception in field_by_name, return the field if guessable
sylvain.thenault@logilab.fr
parents: 1234
diff changeset
   273
            return field
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   274
    
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   275
    def __init__(self, *args, **kwargs):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   276
        super(AutomaticEntityForm, self).__init__(*args, **kwargs)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   277
        if self.edited_entity.has_eid():
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   278
            self.edited_entity.complete()
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   279
        for rschema, role in self.editable_attributes():
1277
2eeef019e186 don't guess field if explicitly defined, rename init_rtags_category
sylvain.thenault@logilab.fr
parents: 1276
diff changeset
   280
            try:
2eeef019e186 don't guess field if explicitly defined, rename init_rtags_category
sylvain.thenault@logilab.fr
parents: 1276
diff changeset
   281
                self.field_by_name(rschema.type, role)
2eeef019e186 don't guess field if explicitly defined, rename init_rtags_category
sylvain.thenault@logilab.fr
parents: 1276
diff changeset
   282
                continue # explicitly specified
2eeef019e186 don't guess field if explicitly defined, rename init_rtags_category
sylvain.thenault@logilab.fr
parents: 1276
diff changeset
   283
            except FieldNotFound:
2eeef019e186 don't guess field if explicitly defined, rename init_rtags_category
sylvain.thenault@logilab.fr
parents: 1276
diff changeset
   284
                pass # has to be guessed
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   285
            wdgname = self.rwidgets.etype_rtag(self.edited_entity.id, rschema,
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   286
                                               role)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   287
            if wdgname:
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   288
                widget = getattr(formwidgets, wdgname)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   289
                field = guess_field(self.edited_entity.__class__, rschema, role,
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   290
                                    eidparam=True, widget=widget)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   291
            else:
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   292
                field = guess_field(self.edited_entity.__class__, rschema, role,
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   293
                                    eidparam=True)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   294
            if field is not None:
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   295
                self.fields.append(field)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   296
    
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   297
    def relations_by_category(self, categories=None, permission=None):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   298
        """return a list of (relation schema, target schemas, role) matching
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   299
        given category(ies) and permission
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   300
        """
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   301
        return self.erelations_by_category(self.edited_entity, categories,
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   302
                                           permission)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   303
    
1291
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   304
    def inlined_relations(self):
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   305
        """return a list of (relation schema, target schemas, role) matching
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   306
        given category(ies) and permission
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   307
        """
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   308
        # we'll need an initialized varmaker if there are some inlined relation
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   309
        self.initialize_varmaker()
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   310
        return self.erelations_by_category(self.edited_entity, True, 'add', self.rinlined)
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1286
diff changeset
   311
    
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   312
    def srelations_by_category(self, categories=None, permission=None):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   313
        """filter out result of relations_by_category(categories, permission) by
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   314
        removing final relations
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   315
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   316
        return a sorted list of (relation's label, relation'schema, role) 
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   317
        """
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   318
        return self.esrelations_by_category(self.edited_entity, categories,
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   319
                                           permission)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   320
        
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   321
    def action(self):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   322
        """return the form's action attribute. Default to validateform if not
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   323
        explicitly overriden.
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   324
        """
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   325
        try:
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   326
            return self._action
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   327
        except AttributeError:
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   328
            return self.build_url('validateform')
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   329
        
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   330
    def set_action(self, value):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   331
        """override default action"""
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   332
        self._action = value
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   333
        
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   334
    action = property(action, set_action)
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   335
    
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   336
    def editable_attributes(self):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   337
        """return a list of (relation schema, role) to edit for the entity"""
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   338
        return [(rschema, x) for rschema, _, x in self.relations_by_category(
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   339
            self.attrcategories, 'add') if rschema != 'eid']
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   340
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   341
    def relations_table(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   342
        """yiels 3-tuples (rtype, target, related_list)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   343
        where <related_list> itself a list of :
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   344
          - node_id (will be the entity element's DOM id)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   345
          - appropriate javascript's togglePendingDelete() function call
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   346
          - status 'pendingdelete' or ''
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   347
          - oneline view of related entity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   348
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   349
        entity = self.edited_entity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   350
        pending_deletes = self.req.get_pending_deletes(entity.eid)
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   351
        for label, rschema, role in self.srelations_by_category('generic', 'add'):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   352
            relatedrset = entity.related(rschema, role, limit=self.limit)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   353
            if rschema.has_perm(self.req, 'delete'):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   354
                toggable_rel_link_func = toggable_relation_link
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   355
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   356
                toggable_rel_link_func = lambda x, y, z: u''
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   357
            related = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   358
            for row in xrange(relatedrset.rowcount):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   359
                nodeid = relation_id(entity.eid, rschema, role,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   360
                                     relatedrset[row][0])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   361
                if nodeid in pending_deletes:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   362
                    status = u'pendingDelete'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   363
                    label = '+'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   364
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   365
                    status = u''
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   366
                    label = 'x'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   367
                dellink = toggable_rel_link_func(entity.eid, nodeid, label)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   368
                eview = self.view('oneline', relatedrset, row=row)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   369
                related.append((nodeid, dellink, status, eview))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   370
            yield (rschema, role, related)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   371
            
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   372
    def restore_pending_inserts(self, cell=False):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   373
        """used to restore edition page as it was before clicking on
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   374
        'search for <some entity type>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   375
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   376
        eid = self.edited_entity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   377
        cell = cell and "div_insert_" or "tr"
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   378
        pending_inserts = set(self.req.get_pending_inserts(eid))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   379
        for pendingid in pending_inserts:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   380
            eidfrom, rtype, eidto = pendingid.split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   381
            if typed_eid(eidfrom) == eid: # subject
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   382
                label = display_name(self.req, rtype, 'subject')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   383
                reid = eidto
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   384
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   385
                label = display_name(self.req, rtype, 'object')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   386
                reid = eidfrom
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   387
            jscall = "javascript: cancelPendingInsert('%s', '%s', null, %s);" \
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   388
                     % (pendingid, cell, eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   389
            rset = self.req.eid_rset(reid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   390
            eview = self.view('text', rset, row=0)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   391
            # XXX find a clean way to handle baskets
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   392
            if rset.description[0][0] == 'Basket':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   393
                eview = '%s (%s)' % (eview, display_name(self.req, 'Basket'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   394
            yield rtype, pendingid, jscall, label, reid, eview
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   395
            
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   396
    # should_* method extracted to allow overriding
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   397
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   398
    def should_inline_relation_form(self, rschema, targettype, role):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   399
        """return true if the given relation with entity has role and a
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   400
        targettype target should be inlined
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   401
        """
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   402
        return self.rinlined.etype_rtag(self.edited_entity.id, rschema, role, targettype)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   403
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   404
    def should_display_inline_creation_form(self, rschema, existant, card):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   405
        """return true if a creation form should be inlined
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   406
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   407
        by default true if there is no related entity and we need at least one
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   408
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   409
        return not existant and card in '1+'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   410
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   411
    def should_display_add_new_relation_link(self, rschema, existant, card):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   412
        """return true if we should add a link to add a new creation form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   413
        (through ajax call)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   414
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   415
        by default true if there is no related entity or if the relation has
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   416
        multiple cardinality
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   417
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   418
        return not existant or card in '+*'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   419
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   420
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   421
class EditionFormView(EntityView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   422
    """display primary entity edition form"""    
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   423
    id = 'edition'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   424
    # add yes() so it takes precedence over deprecated views in baseforms,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   425
    # though not baseforms based customized view
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   426
    __select__ = one_line_rset() & non_final_entity() & yes()
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   427
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   428
    title = _('edition')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   429
    controller = 'edit'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   430
    
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   431
    def cell_call(self, row, col, **kwargs):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   432
        entity = self.complete_entity(row, col)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   433
        self.render_form(entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   434
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   435
    def render_form(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   436
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   437
        self.form_title(entity)
1286
cb68c8af3858 use entity's rset and row/col info
sylvain.thenault@logilab.fr
parents: 1285
diff changeset
   438
        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
   439
                                       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
   440
                                       submitmsg=self.submited_message())
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   441
        self.init_form(form, entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   442
        self.w(form.form_render(renderer=EntityFormRenderer(), formvid=u'edition'))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   443
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   444
    def init_form(self, form, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   445
        """customize your form before rendering here"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   446
        form.form_add_hidden(u'__maineid', entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   447
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   448
    def form_title(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   449
        """the form view title"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   450
        ptitle = self.req._(self.title)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   451
        self.w(u'<div class="formTitle"><span>%s %s</span></div>' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   452
            entity.dc_type(), ptitle and '(%s)' % ptitle))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   453
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   454
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   455
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   456
        return self.req._('entity edited')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   457
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   458
        
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   459
class CreationFormView(EditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   460
    """display primary entity creation form"""    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   461
    id = 'creation'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   462
    __select__ = specified_etype_implements('Any') & yes()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   463
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   464
    title = _('creation')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   465
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   466
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   467
        """creation view for an entity"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   468
        etype = kwargs.pop('etype', self.req.form.get('etype'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   469
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   470
            entity = self.vreg.etype_class(etype)(self.req, None, None)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   471
        except:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   472
            self.w(self.req._('no such entity type %s') % etype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   473
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   474
            self.initialize_varmaker()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   475
            entity.eid = self.varmaker.next()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   476
            self.render_form(entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   477
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   478
    def form_title(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   479
        """the form view title"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   480
        if '__linkto' in self.req.form:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   481
            if isinstance(self.req.form['__linkto'], list):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   482
                # XXX which one should be considered (case: add a ticket to a
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   483
                # version in jpl)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   484
                rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   485
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   486
                rtype, linkto_eid, role = self.req.form['__linkto'].split(':')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   487
            linkto_rset = self.req.eid_rset(linkto_eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   488
            linkto_type = linkto_rset.description[0][0]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   489
            if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   490
                title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   491
                    entity.e_schema, entity.e_schema, rtype, linkto_type))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   492
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   493
                title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   494
                    entity.e_schema, linkto_type, rtype, entity.e_schema))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   495
            msg = title % {'linkto' : self.view('incontext', linkto_rset)}
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   496
            self.w(u'<div class="formTitle notransform"><span>%s</span></div>' % msg)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   497
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   498
            super(CreationFormView, self).form_title(entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   499
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   500
    def url(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   501
        """return the url associated with this view"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   502
        return self.create_url(self.req.form.get('etype'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   503
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   504
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   505
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   506
        return self.req._('entity created')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   507
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   508
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   509
class CopyFormView(EditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   510
    """display primary entity creation form initialized with values from another
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   511
    entity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   512
    """    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   513
    id = 'copy'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   514
    def render_form(self, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   515
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   516
        # make a copy of entity to avoid altering the entity in the
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   517
        # request's cache. 
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   518
        self.newentity = copy(entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   519
        self.copying = self.newentity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   520
        self.newentity.eid = None
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   521
        self.w(u'<script type="text/javascript">updateMessage("%s");</script>\n'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   522
               % self.req._('Please note that this is only a shallow copy'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   523
        super(CopyFormView, self).render_form(entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   524
        del self.newentity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   525
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   526
    def init_form(self, form, entity):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   527
        """customize your form before rendering here"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   528
        super(CopyFormView, self).init_form(form, entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   529
        if entity.eid == self.newentity.eid:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   530
            form.form_add_hidden('__cloned_eid', self.copying, eidparam=True)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   531
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   532
    def submited_message(self):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   533
        """return the message that will be displayed on successful edition"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   534
        return self.req._('entity copied')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   535
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   536
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   537
class TableEditForm(CompositeForm):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   538
    id = 'muledit'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   539
    onsubmit = "return validateForm('entityForm', null);"
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   540
    form_buttons = [SubmitButton(_('validate modifications on selected items')),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1297
diff changeset
   541
                    ResetButton(_('revert changes'))]
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   542
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   543
    def __init__(self, *args, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   544
        super(TableEditForm, self).__init__(*args, **kwargs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   545
        for row in xrange(len(self.rset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   546
            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
   547
                                           row=row, attrcategories=('primary',),
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   548
                                           set_error_url=False)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   549
            # XXX rely on the MultipleEntityFormRenderer to put the eid input
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   550
            form.remove_field(form.field_by_name('eid'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   551
            self.form_add_subform(form)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff changeset
   552
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   553
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   554
class TableEditFormView(EntityView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   555
    id = 'muledit'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   556
    __select__ = EntityView.__select__ & yes()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   557
    title = _('multiple edit')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   558
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   559
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   560
        """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
   561
        should be the eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   562
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   563
        #self.form_title(entity)
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   564
        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
   565
        self.w(form.form_render(renderer=EntityCompositeFormRenderer()))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   566
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   567
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   568
class InlineEntityEditionFormView(EntityView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   569
    id = 'inline-edition'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   570
    __select__ = non_final_entity() & match_kwargs('peid', 'rtype')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   571
    removejs = "removeInlinedEntity('%s', '%s', '%s')"
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   572
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   573
    def call(self, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   574
        """redefine default call() method to avoid automatic
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   575
        insertions of <div class="section"> between each row of
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   576
        the resultset
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   577
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   578
        rset = self.rset
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   579
        for i in xrange(len(rset)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   580
            self.wview(self.id, rset, row=i, **kwargs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   581
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   582
    def cell_call(self, row, col, peid, rtype, role='subject', **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   583
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   584
        :param peid: the parent entity's eid hosting the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   585
        :param rtype: the relation bridging `etype` and `peid`
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   586
        :param role: the role played by the `peid` in the relation
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   587
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   588
        entity = self.entity(row, col)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   589
        divonclick = "restoreInlinedEntity('%s', '%s', '%s')" % (peid, rtype,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   590
                                                                 entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   591
        self.render_form(entity, peid, rtype, role, divonclick=divonclick)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   592
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   593
    def render_form(self, entity, peid, rtype, role, **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   594
        """fetch and render the form"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   595
        rschema = self.schema.rschema(rtype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   596
        divid = '%s-%s-%s' % (peid, rtype, entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   597
        title = rschema.display_name(self.req, role)
1297
174ebf2a1459 should give None as rset
sylvain.thenault@logilab.fr
parents: 1296
diff changeset
   598
        form = self.vreg.select_object('forms', 'edition', self.req, None,
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   599
                                       entity=entity)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   600
        removejs = self.removejs % (peid, rtype,entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   601
        if self.keep_entity(entity, peid, rtype):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   602
            if entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   603
                rval = entity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   604
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   605
                rval = INTERNAL_FIELD_VALUE
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   606
            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
   607
        form.form_add_hidden(name='%s:%s' % (rtype, peid), value=entity.eid,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   608
                             id='rel-%s-%s-%s'  % (peid, rtype, entity.eid))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   609
        self.w(form.form_render(renderer=EntityInlinedFormRenderer(), divid=divid,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   610
                                title=title, removejs=removejs,**kwargs))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   611
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   612
    def keep_entity(self, entity, peid, rtype):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   613
        if not entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   614
            return True
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   615
        # are we regenerating form because of a validation error ?
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   616
        erroneous_post = self.req.data.get('formvalues')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   617
        if erroneous_post:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   618
            cdvalues = self.req.list_form_param('%s:%s' % (rtype, peid),
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   619
                                                erroneous_post)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   620
            if unicode(entity.eid) not in cdvalues:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   621
                return False
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   622
        return True
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   623
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   624
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   625
class InlineEntityCreationFormView(InlineEntityEditionFormView):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   626
    id = 'inline-creation'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   627
    __select__ = (match_kwargs('peid', 'rtype')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   628
                  & specified_etype_implements('Any'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   629
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   630
    def call(self, etype, peid, rtype, role='subject', **kwargs):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   631
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   632
        :param etype: the entity type being created in the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   633
        :param peid: the parent entity's eid hosting the inline form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   634
        :param rtype: the relation bridging `etype` and `peid`
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   635
        :param role: the role played by the `peid` in the relation
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   636
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   637
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   638
            entity = self.vreg.etype_class(etype)(self.req, None, None)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   639
        except:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   640
            self.w(self.req._('no such entity type %s') % etype)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   641
            return
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   642
        self.initialize_varmaker()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   643
        entity.eid = self.varmaker.next()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents: 1138
diff changeset
   644
        self.render_form(entity, peid, rtype, role)