[component] Add admin components
authorVincent Michel <vincent.michel@logilab.fr>
Wed, 17 Jul 2013 12:07:51 +0200
changeset 275 e52dd8d14a58
parent 274 2bebd692dedc
child 276 8425e2b8e2b2
[component] Add admin components
views/actions.py
views/components.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/actions.py	Wed Jul 17 12:07:51 2013 +0200
@@ -0,0 +1,110 @@
+# -*- coding: utf-8 -*-
+# copyright 2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2013 CEA (Saclay, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact@logilab.fr
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from cubicweb.web.action import Action
+from cubicweb.predicates import is_instance, nonempty_rset, anonymous_user
+from cubicweb.web.views.actions import (CopyAction, ModifyAction, DeleteAction,
+                                        ManagePermissionsAction, AddRelatedActions)
+
+
+from cubes.brainomics import ALL_MEASURES, COMMENTED_ENTITIES, WIKI_ENTITIES
+
+
+###############################################################################
+### ADMIN ACTIONS #############################################################
+###############################################################################
+class BrainomicsAbstractAdminAction(Action):
+    __abstract__ = True
+    __select__ = Action.__select__ & nonempty_rset() & ~anonymous_user()
+    category = 'action-admin'
+    icon = 'question-sign'
+    title = None    # action title, i18nable
+
+
+class BrainomicsWikiAdminAction(BrainomicsAbstractAdminAction):
+    __regid__ = 'ctx-admin-link-wiki'
+    __select__ = BrainomicsAbstractAdminAction.__select__ & is_instance(*WIKI_ENTITIES)
+    title = _('Update wiki')
+
+    def url(self):
+        entity = self.cw_rset.get_entity(0, 0)
+        return self._cw.build_url('add/Card', __linkto='wiki:%s:object' % entity.eid,
+                                  __redirectpath=entity.rest_path())
+
+
+class BrainomicsCommentAdminAction(BrainomicsAbstractAdminAction):
+    __regid__ = 'ctx-admin-link-comment'
+    __select__ = BrainomicsAbstractAdminAction.__select__ & is_instance(*COMMENTED_ENTITIES)
+    title = _('Add comment')
+
+    def url(self):
+        entity = self.cw_rset.get_entity(0, 0)
+        return self._cw.build_url('add/Comment', __linkto='comments:%s:subject' % entity.eid,
+                                  __redirectpath=entity.rest_path())
+
+# Add icon for cubicweb admin action
+ModifyAction.icon = 'edit'
+DeleteAction.icon = 'remove'
+ManagePermissionsAction.icon = 'eye-open'
+
+
+###############################################################################
+### DOWNLOAD ACTIONS ##########################################################
+###############################################################################
+class BrainomicsAbstractDownloadAction(Action):
+    __abstract__ = True
+    __select__ = Action.__select__ & nonempty_rset()
+    category = 'download-links'
+
+
+class ScanZipFileBox(BrainomicsAbstractDownloadAction):
+    __regid__ = 'scan_zipfile'
+    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance('Scan', 'GenomicMeasure')
+    title = _('Download Zip')
+    download_vid = 'data-zip'
+
+
+class CsvFileBox(BrainomicsAbstractDownloadAction):
+    __regid__ = 'measure_csvfile'
+    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance('Subject', 'Scan',
+                                                                            'ScoreDef',
+                                                                            'QuestionnaireRun', 'Answer',
+                                                                            'GenomicMeasure', 'Snp')
+    title = _('Download CSV')
+    download_vid = 'csvexport'
+
+
+class XcedeBox(BrainomicsAbstractDownloadAction):
+    __regid__ = 'xcede_file'
+    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance('Subject', 'Scan',
+                                                                            'GenomicMeasure',
+                                                                            'QuestionnaireRun',
+                                                                            'Questionnaire')
+    title = _('Download Xcede')
+    download_vid = 'xcede'
+
+
+
+###############################################################################
+### REGISTRATION CALLBACK #####################################################
+###############################################################################
+def registration_callback(vreg):
+    vreg.register_all(globals().values(), __name__, ())
+    # Desactivate some actions for now
+    vreg.unregister(CopyAction)
+    vreg.unregister(AddRelatedActions)
--- a/views/components.py	Wed Jul 17 11:37:10 2013 +0200
+++ b/views/components.py	Wed Jul 17 12:07:51 2013 +0200
@@ -31,7 +31,7 @@
 ################################################################################
 ### SEARCHBOX ##################################################################
 ################################################################################
-class SearchBoxBrainomics(SearchBox):
+class BrainomicsSearchBox(SearchBox):
     """display a box with a simple search form
     """
     context = _('header-center')
@@ -74,6 +74,7 @@
         if not links:
             return
         w(u'<div class="well">')
+        w(u'<h4>%s</h4>' % self._cw._('Related entities'))
         w(u'<div class="btn-toolbar">')
         # Render links
         for link in links:
@@ -234,91 +235,85 @@
         card_rset = self._cw.execute('Any X WHERE X is Card, Q wiki X, Q eid %(eid)s',
                                 {'eid': entity.eid})
         if card_rset:
-            w(self._cw.view(rset=card_rset))
+            w(self._cw.view('documentation_card', rset=card_rset))
         else:
             w(u'<div>%s</div>' % self._cw._('No documentation available'))
 
 
 ###############################################################################
