[schema] add workflow on BlogEntry so that anonymous could only see published blog entries
authorSandrine Ribeau <sandrine.ribeau@logilab.fr>
Tue, 20 Oct 2009 08:15:56 -0700
changeset 132 c31eb0c07ac6
parent 130 38eda314be39
child 133 c806a41eecf2
[schema] add workflow on BlogEntry so that anonymous could only see published blog entries
migration/1.7.0_Any.py
migration/postcreate.py
schema.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/migration/1.7.0_Any.py	Tue Oct 20 08:15:56 2009 -0700
@@ -0,0 +1,22 @@
+add_relation_definition('BlogEntry',  'in_state', 'State')
+add_relation_definition('TrInfo',  'wf_info_for', 'BlogEntry')
+add_relation_definition('BlogEntry',  'custom_workflow', 'Workflow')
+
+
+# add BlogEntry workflow
+bwf = add_workflow(_('default BlogEntry workflow'), 'BlogEntry')
+
+draft = bwf.add_state(_('draft'), initial=True)
+published = bwf.add_state(_('published'))
+
+publish = bwf.add_transition(_('publish'), draft, published,
+                             ('managers',))
+checkpoint()
+
+# set state to published for already existing blog entries
+blogentries = rql('Any B WHERE B is BlogEntry')
+
+for blogentry in blogentries:
+    session.unsafe_execute('SET B in_state S WHERE S name "published", B eid %(b)s', {'b': blogentry[0]})
+
+checkpoint()
--- a/migration/postcreate.py	Mon Oct 12 13:16:14 2009 +0200
+++ b/migration/postcreate.py	Tue Oct 20 08:15:56 2009 -0700
@@ -1,2 +1,11 @@
 # postcreate script. You could setup a workflow here for example
 
+# BlogEntry workflow
+bwf = add_workflow(_('default BlogEntry workflow'), 'BlogEntry')
+
+draft = bwf.add_state(_('draft'), initial=True)
+published = bwf.add_state(_('published'))
+
+publish = bwf.add_transition(_('publish'), draft, published,
+                             ('managers',))
+
--- a/schema.py	Mon Oct 12 13:16:14 2009 +0200
+++ b/schema.py	Tue Oct 20 08:15:56 2009 -0700
@@ -1,4 +1,5 @@
 from yams.buildobjs import EntityType, String, SubjectRelation
+from cubicweb.schema import WorkflowableEntityType
 try:
     from yams.buildobjs import RichString
 except ImportError:
@@ -11,7 +12,13 @@
     rss_url = String(maxsize=128, description=_('blog\'s rss url (useful for when using external site such as feedburner)'))
 
 
-class BlogEntry(EntityType):
+class BlogEntry(WorkflowableEntityType):
+    permissions = {
+        'read': ('managers', 'users', ERQLExpression('X in_state S, S name "published"'),),
+        'add': ('managers', 'users'),
+        'update': ('managers', 'owners'),
+        'delete': ('managers', 'owners')
+        }
     title = String(required=True, fulltextindexed=True, maxsize=256)
     content = RichString(required=True, fulltextindexed=True)
     entry_of = SubjectRelation('Blog', cardinality='**')