[ckan-changes] commit/ckan: John Glover: [moderatededits] Add resource_update to logic layer

Bitbucket commits-noreply at bitbucket.org
Thu Jul 21 14:54:19 UTC 2011


1 new changeset in ckan:

http://bitbucket.org/okfn/ckan/changeset/b9490af47c82/
changeset:   b9490af47c82
branch:      feature-1141-moderated-edits-ajax
user:        John Glover
date:        2011-07-21 16:52:48
summary:     [moderatededits] Add resource_update to logic layer
affected #:  3 files (2.1 KB)

--- a/ckan/logic/action/update.py	Thu Jul 21 14:22:36 2011 +0100
+++ b/ckan/logic/action/update.py	Thu Jul 21 15:52:48 2011 +0100
@@ -9,15 +9,18 @@
 from ckan.lib.dictization.model_dictize import (package_dictize,
                                                 package_to_api1,
                                                 package_to_api2,
+                                                resource_dictize,
                                                 group_dictize,
                                                 group_to_api1,
                                                 group_to_api2)
 from ckan.lib.dictization.model_save import (group_api_to_dict,
                                              package_api_to_dict,
                                              group_dict_save,
-                                             package_dict_save)
+                                             package_dict_save,
+                                             resource_dict_save)
 from ckan.logic.schema import (default_update_group_schema,
-                               default_update_package_schema)
+                               default_update_package_schema,
+                               default_update_resource_schema)
 from ckan.lib.navl.dictization_functions import validate
 log = logging.getLogger(__name__)
 
@@ -39,6 +42,18 @@
             error_summary[_(prettify(key))] = error[0]
     return error_summary
 
+def resource_error_summary(error_dict):
+
+    error_summary = {}
+    for key, error in error_dict.iteritems():
+        if key == 'extras':
+            error_summary[_('Extras')] = _('Missing Value')
+        elif key == 'extras_validation':
+            error_summary[_('Extras')] = error[0]
+        else:
+            error_summary[_(prettify(key))] = error[0]
+    return error_summary
+
 def group_error_summary(error_dict):
 
     error_summary = {}
@@ -146,6 +161,42 @@
     session.remove()        
 
 
+def resource_update(data_dict, context):
+    model = context['model']
+    user = context['user']
+    id = context["id"]
+    schema = context.get('schema') or default_update_resource_schema()
+    model.Session.remove()
+
+    resource = model.Resource.get(id)
+    context["resource"] = resource
+
+    if resource is None:
+        raise NotFound(_('Resource was not found.'))
+    context["id"] = resource.id
+
+    # TODO: check_access needs to be called against the package 
+    #       rather than the resource
+    check_access(resource, model.Action.EDIT, context)
+
+    data, errors = validate(data_dict, schema, context)
+
+    if errors:
+        model.Session.rollback()
+        raise ValidationError(errors, resource_error_summary(errors))
+
+    rev = model.repo.new_revision()
+    rev.author = user
+    if 'message' in context:
+        rev.message = context['message']
+    else:
+        rev.message = _(u'REST API: Update object %s') % data.get("name")
+
+    resource = resource_dict_save(data, context)
+    model.repo.commit()        
+    return resource_dictize(resource, context)
+
+
 def package_update(data_dict, context):
     model = context['model']
     user = context['user']


--- a/ckan/logic/schema.py	Thu Jul 21 14:22:36 2011 +0100
+++ b/ckan/logic/schema.py	Thu Jul 21 15:52:48 2011 +0100
@@ -44,6 +44,10 @@
 
     return schema
 
+def default_update_resource_schema():
+    schema = default_resource_schema()
+    return schema
+
 def default_tags_schema():
 
     schema = {


--- a/ckan/model/resource.py	Thu Jul 21 14:22:36 2011 +0100
+++ b/ckan/model/resource.py	Thu Jul 21 15:52:48 2011 +0100
@@ -81,6 +81,15 @@
         if self.resource_group and not core_columns_only:
             _dict["package_id"] = self.resource_group.package_id
         return _dict
+
+    @classmethod
+    def get(cls, reference):
+        '''Returns a resource object referenced by its id.'''
+        query = Session.query(ResourceRevision).filter(ResourceRevision.id==reference)
+        query = query.filter(and_(
+            ResourceRevision.state == 'active', ResourceRevision.current == True
+        ))
+        return query.first()
         
     @classmethod
     def get_columns(cls, extra_columns=True):

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