web/views/editforms.py
branchtls-sprint
changeset 1154 9b23a6836c32
parent 1151 b20677336ee6
child 1179 70825477c6ce
equal deleted inserted replaced
1153:6a7636b32a97 1154:9b23a6836c32
   247                 self.button_cancel(tabindex=self.req.next_tabindex())]
   247                 self.button_cancel(tabindex=self.req.next_tabindex())]
   248 
   248 
   249     def editable_attributes(self):
   249     def editable_attributes(self):
   250         """return a list of (relation schema, role) to edit for the entity
   250         """return a list of (relation schema, role) to edit for the entity
   251         """
   251         """
   252         return [(rschema, x) for rschema, _, x in self.relations_by_category(self.attrcategories, 'add')
   252         return [(rschema, x) for rschema, _, x in self.relations_by_category(
   253                 if rschema != 'eid']
   253             self.entity, self.attrcategories, 'add') if rschema != 'eid']
   254     
   254 
   255     def relations_by_category(self, categories=None, permission=None):
   255     @classmethod
       
   256     def relations_by_category(cls, entity, categories=None, permission=None):
   256         """return a list of (relation schema, target schemas, role) matching
   257         """return a list of (relation schema, target schemas, role) matching
   257         categories and permission
   258         categories and permission
   258         """
   259         """
   259         if categories is not None:
   260         if categories is not None:
   260             if not isinstance(categories, (list, tuple, set, frozenset)):
   261             if not isinstance(categories, (list, tuple, set, frozenset)):
   261                 categories = (categories,)
   262                 categories = (categories,)
   262             if not isinstance(categories, (set, frozenset)):
   263             if not isinstance(categories, (set, frozenset)):
   263                 categories = frozenset(categories)
   264                 categories = frozenset(categories)
   264         eschema  = self.edited_entity.e_schema
   265         eschema  = entity.e_schema
   265         rtags = self.rcategories
   266         rtags = cls.rcategories
   266         permsoverrides = self.rpermissions_overrides
   267         permsoverrides = cls.rpermissions_overrides
   267         if self.edited_entity.has_eid():
   268         if entity.has_eid():
   268             eid = self.edited_entity.eid
   269             eid = entity.eid
   269         else:
   270         else:
   270             eid = None
   271             eid = None
   271         for rschema, targetschemas, role in eschema.relation_definitions(True):
   272         for rschema, targetschemas, role in eschema.relation_definitions(True):
   272             if rschema in ('identity', 'has_text'):
   273             if rschema in ('identity', 'has_text'):
   273                 continue
   274                 continue
   284                 # created...
   285                 # created...
   285                 if eid is None and '%s_on_new' % permission in permsoverrides.etype_rtags(eschema, role, rschema):
   286                 if eid is None and '%s_on_new' % permission in permsoverrides.etype_rtags(eschema, role, rschema):
   286                     yield (rschema, targetschemas, role)
   287                     yield (rschema, targetschemas, role)
   287                     continue
   288                     continue
   288                 if rschema.is_final():
   289                 if rschema.is_final():
   289                     if not rschema.has_perm(self.req, permission, eid):
   290                     if not rschema.has_perm(entity.req, permission, eid):
   290                         continue
   291                         continue
   291                 elif role == 'subject':
   292                 elif role == 'subject':
   292                     if not ((eid is None and rschema.has_local_role(permission)) or
   293                     if not ((eid is None and rschema.has_local_role(permission)) or
   293                             rschema.has_perm(self.req, permission, fromeid=eid)):
   294                             rschema.has_perm(entity.req, permission, fromeid=eid)):
   294                         continue
   295                         continue
   295                     # on relation with cardinality 1 or ?, we need delete perm as well
   296                     # on relation with cardinality 1 or ?, we need delete perm as well
   296                     # if the relation is already set
   297                     # if the relation is already set
   297                     if (permission == 'add'
   298                     if (permission == 'add'
   298                         and rschema.cardinality(eschema, targetschemas[0], role) in '1?'
   299                         and rschema.cardinality(eschema, targetschemas[0], role) in '1?'
   299                         and eid and self.edited_entity.related(rschema.type, role)
   300                         and eid and entity.related(rschema.type, role)
   300                         and not rschema.has_perm(self.req, 'delete', fromeid=eid,
   301                         and not rschema.has_perm(entity.req, 'delete', fromeid=eid,
   301                                                  toeid=self.edited_entity.related(rschema.type, role)[0][0])):
   302                                                  toeid=entity.related(rschema.type, role)[0][0])):
   302                         continue
   303                         continue
   303                 elif role == 'object':
   304                 elif role == 'object':
   304                     if not ((eid is None and rschema.has_local_role(permission)) or
   305                     if not ((eid is None and rschema.has_local_role(permission)) or
   305                             rschema.has_perm(self.req, permission, toeid=eid)):
   306                             rschema.has_perm(entity.req, permission, toeid=eid)):
   306                         continue
   307                         continue
   307                     # on relation with cardinality 1 or ?, we need delete perm as well
   308                     # on relation with cardinality 1 or ?, we need delete perm as well
   308                     # if the relation is already set
   309                     # if the relation is already set
   309                     if (permission == 'add'
   310                     if (permission == 'add'
   310                         and rschema.cardinality(targetschemas[0], eschema, role) in '1?'
   311                         and rschema.cardinality(targetschemas[0], eschema, role) in '1?'
   311                         and eid and self.edited_entity.related(rschema.type, role)
   312                         and eid and entity.related(rschema.type, role)
   312                         and not rschema.has_perm(self.req, 'delete', toeid=eid,
   313                         and not rschema.has_perm(entity.req, 'delete', toeid=eid,
   313                                                  fromeid=self.edited_entity.related(rschema.type, role)[0][0])):
   314                                                  fromeid=entity.related(rschema.type, role)[0][0])):
   314                         continue
   315                         continue
   315             yield (rschema, targetschemas, role)
   316             yield (rschema, targetschemas, role)
   316     
   317     
   317     def srelations_by_category(self, categories=None, permission=None):
   318     def srelations_by_category(self, categories=None, permission=None):
   318         """filter out result of relations_by_category(categories, permission) by
   319         """filter out result of relations_by_category(categories, permission) by
   319         removing final relations
   320         removing final relations
   320 
   321 
   321         return a list of (relation's label, relation'schema, role)
   322         return a list of (relation's label, relation'schema, role)
   322         """
   323         """
   323         result = []
   324         result = []
   324         for rschema, ttypes, role in self.relations_by_category(categories,
   325         for rschema, ttypes, role in self.relations_by_category(
   325                                                                 permission):
   326             self.entity, categories, permission):
   326             if rschema.is_final():
   327             if rschema.is_final():
   327                 continue
   328                 continue
   328             result.append( (rschema.display_name(self.req, role), rschema, role) )
   329             result.append( (rschema.display_name(self.req, role), rschema, role) )
   329         return sorted(result)
   330         return sorted(result)
   330         
   331