-### DOWNLOAD BOXES ############################################################
+### ADMIN ACTION/BOX ##########################################################
+###############################################################################
+class BrainomicsEditBox(EditBox):
+    context = 'left'
+    order = -10
+
+    def init_rendering(self):
+        pass
+
+    def get_actions(self):
+        actions = self._cw.vreg['actions'].possible_actions(self._cw, self.cw_rset)
+        all_actions = []
+        for category in ('mainactions', 'moreactions', 'action-admin'):
+            for action in actions.get(category, ()):
+                all_actions.append(action)
+        return all_actions
+
+    def render(self, w, **kwargs):
+        actions = self.get_actions()
+        if actions:
+            w(u'<div class="well">')
+            w(u'<h4>%s</h4>' % self._cw._('Administration tools'))
+            for action in actions:
+                if hasattr(action, 'title'):
+                    icon = getattr(action, 'icon', 'edit')
+                    title = self._cw._(action.title)
+                    w(u'<a class="btn btn-primary btn-block" href="%s">' % action.url())
+                    w(u'<i class="icon-%s icon-white"></i>%s</a>' % (icon, title))
+            w(u'</div>')
+
+
+###############################################################################
+### DOWNLOAD BOX ##############################################################
 ###############################################################################
 class BrainomicsAbstractDownloadBox(component.CtxComponent):
     __select__ = component.CtxComponent.__select__ & nonempty_rset()
     context = 'left'
-    always_available = False
     order = 1
     __regid__ = 'ctx-download-box'
 
     def render(self, w, **kwargs):
-        demo = self._cw.vreg.config.get('demo-site')
-        if demo and not self.always_available:
-            return
-        else:
-            links = list(self._cw.vreg['ctx-download-links'].possible_objects(self._cw,
-                                                                              rset=self.cw_rset))
-            if links:
-                w(u'<div class="well">')
-                for link in links:
-                    url = self._cw.build_url(rql=self.cw_rset.printable_rql(), vid=link.download_vid)
-                    _id = link.download_vid
-                    w(u'<a class="btn btn-primary btn-block download-ctx" id="%s" href="%s">'
-                      % (_id, url))
-                    w(u'<i class="icon-download icon-white"></i>%s</a>'
-                      % xml_escape(self._cw._(link.title)))
-                w(u'</div>')
-                # Add on load the rql facet change
-                self._cw.add_onload("""$(cw).bind('facets-content-loaded',
-                cw.cubes.brainomics.changeDownloadUrls);""")
-
-
-class BrainomicsAbstractDownloadLink(component.CtxComponent):
-    __abstract__ = True
-    __select__ = component.CtxComponent.__select__ & nonempty_rset()
-    context = 'left'
-    always_available = False
-    __registry__ = 'ctx-download-links'
-
-
-class ScanZipFileBox(BrainomicsAbstractDownloadLink):
-    __regid__ = 'scan_zipfile'
-    __select__ = BrainomicsAbstractDownloadLink.__select__  & is_instance('Scan', 'GenomicMeasure')
-    title = _('Download Zip')
-    download_vid = 'data-zip'
-
-
-class CsvFileBox(BrainomicsAbstractDownloadLink):
-    __regid__ = 'measure_csvfile'
-    __select__ = component.CtxComponent.__select__  & is_instance('Subject', 'Scan',
-                                                                  'ScoreDef',
-                                                                  'QuestionnaireRun', 'Answer',
-                                                                  'GenomicMeasure', 'Snp')
-    title = _('Download CSV')
-    always_available = True
-    download_vid = 'csvexport'
-
-
-class XcedeBox(BrainomicsAbstractDownloadLink):
-    __regid__ = 'xcede_file'
-    __select__ = component.CtxComponent.__select__  & is_instance('Subject', 'Scan', 'GenomicMeasure',
-                                                                  'QuestionnaireRun', 'Questionnaire')
-    title = _('Download Xcede')
-    always_available = True
-    download_vid = 'xcede'
+        possible_actions = self._cw.vreg['actions'].possible_actions(self._cw, self.cw_rset)
+        links = list(possible_actions['download-links'])
+        if links:
+            w(u'<div class="well">')
+            w(u'<h4>%s</h4>' % self._cw._('Download tools'))
+            for link in links:
+                url = self._cw.build_url(rql=self.cw_rset.printable_rql(), vid=link.download_vid)
+                _id = link.__regid__
+                w(u'<a class="btn btn-primary btn-block download-ctx" id="%s" href="%s">'
+                  % (_id, url))
+                w(u'<i class="icon-download icon-white"></i>%s</a>' % self._cw._(link.title))
+            w(u'</div>')
+            # Add on load the rql facet change
+            self._cw.add_onload("""$(cw).bind('facets-content-loaded',
+            cw.cubes.brainomics.changeDownloadUrls);""")
 
 
 ###############################################################################
 ### REGISTRATION CALLBACK #####################################################
 ###############################################################################
 BookmarksBox.__select__ = BookmarksBox.__select__ & ~anonymous_user()
-EditBox.__select__ = EditBox.__select__ & ~anonymous_user()
 
 def registration_callback(vreg):
-    vreg.register_all(globals().values(), __name__, (SearchBoxBrainomics,))
-    vreg.register_and_replace(SearchBoxBrainomics, SearchBox)
+    vreg.register_all(globals().values(), __name__, (BrainomicsSearchBox, BrainomicsEditBox))
+    vreg.register_and_replace(BrainomicsSearchBox, SearchBox)
+    vreg.register_and_replace(BrainomicsEditBox, EditBox)
     # Unregister breadcrumbs
     from cubicweb.web.views.ibreadcrumbs import (BreadCrumbEntityVComponent,
+                                                 BreadCrumbLinkToVComponent,
                                                  BreadCrumbAnyRSetVComponent,
                                                  BreadCrumbETypeVComponent)
     vreg.unregister(BreadCrumbEntityVComponent)
     vreg.unregister(BreadCrumbAnyRSetVComponent)
     vreg.unregister(BreadCrumbETypeVComponent)
+    vreg.unregister(BreadCrumbLinkToVComponent)