web/views/uicfg.py
changeset 8667 5a394fc419b4
parent 8666 1dd655788ece
child 8696 0bb18407c053
equal deleted inserted replaced
8666:1dd655788ece 8667:5a394fc419b4
   352                     and not rdef.has_perm(cw, 'delete', toeid=eid,
   352                     and not rdef.has_perm(cw, 'delete', toeid=eid,
   353                                           fromeid=entity.related(rschema.type, role)[0][0])):
   353                                           fromeid=entity.related(rschema.type, role)[0][0])):
   354                     continue
   354                     continue
   355             yield (rschema, targetschemas, role)
   355             yield (rschema, targetschemas, role)
   356 
   356 
       
   357     def hide_field(self, etype, attr, desttype='*', formtype='main'):
       
   358         """hide `attr` in `etype` forms.
       
   359 
       
   360         :param etype: the entity type as a string
       
   361         :param attr: the name of the attribute or relation to hide
       
   362         :param formtype: which form will be affected ('main', 'inlined', etc.),
       
   363          *main* by default.
       
   364 
       
   365         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_rel)
       
   366 
       
   367         Examples:
       
   368 
       
   369         .. sourcecode:: python
       
   370 
       
   371           from cubicweb.web.views.uicfg import autoform_section as afs
       
   372           afs.hide_field('CWUser', 'login')
       
   373           afs.hide_field('*', 'name')
       
   374           afs.hide_field('CWUser', 'use_email', formtype='inlined')
       
   375 
       
   376         """
       
   377         self._tag_etype_attr(etype, attr, desttype,
       
   378                              formtype=formtype, section='hidden')
       
   379 
       
   380     def hide_fields(self, etype, attrs, formtype='main'):
       
   381         """simple for-loop wrapper around :func:`hide_field`.
       
   382 
       
   383         :param etype: the entity type as a string
       
   384         :param attrs: the ordered list of attribute names (or relations)
       
   385         :param formtype: which form will be affected ('main', 'inlined', etc.),
       
   386          *main* by default.
       
   387 
       
   388         `attrs` can be strings or 2-tuples (relname, role_of_etype_in_the_rel)
       
   389 
       
   390         Examples:
       
   391 
       
   392         .. sourcecode:: python
       
   393 
       
   394           from cubicweb.web.views.uicfg import autoform_section as afs
       
   395           afs.hide_fields('CWUser', ('login', ('use_email', 'subject')),
       
   396                           formtype='inlined')
       
   397         """
       
   398         for attr in attrs:
       
   399             self.hide_field(etype, attr, formtype=formtype)
       
   400 
       
   401     def edit_inline(self, etype, attr, desttype='*', formtype=('main', 'inlined')):
       
   402         """edit `attr` with and inlined form.
       
   403 
       
   404         :param etype: the entity type as a string
       
   405         :param attr: the name of the attribute or relation
       
   406         :param desttype: the destination type(s) concerned, default is everything
       
   407         :param formtype: which form will be affected ('main', 'inlined', etc.),
       
   408           *main* and *inlined* by default.
       
   409 
       
   410         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
       
   411 
       
   412         Examples:
       
   413 
       
   414         .. sourcecode:: python
       
   415 
       
   416           from cubicweb.web.views.uicfg import autoform_section as afs
       
   417 
       
   418           afs.edit_inline('*', 'use_email')
       
   419       """
       
   420         self._tag_etype_attr(etype, attr, desttype, formtype=formtype,
       
   421                              section='inlined')
       
   422 
       
   423     def edit_as_attr(self, etype, attr, desttype='*', formtype=('main', 'muledit')):
       
   424         """make `attr` appear in the *attributes* section of `etype` form.
       
   425 
       
   426         :param etype: the entity type as a string
       
   427         :param attr: the name of the attribute or relation
       
   428         :param desttype: the destination type(s) concerned, default is everything
       
   429         :param formtype: which form will be affected ('main', 'inlined', etc.),
       
   430           *main* and *muledit* by default.
       
   431 
       
   432         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
       
   433 
       
   434         Examples:
       
   435 
       
   436         .. sourcecode:: python
       
   437 
       
   438           from cubicweb.web.views.uicfg import autoform_section as afs
       
   439 
       
   440           afs.edit_as_attr('CWUser', 'in_group')
       
   441         """
       
   442         self._tag_etype_attr(etype, attr, desttype,
       
   443                              formtype=formtype, section='attributes')
       
   444 
       
   445     def set_muledit_editable(self, etype, attrs):
       
   446         """make `attrs` appear in muledit form of `etype`.
       
   447 
       
   448         :param etype: the entity type as a string
       
   449         :param attrs: the ordered list of attribute names (or relations)
       
   450 
       
   451         `attrs` can be strings or 2-tuples (relname, role_of_etype_in_the_relation)
       
   452 
       
   453         Examples:
       
   454 
       
   455         .. sourcecode:: python
       
   456 
       
   457           from cubicweb.web.views.uicfg import autoform_section as afs
       
   458 
       
   459           afs.set_muledit_editable('CWUser', ('firstname', 'surname', 'in_group'))
       
   460         """
       
   461         for attr in attrs:
       
   462             self.edit_as_attr(self, etype, attr, formtype='muledit')
       
   463 
   357 autoform_section = AutoformSectionRelationTags()
   464 autoform_section = AutoformSectionRelationTags()
   358 
   465 
       
   466 
   359 # relations'field class
   467 # relations'field class
       
   468 
   360 class AutoformFieldTags(RelationTags):
   469 class AutoformFieldTags(RelationTags):
   361     __regid__ = 'autoform_field'
   470     __regid__ = 'autoform_field'
   362 
   471 
       
   472     def set_field(self, etype, attr, field):
       
   473         """sets the `attr` field of `etype`.
       
   474 
       
   475         :param etype: the entity type as a string
       
   476         :param attr: the name of the attribute or relation
       
   477 
       
   478         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
       
   479 
       
   480         """
       
   481         self._tag_etype_attr(etype, attr, '*', field)
       
   482 
   363 autoform_field = AutoformFieldTags()
   483 autoform_field = AutoformFieldTags()
   364 
   484 
       
   485 
   365 # relations'field explicit kwargs (given to field's __init__)
   486 # relations'field explicit kwargs (given to field's __init__)
       
   487 
   366 class AutoformFieldKwargsTags(RelationTagsDict):
   488 class AutoformFieldKwargsTags(RelationTagsDict):
   367     __regid__ = 'autoform_field_kwargs'
   489     __regid__ = 'autoform_field_kwargs'
       
   490 
       
   491     def set_fields_order(self, etype, attrs):
       
   492         """specify the field order in `etype` main edition form.
       
   493 
       
   494         :param etype: the entity type as a string
       
   495         :param attrs: the ordered list of attribute names (or relations)
       
   496 
       
   497         `attrs` can be strings or 2-tuples (relname, role_of_etype_in_the_rel)
       
   498 
       
   499         Unspecified fields will be displayed after specified ones, their
       
   500         order being consistent with the schema definition.
       
   501 
       
   502         Examples:
       
   503 
       
   504         .. sourcecode:: python
       
   505 
       
   506           from cubicweb.web.views.uicfg import autoform_field_kwargs as affk
       
   507           affk.set_fields_order('CWUser', ('firstname', 'surname', 'login'))
       
   508           affk.set_fields_order('CWUser', ('firstname', ('in_group', 'subject'),
       
   509                                 'surname', 'login'))
       
   510 
       
   511         """
       
   512         for index, attr in enumerate(attrs):
       
   513             self._tag_etype_attr(etype, attr, '*', {'order': index})
       
   514 
       
   515     def set_field_kwargs(self, etype, attr, **kwargs):
       
   516         """tag `attr` field of `etype` with additional named paremeters.
       
   517 
       
   518         :param etype: the entity type as a string
       
   519         :param attr: the name of the attribute or relation
       
   520 
       
   521         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
       
   522 
       
   523         Examples:
       
   524 
       
   525         .. sourcecode:: python
       
   526 
       
   527           from cubicweb.web.views.uicfg import autoform_field_kwargs as affk
       
   528           affk.set_field_kwargs('Person', 'works_for', widget=fwdgs.AutoCompletionWidget())
       
   529           affk.set_field_kwargs('CWUser', 'login', label=_('login or email address'),
       
   530                                 widget=fwdgs.TextInput(attrs={'size': 30}))
       
   531         """
       
   532         self._tag_etype_attr(etype, attr, '*', kwargs)
       
   533 
   368 
   534 
   369 autoform_field_kwargs = AutoformFieldKwargsTags()
   535 autoform_field_kwargs = AutoformFieldKwargsTags()
   370 
   536 
   371 
   537 
   372 # set of tags of the form <action>_on_new on relations. <action> is a
   538 # set of tags of the form <action>_on_new on relations. <action> is a
   440 
   606 
   441 
   607 
   442 # boxes.EditBox configuration #################################################
   608 # boxes.EditBox configuration #################################################
   443 
   609 
   444 # 'link' / 'create' relation tags, used to control the "add entity" submenu
   610 # 'link' / 'create' relation tags, used to control the "add entity" submenu
       
   611 
   445 class ActionBoxUicfg(RelationTagsBool):
   612 class ActionBoxUicfg(RelationTagsBool):
   446     __regid__ = 'actionbox_appearsin_addmenu'
   613     __regid__ = 'actionbox_appearsin_addmenu'
   447 
   614 
   448     def _init(self, sschema, rschema, oschema, role):
   615     def _init(self, sschema, rschema, oschema, role):
   449         if self.get(sschema, rschema, oschema, role) is None:
   616         if self.get(sschema, rschema, oschema, role) is None:
   452                 return
   619                 return
   453             rdef = rschema.rdef(sschema, oschema)
   620             rdef = rschema.rdef(sschema, oschema)
   454             if not rdef.role_cardinality(role) in '?1' and rdef.composite == role:
   621             if not rdef.role_cardinality(role) in '?1' and rdef.composite == role:
   455                 self.tag_relation((sschema, rschema, oschema, role), True)
   622                 self.tag_relation((sschema, rschema, oschema, role), True)
   456 
   623 
       
   624     def _tag_etype_attr(self, etype, attr, desttype='*', *args, **kwargs):
       
   625         if isinstance(attr, basestring):
       
   626             attr, role = attr, 'subject'
       
   627         else:
       
   628             attr, role = attr
       
   629         if role == 'subject':
       
   630             self.tag_subject_of((etype, attr, desttype), *args, **kwargs)
       
   631         else:
       
   632             self.tag_object_of((desttype, attr, etype), *args, **kwargs)
       
   633 
       
   634     def append_to_addmenu(self, etype, attr, createdtype='*'):
       
   635         """adds `attr` in the actions box *addrelated* submenu of `etype`.
       
   636 
       
   637         :param etype: the entity type as a string
       
   638         :param attr: the name of the attribute or relation to hide
       
   639         :param createdtype: the target type of the relation (optional, defaults to '*' (all possible types))
       
   640 
       
   641         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
       
   642 
       
   643         """
       
   644         self._tag_etype_attr(etype, attr, createdtype, True)
       
   645 
       
   646     def remove_from_addmenu(self, etype, attr, createdtype='*'):
       
   647         """removes `attr` from the actions box *addrelated* submenu of `etype`.
       
   648 
       
   649         :param etype: the entity type as a string
       
   650         :param attr: the name of the attribute or relation to hide
       
   651         :param createdtype: the target type of the relation (optional, defaults to '*' (all possible types))
       
   652 
       
   653         `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
       
   654         """
       
   655         self._tag_etype_attr(etype, attr, createdtype, False)
       
   656 
   457 actionbox_appearsin_addmenu = ActionBoxUicfg()
   657 actionbox_appearsin_addmenu = ActionBoxUicfg()
   458 
   658 
   459 
   659 
   460 
   660 
   461 def registration_callback(vreg):
   661 def registration_callback(vreg):