doc/book/en/devweb/edition/form.rst
changeset 7901 bdb81b1a8243
parent 7643 f3e3892fc7e3
child 8190 2a3c1b787688
equal deleted inserted replaced
7896:4c954e1e73ef 7901:bdb81b1a8243
    85     from cubicweb.web import formfields
    85     from cubicweb.web import formfields
    86 
    86 
    87     def ticket_done_in_choices(form, field):
    87     def ticket_done_in_choices(form, field):
    88         entity = form.edited_entity
    88         entity = form.edited_entity
    89         # first see if its specified by __linkto form parameters
    89         # first see if its specified by __linkto form parameters
    90         linkedto = formfields.relvoc_linkedto(entity, 'done_in', 'subject')
    90         linkedto = form.linked_to[('done_in', 'subject')]
    91         if linkedto:
    91         if linkedto:
    92             return linkedto
    92             return linkedto
    93         # it isn't, get initial values
    93         # it isn't, get initial values
    94         vocab = formfields.relvoc_init(entity, 'done_in', 'subject')
    94         vocab = field.relvoc_init(form)
    95         veid = None
    95         veid = None
    96         # try to fetch the (already or pending) related version and project
    96         # try to fetch the (already or pending) related version and project
    97         if not entity.has_eid():
    97         if not entity.has_eid():
    98             peids = entity.linked_to('concerns', 'subject')
    98             peids = form.linked_to[('concerns', 'subject')]
    99             peid = peids and peids[0]
    99             peid = peids and peids[0]
   100         else:
   100         else:
   101             peid = entity.project.eid
   101             peid = entity.project.eid
   102             veid = entity.done_in and entity.done_in[0].eid
   102             veid = entity.done_in and entity.done_in[0].eid
   103         if peid:
   103         if peid:
   110             vocab += [(v.view('combobox'), v.eid) for v in rset.entities()
   110             vocab += [(v.view('combobox'), v.eid) for v in rset.entities()
   111                       if rschema.has_perm(form._cw, 'add', toeid=v.eid)
   111                       if rschema.has_perm(form._cw, 'add', toeid=v.eid)
   112                       and v.eid != veid]
   112                       and v.eid != veid]
   113         return vocab
   113         return vocab
   114 
   114 
   115 The first thing we have to do is fetch potential values from the
   115 The first thing we have to do is fetch potential values from the ``__linkto`` url
   116 ``__linkto`` url parameter that is often found in entity creation
   116 parameter that is often found in entity creation contexts (the creation action
   117 contexts (the creation action provides such a parameter with a
   117 provides such a parameter with a predetermined value; for instance in this case,
   118 predetermined value; for instance in this case, ticket creation could
   118 ticket creation could occur in the context of a `Version` entity). The
   119 occur in the context of a `Version` entity). The
   119 :class:`~cubicweb.web.formfields.RelationField` field class provides a
   120 :mod:`cubicweb.web.formfields` module provides a ``relvoc_linkedto``
   120 :meth:`~cubicweb.web.formfields.RelationField.relvoc_linkedto` method that gets a
   121 utility function that gets a list suitably filled with vocabulary
   121 list suitably filled with vocabulary values.
   122 values.
   122 
   123 
   123 .. sourcecode:: python
   124 .. sourcecode:: python
   124 
   125 
   125         linkedto = field.relvoc_linkedto(form)
   126         linkedto = formfields.relvoc_linkedto(entity, 'done_in', 'subject')
       
   127         if linkedto:
   126         if linkedto:
   128             return linkedto
   127             return linkedto
   129 
   128 
   130 Then, if no ``__linkto`` argument was given, we must prepare the
   129 Then, if no ``__linkto`` argument was given, we must prepare the vocabulary with
   131 vocabulary with an initial empty value (because `done_in` is not
   130 an initial empty value (because `done_in` is not mandatory, we must allow the
   132 mandatory, we must allow the user to not select a verson) and already
   131 user to not select a verson) and already linked values. This is done with the
   133 linked values. This is done with the ``relvoc_init`` function.
   132 :meth:`~cubicweb.web.formfields.RelationField.relvoc_init` method.
   134 
   133 
   135 .. sourcecode:: python
   134 .. sourcecode:: python
   136 
   135 
   137         vocab = formfields.relvoc_init(entity, 'done_in', 'subject')
   136         vocab = field.relvoc_init(form)
   138 
   137 
   139 But then, we have to give more: if the ticket is related to a project,
   138 But then, we have to give more: if the ticket is related to a project,
   140 we should provide all the non published versions of this project
   139 we should provide all the non published versions of this project
   141 (`Version` and `Project` can be related through the `version_of`
   140 (`Version` and `Project` can be related through the `version_of`
   142 relation). Conversely, if we do not know yet the project, it would not
   141 relation). Conversely, if we do not know yet the project, it would not
   167 
   166 
   168 .. sourcecode:: python
   167 .. sourcecode:: python
   169 
   168 
   170         veid = None
   169         veid = None
   171         if not entity.has_eid():
   170         if not entity.has_eid():
   172             peids = entity.linked_to('concerns', 'subject')
   171             peids = form.linked_to[('concerns', 'subject')]
   173             peid = peids and peids[0]
   172             peid = peids and peids[0]
   174         else:
   173         else:
   175             peid = entity.project.eid
   174             peid = entity.project.eid
   176             veid = entity.done_in and entity.done_in[0].eid
   175             veid = entity.done_in and entity.done_in[0].eid
   177 
   176