entities/schemaobjs.py
brancholdstable
changeset 4985 02b52bf9f5f8
parent 4839 f482dbdf2f8c
child 5030 5238d9a8dfee
equal deleted inserted replaced
4563:c25da7573ebd 4985:02b52bf9f5f8
    14 
    14 
    15 from cubicweb.entities import AnyEntity, fetch_config
    15 from cubicweb.entities import AnyEntity, fetch_config
    16 
    16 
    17 
    17 
    18 class CWEType(AnyEntity):
    18 class CWEType(AnyEntity):
    19     id = 'CWEType'
    19     __regid__ = 'CWEType'
    20     fetch_attrs, fetch_order = fetch_config(['name'])
    20     fetch_attrs, fetch_order = fetch_config(['name'])
    21 
    21 
    22     def dc_title(self):
    22     def dc_title(self):
    23         return u'%s (%s)' % (self.name, self.req._(self.name))
    23         return u'%s (%s)' % (self.name, self._cw._(self.name))
    24 
    24 
    25     def dc_long_title(self):
    25     def dc_long_title(self):
    26         stereotypes = []
    26         stereotypes = []
    27         _ = self.req._
    27         _ = self._cw._
    28         if self.final:
    28         if self.final:
    29             stereotypes.append(_('final'))
    29             stereotypes.append(_('final'))
    30         if stereotypes:
    30         if stereotypes:
    31             return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes))
    31             return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes))
    32         return self.dc_title()
    32         return self.dc_title()
    35         """XXX goa specific"""
    35         """XXX goa specific"""
    36         return self.get('name')
    36         return self.get('name')
    37 
    37 
    38 
    38 
    39 class CWRType(AnyEntity):
    39 class CWRType(AnyEntity):
    40     id = 'CWRType'
    40     __regid__ = 'CWRType'
    41     fetch_attrs, fetch_order = fetch_config(['name'])
    41     fetch_attrs, fetch_order = fetch_config(['name'])
    42 
    42 
    43     def dc_title(self):
    43     def dc_title(self):
    44         return u'%s (%s)' % (self.name, self.req._(self.name))
    44         return u'%s (%s)' % (self.name, self._cw._(self.name))
    45 
    45 
    46     def dc_long_title(self):
    46     def dc_long_title(self):
    47         stereotypes = []
    47         stereotypes = []
    48         _ = self.req._
    48         _ = self._cw._
    49         if self.symetric:
    49         if self.symmetric:
    50             stereotypes.append(_('symetric'))
    50             stereotypes.append(_('symmetric'))
    51         if self.inlined:
    51         if self.inlined:
    52             stereotypes.append(_('inlined'))
    52             stereotypes.append(_('inlined'))
    53         if self.final:
    53         if self.final:
    54             stereotypes.append(_('final'))
    54             stereotypes.append(_('final'))
    55         if stereotypes:
    55         if stereotypes:
    56             return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes))
    56             return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes))
    57         return self.dc_title()
    57         return self.dc_title()
    58 
    58 
    59     def inlined_changed(self, inlined):
    59     def check_inlined_allowed(self):
    60         """check inlining is necessary and possible:
    60         """check inlining is possible, raise ValidationError if not possible
    61 
    61         """
    62         * return False if nothing has changed
    62         # don't use the persistent schema, we may miss cardinality changes
    63         * raise ValidationError if inlining is'nt possible
    63         # in the same transaction
    64         * eventually return True
    64         for rdef in self.reverse_relation_type:
    65         """
    65             card = rdef.cardinality[0]
    66         rschema = self.schema.rschema(self.name)
    66             if not card in '?1':
    67         if inlined == rschema.inlined:
    67                 rtype = self.name
    68             return False
    68                 stype = rdef.stype
    69         if inlined:
    69                 otype = rdef.otype
    70             # don't use the persistent schema, we may miss cardinality changes
    70                 msg = self._cw._("can't set inlined=%(inlined)s, "
    71             # in the same transaction
    71                                  "%(stype)s %(rtype)s %(otype)s "
    72             for rdef in self.reverse_relation_type:
    72                                  "has cardinality=%(card)s")
    73                 card = rdef.cardinality[0]
    73                 raise ValidationError(self.eid, {'inlined': msg % locals()})
    74                 if not card in '?1':
       
    75                     rtype = self.name
       
    76                     stype = rdef.stype
       
    77                     otype = rdef.otype
       
    78                     msg = self.req._("can't set inlined=%(inlined)s, "
       
    79                                      "%(stype)s %(rtype)s %(otype)s "
       
    80                                      "has cardinality=%(card)s")
       
    81                     raise ValidationError(self.eid, {'inlined': msg % locals()})
       
    82         return True
       
    83 
    74 
    84     def db_key_name(self):
    75     def db_key_name(self):
    85         """XXX goa specific"""
    76         """XXX goa specific"""
    86         return self.get('name')
    77         return self.get('name')
    87 
    78 
    88 
    79 
    89 class CWRelation(AnyEntity):
    80 class CWRelation(AnyEntity):
    90     id = 'CWRelation'
    81     __regid__ = 'CWRelation'
    91     fetch_attrs = fetch_config(['cardinality'])[0]
    82     fetch_attrs = fetch_config(['cardinality'])[0]
    92 
    83 
    93     def dc_title(self):
    84     def dc_title(self):
    94         return u'%s %s %s' % (
    85         return u'%s %s %s' % (
    95             self.from_entity[0].name,
    86             self.from_entity[0].name,
   128     def otype(self):
   119     def otype(self):
   129         return self.to_entity[0]
   120         return self.to_entity[0]
   130 
   121 
   131 
   122 
   132 class CWAttribute(CWRelation):
   123 class CWAttribute(CWRelation):
   133     id = 'CWAttribute'
   124     __regid__ = 'CWAttribute'
   134 
   125 
   135     def dc_long_title(self):
   126     def dc_long_title(self):
   136         card = self.cardinality
   127         card = self.cardinality
   137         scard = u''
   128         scard = u''
   138         if card[0] == '1':
   129         if card[0] == '1':
   142             scard, self.relation_type[0].name,
   133             scard, self.relation_type[0].name,
   143             self.to_entity[0].name)
   134             self.to_entity[0].name)
   144 
   135 
   145 
   136 
   146 class CWConstraint(AnyEntity):
   137 class CWConstraint(AnyEntity):
   147     id = 'CWConstraint'
   138     __regid__ = 'CWConstraint'
   148     fetch_attrs, fetch_order = fetch_config(['value'])
   139     fetch_attrs, fetch_order = fetch_config(['value'])
   149 
   140 
   150     def dc_title(self):
   141     def dc_title(self):
   151         return '%s(%s)' % (self.cstrtype[0].name, self.value or u'')
   142         return '%s(%s)' % (self.cstrtype[0].name, self.value or u'')
   152 
   143 
   162     def type(self):
   153     def type(self):
   163         return self.cstrtype[0].name
   154         return self.cstrtype[0].name
   164 
   155 
   165 
   156 
   166 class RQLExpression(AnyEntity):
   157 class RQLExpression(AnyEntity):
   167     id = 'RQLExpression'
   158     __regid__ = 'RQLExpression'
   168     fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression'])
   159     fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression'])
   169 
   160 
   170     def dc_title(self):
   161     def dc_title(self):
   171         return '%s(%s)' % (self.exprtype, self.expression or u'')
   162         return '%s(%s)' % (self.exprtype, self.expression or u'')
   172 
   163 
   196             return self.expression_of.rest_path(), {}
   187             return self.expression_of.rest_path(), {}
   197         return super(RQLExpression, self).after_deletion_path()
   188         return super(RQLExpression, self).after_deletion_path()
   198 
   189 
   199 
   190 
   200 class CWPermission(AnyEntity):
   191 class CWPermission(AnyEntity):
   201     id = 'CWPermission'
   192     __regid__ = 'CWPermission'
   202     fetch_attrs, fetch_order = fetch_config(['name', 'label'])
   193     fetch_attrs, fetch_order = fetch_config(['name', 'label'])
   203 
   194 
   204     def dc_title(self):
   195     def dc_title(self):
   205         if self.label:
   196         if self.label:
   206             return '%s (%s)' % (self.req._(self.name), self.label)
   197             return '%s (%s)' % (self._cw._(self.name), self.label)
   207         return self.req._(self.name)
   198         return self._cw._(self.name)
   208 
   199 
   209     def after_deletion_path(self):
   200     def after_deletion_path(self):
   210         """return (path, parameters) which should be used as redirect
   201         """return (path, parameters) which should be used as redirect
   211         information when this entity is being deleted
   202         information when this entity is being deleted
   212         """
   203         """