[ckan-changes] commit/ckan: kindly: [plugin] add extension points for url change

Bitbucket commits-noreply at bitbucket.org
Mon Oct 17 12:32:55 UTC 2011


1 new changeset in ckan:

http://bitbucket.org/okfn/ckan/changeset/cc4ac97552e0/
changeset:   cc4ac97552e0
branch:      feature-1371-task-status-logic-layer
user:        kindly
date:        2011-10-17 14:32:39
summary:     [plugin] add extension points for url change
affected #:  7 files (-1 bytes)

--- a/ckan/lib/dictization/model_save.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/lib/dictization/model_save.py	Mon Oct 17 13:32:39 2011 +0100
@@ -1,12 +1,15 @@
 from ckan.lib.dictization import table_dict_save
 from sqlalchemy.orm import class_mapper
 from ckan.lib.helpers import json
+from ckan.plugins import (PluginImplementations,
+                          IResourceUrlChange)
 
 ##package saving
 
 def resource_dict_save(res_dict, context):
     model = context["model"]
     session = context["session"]
+    trigger_url_change = False
 
     # try to get resource object directly from context, then by ID
     # if not found, create a new resource object
@@ -15,7 +18,10 @@
     if (not obj) and id:
         obj = session.query(model.Resource).get(id)
     if not obj:
+        new = True
         obj = model.Resource()
+    else:
+        new = False
 
     table = class_mapper(model.Resource).mapped_table
     fields = [field.name for field in table.c]
@@ -26,6 +32,8 @@
         if key in ('extras', 'revision_timestamp'):
             continue
         if key in fields:
+            if key == 'url' and not new and obj.url <> value:
+                trigger_url_change = True 
             setattr(obj, key, value)
         else:
             # resources save extras directly onto the object, instead
@@ -38,6 +46,10 @@
     else:
         obj.state = u'active'
 
+    if trigger_url_change:
+        for item in PluginImplementations(IResourceUrlChange):
+            item.notify(obj)
+
     session.add(obj)
     return obj
 


--- a/ckan/lib/search/__init__.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/lib/search/__init__.py	Mon Oct 17 13:32:39 2011 +0100
@@ -1,10 +1,11 @@
 import logging
-from pylons import config
+from pylons import config, c
 
 from ckan import model
 from ckan.model import DomainObjectOperation
 from ckan.plugins import SingletonPlugin, implements, IDomainObjectModification
-from ckan.lib.dictization.model_dictize import package_to_api1
+from ckan.logic import get_action
+
 from common import SearchIndexError, SearchError, make_connection, is_available, DEFAULT_SOLR_URL
 from index import PackageSearchIndex, NoopSearchIndex
 from query import TagSearchQuery, ResourceSearchQuery, PackageSearchQuery, QueryOptions, convert_legacy_parameters_to_solr
@@ -88,10 +89,17 @@
     implements(IDomainObjectModification, inherit=True)
 
     def notify(self, entity, operation):
+        if not isinstance(entity, model.Package):
+            return
         if operation != DomainObjectOperation.deleted:
-            dispatch_by_operation(entity.__class__.__name__, 
-                                  package_to_api1(entity, {'model': model}),
-                                  operation)
+            dispatch_by_operation(
+                entity.__class__.__name__, 
+                get_action('package_show_rest')(
+                    {'model': model, 'ignore_auth': True},
+                    {'id': entity.id}
+                ),
+                operation
+            )
         elif operation == DomainObjectOperation.deleted:
             dispatch_by_operation(entity.__class__.__name__, 
                                   {'id': entity.id}, operation)
@@ -106,7 +114,12 @@
     package_index = index_for(model.Package)
     package_index.clear()
     for pkg in model.Session.query(model.Package).all():
-        package_index.insert_entity(pkg)
+        package_index.insert_dict(
+            get_action('package_show_rest')(
+                {'model': model, 'ignore_auth': True},
+                {'id': pkg.id}
+            )
+        )
     model.Session.commit()
 
 def check():


--- a/ckan/lib/search/index.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/lib/search/index.py	Mon Oct 17 13:32:39 2011 +0100
@@ -50,27 +50,14 @@
         """ Insert new data from a dictionary. """
         return self.update_dict(data)
         
