[views] Add actions to trigger synchronization to CKAN instance on entity
authorDenis Laxalde <denis.laxalde@logilab.fr>
Mon, 26 Jan 2015 10:47:20 +0100
changeset 72 e0f76f26f987
parent 70 5ab8c18d8056
child 74 21d5a0da9b26
[views] Add actions to trigger synchronization to CKAN instance on entity Closes #4869952.
i18n/en.po
i18n/fr.po
views.py
--- a/i18n/en.po	Thu Jan 22 18:14:14 2015 +0100
+++ b/i18n/en.po	Mon Jan 26 10:47:20 2015 +0100
@@ -20,3 +20,12 @@
 # (no object form for final or symmetric relation types)
 msgid "ctxcomponents_ckanpublish.goto-ckan-dataset_description"
 msgstr ""
+
+msgid "synchronization to CKAN failed: "
+msgstr ""
+
+msgid "synchronization to CKAN successfully completed"
+msgstr ""
+
+msgid "synchronize to CKAN"
+msgstr ""
--- a/i18n/fr.po	Thu Jan 22 18:14:14 2015 +0100
+++ b/i18n/fr.po	Mon Jan 26 10:47:20 2015 +0100
@@ -20,3 +20,12 @@
 # (no object form for final or symmetric relation types)
 msgid "ctxcomponents_ckanpublish.goto-ckan-dataset_description"
 msgstr ""
+
+msgid "synchronization to CKAN failed: "
+msgstr "échec de la synchronisation vers CKAN : "
+
+msgid "synchronization to CKAN successfully completed"
+msgstr "synchronisation vers CKAN réussie"
+
+msgid "synchronize to CKAN"
+msgstr "synchroniser vers CKAN"
--- a/views.py	Thu Jan 22 18:14:14 2015 +0100
+++ b/views.py	Mon Jan 26 10:47:20 2015 +0100
@@ -18,10 +18,13 @@
 
 from logilab.mtconverter import xml_escape
 
-from cubicweb.predicates import score_entity
-from cubicweb.web import component
+from cubicweb.predicates import score_entity, adaptable
+from cubicweb.web import component, action, Redirect
+from cubicweb.view import EntityView
 
-from cubes.ckanpublish.utils import ckan_post, ckan_buildurl
+from cubes.ckanpublish.sobjects import CKANSyncError
+from cubes.ckanpublish.utils import (ckan_post, ckan_buildurl,
+                                     ckan_instance_configured, CKANPostError)
 
 
 _ = unicode
@@ -56,3 +59,67 @@
         url = ckan_buildurl(self._cw.vreg.config,
                             'dataset/' + xml_escape(r['name']))
         w(u'<a target=_blank href="%s">%s</a>' % (url, xml_escape(url)))
+
+
+class SyncDatasetToCKANMixin(object):
+    """Mixin for dataset-like entity synchronization to CKAN instance."""
+    __regid__ = 'ckanpublish.sync_dataset_to_ckan'
+    __select__ = adaptable('ICKANDataset') & ckan_instance_configured()
+
+
+class SyncDatasetToCKANAction(SyncDatasetToCKANMixin, action.Action):
+    """Trigger synchronization of a dataset-like entity to the CKAN instance.
+    """
+    title = _('synchronize to CKAN')
+
+    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 SyncDatasetToCKANView(SyncDatasetToCKANMixin, EntityView):
+    """Handle synchronization of a dataset-like entity to the CKAN instance.
+    """
+
+    def entity_call(self, entity):
+        try:
+            self._cw.call_service('ckanpublish.sync_dataset', eid=entity.eid)
+        except (CKANSyncError, CKANPostError) as exc:
+            msg = self._cw._('synchronization to CKAN failed: ' + str(exc))
+        else:
+            msg = self._cw._('synchronization to CKAN successfully completed')
+        raise Redirect(entity.absolute_url(__message=msg))
+
+
+class SyncResourceToCKANMixin(object):
+    """Mixin for dataset resource-like entity synchronization to CKAN
+    instance.
+    """
+    __regid__ = 'ckanpublish.sync_dataset_resource_to_ckan'
+    __select__ = adaptable('ICKANResource') & ckan_instance_configured()
+
+
+class SyncResourceToCKANAction(SyncResourceToCKANMixin, action.Action):
+    """Trigger synchronization of a dataset resource-like entity to the CKAN
+    instance.
+    """
+    title = _('synchronize to CKAN')
+
+    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 SyncResourceToCKANView(SyncResourceToCKANMixin, EntityView):
+    """Handle synchronization of dataset resource-like entity to the CKAN
+    instance.
+    """
+
+    def entity_call(self, entity):
+        try:
+            self._cw.call_service('ckanpublish.sync_resource', eid=entity.eid)
+        except (CKANSyncError, CKANPostError) as exc:
+            msg = self._cw._('synchronization to CKAN failed: ' + str(exc))
+        else:
+            msg = self._cw._('synchronization to CKAN successfully completed')
+        raise Redirect(entity.absolute_url(__message=msg))