action, view and service so managers can start source synchronization from the web ui
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Dec 2015 16:15:00 +0100
changeset 10951 ef1cfc80d51c
parent 10950 282880f81311
child 10956 208c9ac8edbb
action, view and service so managers can start source synchronization from the web ui Closes #5474286
server/test/unittest_migractions.py
sobjects/services.py
web/views/cwsources.py
--- a/server/test/unittest_migractions.py	Mon Nov 30 14:54:30 2015 +0100
+++ b/server/test/unittest_migractions.py	Wed Dec 09 16:15:00 2015 +0100
@@ -526,12 +526,12 @@
             # remaining orphan rql expr which should be deleted at commit (composite relation)
             # unattached expressions -> pending deletion on commit
             self.assertEqual(cnx.execute('Any COUNT(X) WHERE X is RQLExpression, X exprtype "ERQLExpression",'
-                                            'NOT ET1 read_permission X, NOT ET2 add_permission X, '
-                                            'NOT ET3 delete_permission X, NOT ET4 update_permission X')[0][0],
+                                         'NOT ET1 read_permission X, NOT ET2 add_permission X, '
+                                         'NOT ET3 delete_permission X, NOT ET4 update_permission X')[0][0],
                               7)
             self.assertEqual(cnx.execute('Any COUNT(X) WHERE X is RQLExpression, X exprtype "RRQLExpression",'
-                                            'NOT ET1 read_permission X, NOT ET2 add_permission X, '
-                                            'NOT ET3 delete_permission X, NOT ET4 update_permission X')[0][0],
+                                         'NOT ET1 read_permission X, NOT ET2 add_permission X, '
+                                         'NOT ET3 delete_permission X, NOT ET4 update_permission X')[0][0],
                               2)
             # finally
             self.assertEqual(cnx.execute('Any COUNT(X) WHERE X is RQLExpression')[0][0],
--- a/sobjects/services.py	Mon Nov 30 14:54:30 2015 +0100
+++ b/sobjects/services.py	Wed Dec 09 16:15:00 2015 +0100
@@ -22,6 +22,7 @@
 from six import text_type
 
 from yams.schema import role_name
+
 from cubicweb import ValidationError
 from cubicweb.server import Service
 from cubicweb.predicates import match_user_groups, match_kwargs
@@ -60,6 +61,7 @@
         results['threads'] = [t.name for t in threading.enumerate()]
         return results
 
+
 class GcStatsService(Service):
     """Return a dictionary containing some statistics about the repository
     resources usage.
@@ -156,3 +158,17 @@
                         'WHERE U login %(login)s', d, build_descr=False)
 
         return user
+
+
+class SourceSynchronizationService(Service):
+    """Force synchronization of a datafeed source"""
+    __regid__ = 'source-sync'
+    __select__ = Service.__select__ & match_user_groups('managers')
+
+    def call(self, source_eid):
+        source_entity = self._cw.entity_from_eid(source_eid)
+        repo = self._cw.repo # Service are repo side only.
+        with repo.internal_cnx() as cnx:
+            source = repo.sources_by_uri[source_entity.name]
+            source.pull_data(cnx)
+
--- a/web/views/cwsources.py	Mon Nov 30 14:54:30 2015 +0100
+++ b/web/views/cwsources.py	Wed Dec 09 16:15:00 2015 +0100
@@ -33,10 +33,10 @@
 from cubicweb import Unauthorized, tags
 from cubicweb.utils import make_uid
 from cubicweb.predicates import (is_instance, score_entity, has_related_entities,
-                                match_user_groups, match_kwargs, match_view)
+                                 match_user_groups, match_kwargs, match_view, one_line_rset)
 from cubicweb.view import EntityView, StartupView
 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, display_name
-from cubicweb.web import formwidgets as wdgs, facet
+from cubicweb.web import Redirect, formwidgets as wdgs, facet, action
 from cubicweb.web.views import add_etype_button
 from cubicweb.web.views import (uicfg, tabs, actions, ibreadcrumbs, navigation,
                                 tableview, pyviews)
@@ -226,6 +226,36 @@
     layout_args = {'display_filter': 'top'}
 
 
+class CWSourceSyncAction(action.Action):
+    __regid__ = 'cw.source-sync'
+    __select__ = (action.Action.__select__ & match_user_groups('managers')
+                  & one_line_rset() & is_instance('CWSource')
+                  & score_entity(lambda x: x.name != 'system'))
+
+    title = _('synchronize')
+    category = 'mainactions'
+    order = 20
+
+    def url(self):
+        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        return entity.absolute_url(vid=self.__regid__)
+
+
+class CWSourceSyncView(EntityView):
+    __regid__ = 'cw.source-sync'
+    __select__ = (match_user_groups('managers')
+                  & one_line_rset() & is_instance('CWSource')
+                  & score_entity(lambda x: x.name != 'system'))
+
+    title = _('synchronize')
+
+    def entity_call(self, entity):
+        self._cw.call_service('source-sync', source_eid=entity.eid)
+        msg = self._cw._('Source has been synchronized')
+        url = entity.absolute_url(tab='cwsource-imports', __message=msg)
+        raise Redirect(url)
+
+
 
 
 # sources management view ######################################################