entities/__init__.py
branchtls-sprint
changeset 1154 9b23a6836c32
parent 1150 2d1b721fded9
child 1179 70825477c6ce
equal deleted inserted replaced
1153:6a7636b32a97 1154:9b23a6836c32
    21 
    21 
    22 class AnyEntity(Entity):
    22 class AnyEntity(Entity):
    23     """an entity instance has e_schema automagically set on the class and
    23     """an entity instance has e_schema automagically set on the class and
    24     instances have access to their issuing cursor
    24     instances have access to their issuing cursor
    25     """
    25     """
    26     id = 'Any'   
    26     id = 'Any'                    
    27     __rtags__ = {
       
    28         'is' : ('generated', 'link'),
       
    29         'is_instance_of' : ('generated', 'link'),
       
    30         'identity' : ('generated', 'link'),
       
    31         
       
    32         # use primary and not generated for eid since it has to be an hidden
       
    33         # field in edition
       
    34         ('eid',                '*', 'subject'): 'primary',
       
    35         ('creation_date',      '*', 'subject'): 'generated',
       
    36         ('modification_date',  '*', 'subject'): 'generated',
       
    37         ('has_text',           '*', 'subject'): 'generated',
       
    38         
       
    39         ('require_permission', '*', 'subject') : ('generated', 'link'),
       
    40         ('owned_by',           '*', 'subject') : ('generated', 'link'),
       
    41         ('created_by',         '*', 'subject') : ('generated', 'link'),
       
    42         
       
    43         ('wf_info_for',        '*', 'subject') : ('generated', 'link'),
       
    44         ('wf_info_for',        '*', 'object')  : ('generated', 'link'),
       
    45                  
       
    46         ('description',        '*', 'subject'): 'secondary',
       
    47 
       
    48         # XXX should be moved in their respective cubes
       
    49         ('filed_under',        '*', 'subject') : ('generic', 'link'),
       
    50         ('filed_under',        '*', 'object')  : ('generic', 'create'),
       
    51         # generated since there is a componant to handle comments
       
    52         ('comments',           '*', 'subject') : ('generated', 'link'),
       
    53         ('comments',           '*', 'object')  : ('generated', 'link'),
       
    54         }
       
    55 
       
    56     __implements__ = (IBreadCrumbs, IFeed)
    27     __implements__ = (IBreadCrumbs, IFeed)
    57     
    28     
    58     @classmethod
    29     @classmethod
    59     def selected(cls, etype):
    30     def selected(cls, etype):
    60         """the special Any entity is used as the default factory, so
    31         """the special Any entity is used as the default factory, so
   275             wdg = widget(cls.vreg, cls, rschema, tschema, 'subject')
   246             wdg = widget(cls.vreg, cls, rschema, tschema, 'subject')
   276         else:
   247         else:
   277             tschema = rschema.subjects(cls.e_schema)[0]
   248             tschema = rschema.subjects(cls.e_schema)[0]
   278             wdg = widget(cls.vreg, tschema, rschema, cls, 'object')
   249             wdg = widget(cls.vreg, tschema, rschema, cls, 'object')
   279         return wdg
   250         return wdg
   280     
   251 
       
   252     @obsolete('use AutomaticEntityForm.relations_by_category')
   281     def relations_by_category(self, categories=None, permission=None):
   253     def relations_by_category(self, categories=None, permission=None):
   282         if categories is not None:
   254         from cubicweb.web.views.editform import AutomaticEntityForm
   283             if not isinstance(categories, (list, tuple, set, frozenset)):
   255         return AutomaticEntityForm.relations_by_category(entity, categories, permission)
   284                 categories = (categories,)
       
   285             if not isinstance(categories, (set, frozenset)):
       
   286                 categories = frozenset(categories)
       
   287         eschema, rtags  = self.e_schema, self.rtags
       
   288         if self.has_eid():
       
   289             eid = self.eid
       
   290         else:
       
   291             eid = None
       
   292         for rschema, targetschemas, role in eschema.relation_definitions(True):
       
   293             if rschema in ('identity', 'has_text'):
       
   294                 continue
       
   295             # check category first, potentially lower cost than checking
       
   296             # permission which may imply rql queries
       
   297             if categories is not None:
       
   298                 targetschemas = [tschema for tschema in targetschemas
       
   299                                  if rtags.get_tags(rschema.type, tschema.type, role).intersection(categories)]
       
   300                 if not targetschemas:
       
   301                     continue
       
   302             tags = rtags.get_tags(rschema.type, role=role)
       
   303             if permission is not None:
       
   304                 # tag allowing to hijack the permission machinery when
       
   305                 # permission is not verifiable until the entity is actually
       
   306                 # created...
       
   307                 if eid is None and ('%s_on_new' % permission) in tags:
       
   308                     yield (rschema, targetschemas, role)
       
   309                     continue
       
   310                 if rschema.is_final():
       
   311                     if not rschema.has_perm(self.req, permission, eid):
       
   312                         continue
       
   313                 elif role == 'subject':
       
   314                     if not ((eid is None and rschema.has_local_role(permission)) or
       
   315                             rschema.has_perm(self.req, permission, fromeid=eid)):
       
   316                         continue
       
   317                     # on relation with cardinality 1 or ?, we need delete perm as well
       
   318                     # if the relation is already set
       
   319                     if (permission == 'add'
       
   320                         and rschema.cardinality(eschema, targetschemas[0], role) in '1?'
       
   321                         and self.has_eid() and self.related(rschema.type, role)
       
   322                         and not rschema.has_perm(self.req, 'delete', fromeid=eid,
       
   323                                                  toeid=self.related(rschema.type, role)[0][0])):
       
   324                         continue
       
   325                 elif role == 'object':
       
   326                     if not ((eid is None and rschema.has_local_role(permission)) or
       
   327                             rschema.has_perm(self.req, permission, toeid=eid)):
       
   328                         continue
       
   329                     # on relation with cardinality 1 or ?, we need delete perm as well
       
   330                     # if the relation is already set
       
   331                     if (permission == 'add'
       
   332                         and rschema.cardinality(targetschemas[0], eschema, role) in '1?'
       
   333                         and self.has_eid() and self.related(rschema.type, role)
       
   334                         and not rschema.has_perm(self.req, 'delete', toeid=eid,
       
   335                                                  fromeid=self.related(rschema.type, role)[0][0])):
       
   336                         continue
       
   337             yield (rschema, targetschemas, role)
       
   338 
   256 
   339     def srelations_by_category(self, categories=None, permission=None):
   257     def srelations_by_category(self, categories=None, permission=None):
   340         result = []
   258         result = []
   341         for rschema, ttypes, target in self.relations_by_category(categories,
   259         for rschema, ttypes, target in self.relations_by_category(categories,
   342                                                                   permission):
   260                                                                   permission):