[views] treeview for keyword hierarchy
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Sun, 19 Jul 2009 17:00:25 +0200
changeset 45 3190f3dffaa1
parent 44 9403b833592d
child 46 61dd37b6bd83
[views] treeview for keyword hierarchy
entities.py
hooks.py
views.py
--- a/entities.py	Sun Jul 19 16:59:50 2009 +0200
+++ b/entities.py	Sun Jul 19 17:00:25 2009 +0200
@@ -8,10 +8,12 @@
 
 from cubicweb.common.mixins import TreeMixIn
 from cubicweb.entities import AnyEntity, fetch_config
+from cubicweb.interfaces import ITree
 
 class Classification(AnyEntity):
     id = 'Classification'
     fetch_attrs, fetch_order = fetch_config(['name'])
+    __implements__ = AnyEntity.__implements__ + (ITree,)
 
     def parent(self):
         """overriden from TreeMixIn"""
@@ -25,6 +27,8 @@
 class Keyword(TreeMixIn, AnyEntity):
     id = 'Keyword'
     fetch_attrs, fetch_order = fetch_config(['name'])
+    __implements__ = AnyEntity.__implements__ + (ITree,)
+
     tree_attribute = 'subkeyword_of'
 
     @property
--- a/hooks.py	Sun Jul 19 16:59:50 2009 +0200
+++ b/hooks.py	Sun Jul 19 17:00:25 2009 +0200
@@ -157,12 +157,11 @@
     def precommit_event(self):
         session = self.session
         entity = self.entity
+        rql = 'SET X in_state ST WHERE ST name %(name)s, X is ET, ST state_of ET, X eid %(x)s'
         if session.user.is_in_group('managers'):
-            session.execute('SET X in_state ST WHERE ST name "keyword validated", X is ET, ST state_of ET, X eid %(x)s',
-                            {'x' : entity.eid}, 'x')
+            session.execute(rql, {'x': entity.eid, 'name': "keyword validated"}, 'x')
         else:
-            session.unsafe_execute('SET X in_state ST WHERE ST name "validation pending", X is ET, ST state_of ET, X eid %(x)s',
-                                   {'x' : entity.eid}, 'x')
+            session.unsafe_execute(rql, {'x': entity.eid, 'name': "validation pending"}, 'x')
 
 
 class AfterAddAnyEntity(Hook):
--- a/views.py	Sun Jul 19 16:59:50 2009 +0200
+++ b/views.py	Sun Jul 19 17:00:25 2009 +0200
@@ -9,7 +9,7 @@
 import simplejson
 
 from logilab.common.decorators import monkeypatch
-from logilab.mtconverter import html_escape
+from logilab.mtconverter import xml_escape
 
 from cubicweb.selectors import implements, rql_condition, relation_possible
 from cubicweb.view import EntityView
@@ -27,6 +27,22 @@
 uicfg.actionbox_appearsin_addmenu.tag_object_of(('Keyword', 'subkeyword_of', 'Keyword'), True)
 uicfg.autoform_section.tag_object_of(('Keyword', 'in_state', '*'), 'generated')
 
+uicfg.primaryview_section.tag_object_of(('*', 'included_in', 'Classification'), 'hidden')
+
+# classification views ########################################################
+
+class ClassificationPrimaryView(primary.PrimaryView):
+    __select__ = implements('Classification')
+
+    def render_entity_attributes(self, entity):
+        pass
+
+    def render_entity_relations(self, entity):
+        rset = self.req.execute('Any K ORDERBY N WHERE K included_in C, '
+                                'NOT K subkeyword_of KK, K name N, '
+                                'C eid %(x)s', {'x': entity.eid})
+        self.wview('treeview', rset, 'null')
+
 # keyword views ###############################################################
 
 class KeywordPrimaryView(primary.PrimaryView):
@@ -34,14 +50,22 @@
 
     def cell_call(self, row, col, **kwargs):
         entity = self.entity(row, col)
-        self.w(u'<h1 class="titleUnderline">%s [%s]</h1>' % (
-            html_escape(entity.name), html_escape(entity.state)))
-        rset = entity.related('applied_to')
-        if rset:
-            self.wview('list', rset)
-        else:
-            self.w(self.req._('no tagged object'))
+        self.w(u'<h1 class="titleUnderline">%s</h1>' % entity.dc_long_title())
+        rset = entity.related('subkeyword_of','object')
+        self.wview('treeview', rset, 'null')
+
+# class KeywordPrimaryViewBis(KeywordPrimaryView):
+#     __select__ = implements('Keyword') & relation_possible('applied_to', 'subject')
 
+#     def cell_call(self, row, col, **kwargs):
+#         entity = self.entity(row, col)
+#         super(KeywordPrimaryViewBis, self).cell_call(row, col, **kwargs)
+#         rset = entity.related('applied_to')
+#         if rset:
+#             self.w(u'<h2>%s</h2><div>' % self.req._('Tagged objects'))
+#             self.wview('list', rset)
+#         else:
+#             self.w(self.req._('no tagged object'))
 
 class KeywordOneLineView(baseviews.InContextView):
     __select__ = implements('Keyword')
@@ -52,10 +76,9 @@
             cssclass = u'class="needsvalidation"'
         else:
             cssclass = u''
-        self.w(u'<a href="%s" %s>%s</a>' % (html_escape(entity.absolute_url()),
+        self.w(u'<a href="%s" %s>%s</a>' % (xml_escape(entity.absolute_url()),
                                             cssclass,
-                                            html_escape(entity.name)))
-
+                                            xml_escape(entity.name)))
 
 class KeywordComboBoxView(TreePathMixIn, EntityView):
     """display keyword in edition's combobox"""
@@ -64,7 +87,6 @@
     item_vid = 'text'
     separator = u' > '
 
-
 # keyword component ###########################################################
 
 class KeywordBarVComponent(component.EntityVComponent):
@@ -95,7 +117,6 @@
             self.w(u'<div class="%s hidden" id="%s"></div>\n' % (
                 self.div_class(), self.div_id()))
 
-
 class AddKeywordVComponent(component.EntityVComponent):
     """the 'add keyword' component"""
     id = 'addkeywords'