-    def insert_entity(self, entity):
-        """ Insert new data from a domain object. """
-        return self.insert_dict(entity.as_dict())
-    
     def update_dict(self, data):
         """ Update data from a dictionary. """
         log.debug("NOOP Index: %s" % ",".join(data.keys()))
     
-    def update_entity(self, entity):
-        """ Update data from a domain object. """
-        # in convention we trust:
-        return self.update_dict(entity.as_dict())
-    
     def remove_dict(self, data):
         """ Delete an index entry uniquely identified by ``data``. """
         log.debug("NOOP Delete: %s" % ",".join(data.keys()))
         
-    def remove_entity(self, entity):
-        """ Delete ``entity``. """
-        return self.remove_dict(entity.as_dict())
-        
     def clear(self):
         """ Delete the complete index. """
         clear_index()


--- a/ckan/logic/__init__.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/logic/__init__.py	Mon Oct 17 13:32:39 2011 +0100
@@ -121,7 +121,8 @@
 def check_access_old(entity, action, context):
     model = context['model']
     user = context.get('user')
-
+    if context.get('ignore_auth'):
+        return True
     log.debug('check access - user %r, action %s' % (user,action))
     if action and entity and not isinstance(entity, model.PackageRelationship):
         if action != model.Action.READ and user == '':


--- a/ckan/logic/auth/get.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/logic/auth/get.py	Mon Oct 17 13:32:39 2011 +0100
@@ -12,7 +12,7 @@
     ./ckan/controllers/api.py
     """
     model = context['model']
-    user = context['user']
+    user = context.get('user')
     if not Authorizer().is_authorized(user, model.Action.SITE_READ, model.System):
         return {'success': False, 'msg': _('Not authorized to see this page')}
 
@@ -63,7 +63,7 @@
 
 def package_relationships_list(context, data_dict):
     model = context['model']
-    user = context['user']
+    user = context.get('user')
 
     id = data_dict['id']
     id2 = data_dict.get('id2')
@@ -81,7 +81,7 @@
 
 def package_show(context, data_dict):
     model = context['model']
-    user = context['user']
+    user = context.get('user')
     package = get_package_object(context, data_dict)
 
     authorized =  check_access_old(package, model.Action.READ, context)
@@ -96,7 +96,7 @@
 
 def group_show(context, data_dict):
     model = context['model']
-    user = context['user']
+    user = context.get('user')
     group = get_group_object(context, data_dict)
 
     authorized =  check_access_old(group, model.Action.READ, context)


--- a/ckan/model/modification.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/model/modification.py	Mon Oct 17 13:32:39 2011 +0100
@@ -41,11 +41,14 @@
         deleted = obj_cache['deleted']
 
         for obj in new:
-            if isinstance(obj, Package):
+            if isinstance(obj, (Package, Resource)):
                 self.notify(obj, DomainObjectOperation.new)
         for obj in deleted:
-            if isinstance(obj, Package):
+            if isinstance(obj, (Package, Resource)):
                 self.notify(obj, DomainObjectOperation.deleted)
+        for obj in changed:
+            if isinstance(obj, Resource):
+                self.notify(obj, DomainObjectOperation.changed)
 
         changed_pkgs = set(obj for obj in changed if isinstance(obj, Package))
 


--- a/ckan/plugins/interfaces.py	Tue Oct 11 10:07:08 2011 +0100
+++ b/ckan/plugins/interfaces.py	Mon Oct 17 13:32:39 2011 +0100
@@ -12,7 +12,7 @@
     'IDomainObjectModification', 'IGroupController', 
     'IPackageController', 'IPluginObserver',
     'IConfigurable', 'IConfigurer', 'IAuthorizer',
-    'IActions'
+    'IActions', 'IResourceUrlChange'
 ]
 
 from inspect import isclass
@@ -167,12 +167,20 @@
 
 class IDomainObjectModification(Interface):
     """
-    Receives notification of new, changed and deleted domain objects.
+    Receives notification of new, changed and deleted datesets.
     """
 
     def notify(self, entity, operation):
         pass
 
+class IResourceUrlChange(Interface):
+    """
+    Receives notification of changed urls.
+    """
+
+    def notify(self, resource):
+        pass
+
 class IGroupController(Interface):
     """
     Hook into the Group controller. These will

Repository URL: https://bitbucket.org/okfn/ckan/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list