provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces) tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Mon, 16 Feb 2009 14:18:13 +0100
branchtls-sprint
changeset 629 59b6542f5729
parent 628 3a6f28a1ea21
child 630 66ff0b2f7d03
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
schema.py
schemas/base.py
server/test/data/schema/Affaire.py
--- a/schema.py	Mon Feb 16 14:08:16 2009 +0100
+++ b/schema.py	Mon Feb 16 14:18:13 2009 +0100
@@ -840,7 +840,34 @@
         
 PyFileReader.context['RRQLExpression'] = RRQLExpression
 
-        
+# workflow extensions #########################################################
+
+class workflowable_definition(ybo.metadefinition):
+    """extends default EntityType's metaclass to add workflow relations
+    (i.e. in_state and wf_info_for).
+    This is the default metaclass for WorkflowableEntityType
+    """
+    def __new__(mcs, name, bases, classdict):
+        abstract = classdict.pop('abstract', False)
+        defclass = super(workflowable_definition, mcs).__new__(mcs, name, bases, classdict)
+        if not abstract:
+            existing_rels = set(rdef.name for rdef in defclass.__relations__)
+            if 'in_state' not in existing_rels and 'wf_info_for' not in existing_rels:
+                in_state = ybo.SubjectRelation('State', cardinality='1*',
+                                               # XXX automatize this
+                                               constraints=[RQLConstraint('S is ET, O state_of ET')],
+                                               description=_('account state'))
+                yams_add_relation(defclass.__relations__, in_state, 'in_state')
+                wf_info_for = ybo.ObjectRelation('TrInfo', cardinality='1*', composite='object')
+                yams_add_relation(defclass.__relations__, wf_info_for, 'wf_info_for')
+        return defclass
+
+class WorkflowableEntityType(ybo.EntityType):
+    __metaclass__ = workflowable_definition
+    abstract = True
+    
+PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType
+
 # schema loading ##############################################################
 
 class CubicWebRelationFileReader(RelationFileReader):
--- a/schemas/base.py	Mon Feb 16 14:08:16 2009 +0100
+++ b/schemas/base.py	Mon Feb 16 14:18:13 2009 +0100
@@ -9,8 +9,9 @@
 from cubicweb.schema import format_constraint
 
 
-class EUser(RestrictedEntityType):
+class EUser(WorkflowableEntityType):
     """define a CubicWeb user"""
+    meta = True # XXX backported from old times, shouldn't be there anymore
     permissions = {
         'read':   ('managers', 'users', ERQLExpression('X identity U')),
         'add':    ('managers',),
@@ -33,11 +34,6 @@
     in_group = SubjectRelation('EGroup', cardinality='+*',
                                constraints=[RQLConstraint('NOT O name "owners"')],
                                description=_('groups grant permissions to the user'))
-    in_state = SubjectRelation('State', cardinality='1*',
-                               # XXX automatize this
-                               constraints=[RQLConstraint('S is ET, O state_of ET')],
-                               description=_('account state'))
-    wf_info_for = ObjectRelation('TrInfo', cardinality='1*', composite='object')
 
 
 class EmailAddress(MetaEntityType):
--- a/server/test/data/schema/Affaire.py	Mon Feb 16 14:08:16 2009 +0100
+++ b/server/test/data/schema/Affaire.py	Mon Feb 16 14:18:13 2009 +0100
@@ -1,6 +1,6 @@
 from cubicweb.schema import format_constraint
 
-class Affaire(EntityType):
+class Affaire(WorkflowableEntityType):
     permissions = {
         'read':   ('managers', 
                    ERQLExpression('X owned_by U'), ERQLExpression('X concerne S?, S owned_by U')),
@@ -13,9 +13,6 @@
                  constraints=[SizeConstraint(16)])
     sujet = String(fulltextindexed=True,
                    constraints=[SizeConstraint(256)])
-    in_state = SubjectRelation('State', cardinality='1*',
-                               constraints=[RQLConstraint('O state_of ET, ET name "Affaire"')],
-                               description=_('account state'))
     descr_format = String(meta=True, internationalizable=True,
                                 default='text/rest', constraints=[format_constraint])
     descr = String(fulltextindexed=True,
@@ -23,8 +20,7 @@
 
     duration = Int()
     invoiced = Int()
-        
-    wf_info_for = ObjectRelation('TrInfo', cardinality='1*', composite='object')
+
     depends_on = SubjectRelation('Affaire')
     require_permission = SubjectRelation('EPermission')