web/views/autoform.py
changeset 4003 b9436fe77c9e
parent 3890 d7a270f50f54
child 4015 4f8235204dda
equal deleted inserted replaced
3998:94cc7cad3d2d 4003:b9436fe77c9e
    48     rfields = uicfg.autoform_field
    48     rfields = uicfg.autoform_field
    49     rfields_kwargs = uicfg.autoform_field_kwargs
    49     rfields_kwargs = uicfg.autoform_field_kwargs
    50 
    50 
    51     # class methods mapping schema relations to fields in the form ############
    51     # class methods mapping schema relations to fields in the form ############
    52 
    52 
    53 <<<<<<< /home/syt/src/fcubicweb/cubicweb/web/views/autoform.py
       
    54 =======
       
    55     @classmethod
       
    56     def erelations_by_category(cls, entity, categories=None, permission=None,
       
    57                                rtags=None, strict=False):
       
    58         """return a list of (relation schema, target schemas, role) matching
       
    59         categories and permission
       
    60 
       
    61         `strict`:
       
    62           bool telling if having local role is enough (strict = False) or not
       
    63         """
       
    64         if categories is not None:
       
    65             if not isinstance(categories, (list, tuple, set, frozenset)):
       
    66                 categories = (categories,)
       
    67             if not isinstance(categories, (set, frozenset)):
       
    68                 categories = frozenset(categories)
       
    69         eschema  = entity.e_schema
       
    70         if rtags is None:
       
    71             rtags = cls.rcategories
       
    72         permsoverrides = cls.rpermissions_overrides
       
    73         if entity.has_eid():
       
    74             eid = entity.eid
       
    75         else:
       
    76             eid = None
       
    77             strict = False
       
    78         for rschema, targetschemas, role in eschema.relation_definitions(True):
       
    79             # check category first, potentially lower cost than checking
       
    80             # permission which may imply rql queries
       
    81             if categories is not None:
       
    82                 _targetschemas = []
       
    83                 for tschema in targetschemas:
       
    84                     if not rtags.etype_get(eschema, rschema, role, tschema) in categories:
       
    85                         continue
       
    86                     rdef = rschema.role_rdef(eschema, tschema, role)
       
    87                     if not ((not strict and rdef.has_local_role(permission)) or
       
    88                             rdef.has_perm(entity.req, permission, fromeid=eid)):
       
    89                         continue
       
    90                     _targetschemas.append(tschema)
       
    91                 if not _targetschemas:
       
    92                     continue
       
    93                 targetschemas = _targetschemas
       
    94             if permission is not None:
       
    95                 # tag allowing to hijack the permission machinery when
       
    96                 # permission is not verifiable until the entity is actually
       
    97                 # created...
       
    98                 if eid is None and '%s_on_new' % permission in permsoverrides.etype_get(eschema, rschema, role):
       
    99                     yield (rschema, targetschemas, role)
       
   100                     continue
       
   101                 if rschema.final:
       
   102                     if not eschema.rdef(rschema).has_perm(entity.req, permission, eid=eid):
       
   103                         continue
       
   104                 elif role == 'subject':
       
   105                     # on relation with cardinality 1 or ?, we need delete perm as well
       
   106                     # if the relation is already set
       
   107                     if (permission == 'add'
       
   108                         and rschema.rdef(eschema, targetschemas[0]).role_cardinality(role) in '1?'
       
   109                         and eid and entity.related(rschema.type, role)
       
   110                         and not rschema.has_perm(entity.req, 'delete', fromeid=eid,
       
   111                                                  toeid=entity.related(rschema.type, role)[0][0])):
       
   112                         continue
       
   113                 elif role == 'object':
       
   114                     # on relation with cardinality 1 or ?, we need delete perm as well
       
   115                     # if the relation is already set
       
   116                     if (permission == 'add'
       
   117                         and rschema.rdef(targetschemas[0], eschema).role_cardinality(role) in '1?'
       
   118                         and eid and entity.related(rschema.type, role)
       
   119                         and not rschema.has_perm(entity.req, 'delete', toeid=eid,
       
   120                                                  fromeid=entity.related(rschema.type, role)[0][0])):
       
   121                         continue
       
   122             yield (rschema, targetschemas, role)
       
   123 
       
   124     @classmethod
       
   125     def esrelations_by_category(cls, entity, categories=None, permission=None,
       
   126                                 strict=False):
       
   127         """filter out result of relations_by_category(categories, permission) by
       
   128         removing final relations
       
   129 
       
   130         return a sorted list of (relation's label, relation'schema, role)
       
   131         """
       
   132         result = []
       
   133         for rschema, ttypes, role in cls.erelations_by_category(
       
   134             entity, categories, permission, strict=strict):
       
   135             if rschema.final:
       
   136                 continue
       
   137             result.append((rschema.display_name(entity.req, role), rschema, role))
       
   138         return sorted(result)
       
   139 
       
   140 >>>>>>> /tmp/autoform.py~other.rHDQ-C
       
   141     @iclassmethod
    53     @iclassmethod
   142     def field_by_name(cls_or_self, name, role='subject', eschema=None):
    54     def field_by_name(cls_or_self, name, role='subject', eschema=None):
   143         """return field with the given name and role. If field is not explicitly
    55         """return field with the given name and role. If field is not explicitly
   144         defined for the form but `eclass` is specified, guess_field will be
    56         defined for the form but `eclass` is specified, guess_field will be
   145         called.
    57         called.