[ckan-changes] commit/ckan: 2 new changesets

Bitbucket commits-noreply at bitbucket.org
Mon Jul 18 11:11:38 UTC 2011


2 new changesets in ckan:

http://bitbucket.org/okfn/ckan/changeset/4185cc84c4ed/
changeset:   4185cc84c4ed
branch:      feature-1211-drupal
user:        kindly
date:        2011-07-13 19:08:30
summary:     [api] make sure context is always first and data_dict second
affected #:  9 files (2.9 KB)

--- a/ckan/controllers/api.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/controllers/api.py	Wed Jul 13 18:08:30 2011 +0100
@@ -128,9 +128,55 @@
         response_data['version'] = ver or '1'
         return self._finish_ok(response_data) 
 
+    def get_action(self, action):
+        if hasattr(self, '_actions'):
+            return self._actions[action]
+
+        self._actions = {}
+        for name, function in get.__dict__:
+            if not (name.startswith('_') and callable(action) and name == action):
+                self.action[name] = function
+        for name, function in update.__dict__:
+            if not (name.startswith('_') and callable(action) and name == action):
+                self.action[name] = function
+        for name, function in create.__dict__:
+            if not (name.startswith('_') and callable(action) and name == action):
+                self.action[name] = function
+        return self._actions[action]
+    
+    def action(self, action):
+        function = self.get_action(action)
+        context = {'model': model, 'session': model.Session, 'user': c.user}
+        return_dict = {'help': function.__doc__}
+
+        try:
+            request_data = self._get_request_data()
+        except ValueError, inst:
+            return self._finish_bad_request(
+                gettext('JSON Error: %s') % str(inst))
+        try:
+            result = function(context, request_data)
+            return_dict['success'] = True
+            return_dict['result'] = result
+        except DataError:
+            log.error('Format incorrect: %s' % request_data)
+            #TODO make better error message
+            return self._finish(400, _(u'Integrity Error') % request_data)
+        except NotAuthorized:
+            return_dict['error'] = {'__type': 'Authorization Error',
+                                    'message': _('Access denied')}
+            result['success'] = False
+        except ValidationError, e:
+            error_dict = e.error_dict 
+            error_dict['__type'] = 'Validtion Error'
+            return_dict['error'] = error_dict
+            result['success'] = False
+            log.error('Validation error: %r' % str(e.error_dict))
+        return self._finish_ok(return_dict)
+
     def list(self, ver=None, register=None, subregister=None, id=None):
         context = {'model': model, 'session': model.Session,
-                   'user': c.user, 'id': id, 'api_version': ver}
+                   'user': c.user, 'api_version': ver}
         log.debug('listing: %s' % context)
         action_map = {
             'revision': get.revision_list,
@@ -149,7 +195,7 @@
             return self._finish_bad_request(
                 gettext('Cannot list entity of this type: %s') % register)
         try:
-            return self._finish_ok(action(context))
+            return self._finish_ok(action(context, {'id': id}))
         except NotFound, e:
             extra_msg = e.extra_msg
             return self._finish_not_found(extra_msg)
@@ -166,8 +212,9 @@
         }
 
         context = {'model': model, 'session': model.Session, 'user': c.user,
-                   'id': id, 'id2': id2, 'rel': subregister,
                    'api_version': ver}
+        data_dict = {'id': id, 'id2': id2, 'rel': subregister}
+
         for type in model.PackageRelationship.get_all_types():
             action_map[('package', type)] = get.package_relationships_list
         log.debug('show: %s' % context)
@@ -180,7 +227,7 @@
                 gettext('Cannot read entity of this type: %s') % register)
         try:
             
-            return self._finish_ok(action(context))
+            return self._finish_ok(action(context, data_dict))
         except NotFound, e:
             extra_msg = e.extra_msg
             return self._finish_not_found(extra_msg)
@@ -202,13 +249,13 @@
         for type in model.PackageRelationship.get_all_types():
             action_map[('package', type)] = create.package_relationship_create
 
-
         context = {'model': model, 'session': model.Session, 'user': c.user,
-                   'id': id, 'id2': id2, 'rel': subregister,
                    'api_version': ver}
         log.debug('create: %s' % (context))
         try:
             request_data = self._get_request_data()
+            data_dict = {'id': id, 'id2': id2, 'rel': subregister}
+            data_dict.update(request_data)
         except ValueError, inst:
             return self._finish_bad_request(
                 gettext('JSON Error: %s') % str(inst))
@@ -221,10 +268,10 @@
                 gettext('Cannot create new entity of this type: %s %s') % \
                 (register, subregister))
         try:
-            response_data = action(request_data, context)
+            response_data = action(context, data_dict)
             location = None
-            if "id" in context:
-                location = str('%s/%s' % (request.path, context.get("id")))
+            if "id" in data_dict:
+                location = str('%s/%s' % (request.path, data_dict.get("id")))
             return self._finish_ok(response_data,
                                    resource_location=location)
         except NotAuthorized:
@@ -251,11 +298,12 @@
             action_map[('package', type)] = update.package_relationship_update
 
         context = {'model': model, 'session': model.Session, 'user': c.user,
-                   'id': id, 'id2': id2, 'rel': subregister,
-                   'api_version': ver}
+                   'api_version': ver, 'id': id}
         log.debug('update: %s' % (context))
         try:
             request_data = self._get_request_data()
+            data_dict = {'id': id, 'id2': id2, 'rel': subregister}
+            data_dict.update(request_data)
         except ValueError, inst:
             return self._finish_bad_request(
                 gettext('JSON Error: %s') % str(inst))
@@ -267,7 +315,7 @@
                 gettext('Cannot update entity of this type: %s') % \
                     register.encode('utf-8'))
         try:
-            response_data = action(request_data, context)
+            response_data = action(context, data_dict)
             return self._finish_ok(response_data)
         except NotAuthorized:
             return self._finish_not_authz()


--- a/ckan/controllers/group.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/controllers/group.py	Wed Jul 13 18:08:30 2011 +0100
@@ -115,13 +115,14 @@
                    'user': c.user or c.author, 'extras_as_string': True,
                    'save': 'save' in request.params,
                    'schema': self._form_to_db_schema(),
-                   'id': id}
+                   }
+        data_dict = {'id': id}
 
         if context['save'] and not data:
             return self._save_edit(id, context)
 
         try:
-            old_data = get.group_show(context)
+            old_data = get.group_show(context, data_dict)
             c.grouptitle = old_data.get('title')
             c.groupname = old_data.get('name')
             schema = self._db_to_form_schema()
@@ -151,7 +152,7 @@
             data_dict = clean_dict(unflatten(
                 tuplize_dict(parse_params(request.params))))
             context['message'] = data_dict.get('log_message', '')
-            group = create.group_create(data_dict, context)
+            group = create.group_create(context, data_dict)
             h.redirect_to(controller='group', action='read', id=group['name'])
         except NotAuthorized:
             abort(401, _('Unauthorized to read group %s') % '')
@@ -169,7 +170,8 @@
             data_dict = clean_dict(unflatten(
                 tuplize_dict(parse_params(request.params))))
             context['message'] = data_dict.get('log_message', '')
-            group = update.group_update(data_dict, context)
+            data_dict['id'] = id
+            group = update.group_update(context, data_dict)
             h.redirect_to(controller='group', action='read', id=group['name'])
         except NotAuthorized:
             abort(401, _('Unauthorized to read group %s') % id)


--- a/ckan/controllers/home.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/controllers/home.py	Wed Jul 13 18:08:30 2011 +0100
@@ -49,7 +49,8 @@
         c.package_count = query.count
         c.latest_packages = current_package_list_with_resources({'model': model,
                                                                 'user': c.user,
-                                                                'limit': 5})      
+                                                                'limit': 5},
+                                                                 {})      
         return render('home/index.html', cache_key=cache_key,
                 cache_expire=cache_expires)
 


--- a/ckan/controllers/package.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/controllers/package.py	Wed Jul 13 18:08:30 2011 +0100
@@ -77,9 +77,9 @@
             log.info('incorrect form fields posted')
             raise DataError(data_dict)
 
-    def _setup_template_variables(self, context):
-        c.groups = get.group_list_availible(context)
-        c.groups_authz = get.group_list_authz(context)
+    def _setup_template_variables(self, context, data_dict):
+        c.groups = get.group_list_availible(context, data_dict)
+        c.groups_authz = get.group_list_authz(context, data_dict)
         c.licences = [('', '')] + model.Package.get_license_options()
         c.is_sysadmin = Authorizer().is_sysadmin(c.user)
         c.resource_columns = model.Resource.get_columns()
@@ -178,11 +178,11 @@
     def read(self, id):
         context = {'model': model, 'session': model.Session,
                    'user': c.user or c.author, 'extras_as_string': True,
-                   'schema': self._form_to_db_schema(),
-                   'id': id}
+                   'schema': self._form_to_db_schema()}
+        data_dict = {'id': id}
         split = id.split('@')
         if len(split) == 2:
-            context['id'], revision = split
+            data_dict['id'], revision = split
             try:
                 date = datetime.datetime(*map(int, re.split('[^\d]', revision)))
                 context['revision_date'] = date
@@ -190,7 +190,7 @@
                 context['revision_id'] = revision
         #check if package exists
         try:
-            c.pkg_dict = get.package_show(context)
+            c.pkg_dict = get.package_show(context, data_dict)
             c.pkg = context['package']
         except NotFound:
             abort(404, _('Package not found'))
@@ -221,12 +221,11 @@
     def comments(self, id):
         context = {'model': model, 'session': model.Session,
                    'user': c.user or c.author, 'extras_as_string': True,
-                   'schema': self._form_to_db_schema(),
-                   'id': id}
+                   'schema': self._form_to_db_schema()}
 
         #check if package exists
         try:
-            c.pkg_dict = get.package_show(context)
+            c.pkg_dict = get.package_show(context, {'id':id})
             c.pkg = context['package']
         except NotFound:
             abort(404, _('Package not found'))
@@ -323,7 +322,7 @@
         error_summary = error_summary or {}
         vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
 
-        self._setup_template_variables(context)
+        self._setup_template_variables(context, {'id': id})
         c.form = render(self.package_form, extra_vars=vars)
         return render('package/new.html')
 
@@ -333,14 +332,14 @@
                    'user': c.user or c.author, 'extras_as_string': True,
                    'preview': 'preview' in request.params,
                    'save': 'save' in request.params,
-                   'id': id, 'moderated': config.get('moderated'),
+                   'moderated': config.get('moderated'),
                    'pending': True,
                    'schema': self._form_to_db_schema()}
 
         if (context['save'] or context['preview']) and not data:
             return self._save_edit(id, context)
         try:
-            old_data = get.package_show(context)
+            old_data = get.package_show(context, {'id':id})
             schema = self._db_to_form_schema()
             if schema:
                 old_data, errors = validate(old_data, schema)
@@ -359,19 +358,19 @@
         errors = errors or {}
         vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
 
-        self._setup_template_variables(context)
+        self._setup_template_variables(context, {'id':'id'})
         c.form = render(self.package_form, extra_vars=vars)
         return render('package/edit.html')
 
     def read_ajax(self, id, revision=None):
         context = {'model': model, 'session': model.Session,
                    'user': c.user or c.author,
-                   'id': id, 'extras_as_string': True,
+                   'extras_as_string': True,
                    'schema': self._form_to_db_schema(),
                    'revision_id': revision}
 
         try:
-            data = get.package_show(context)
+            data = get.package_show(context, {'id': id})
             schema = self._db_to_form_schema()
             if schema:
                 data, errors = validate(data, schema)
@@ -389,9 +388,6 @@
 
     def history_ajax(self, id):
 
-        context = {'model': model, 'session': model.Session,
-                   'user': c.user or c.author,
-                   'id': id, 'extras_as_string': True}
         pkg = model.Package.get(id)
         data = []
         approved = False
@@ -418,7 +414,7 @@
                 tuplize_dict(parse_params(request.POST))))
             self._check_data_dict(data_dict)
             context['message'] = data_dict.get('log_message', '')
-            pkg = create.package_create(data_dict, context)
+            pkg = create.package_create(context, data_dict)
 
             if context['preview']:
                 PackageSaver().render_package(pkg, context)
@@ -448,9 +444,10 @@
             context['message'] = data_dict.get('log_message', '')
             if not context['moderated']:
                 context['pending'] = False
-            pkg = update.package_update(data_dict, context)
+            data_dict['id'] = id
+            pkg = update.package_update(context, data_dict)
             if request.params.get('save', '') == 'Approve':
-                update.make_latest_pending_package_active(context)
+                update.make_latest_pending_package_active(context, data_dict)
             c.pkg = context['package']
             c.pkg_dict = pkg
 


--- a/ckan/logic/action/create.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/logic/action/create.py	Wed Jul 13 18:08:30 2011 +0100
@@ -30,7 +30,7 @@
                                       check_group_auth)
 log = logging.getLogger(__name__)
 
-def package_create(data_dict, context):
+def package_create(context, data_dict):
 
     model = context['model']
     user = context['user']
@@ -39,7 +39,7 @@
     model.Session.remove()
 
     check_access(model.System(), model.Action.PACKAGE_CREATE, context)
-    check_group_auth(data_dict, context)
+    check_group_auth(context, data_dict)
 
     data, errors = validate(data_dict, schema, context)
 
@@ -76,7 +76,7 @@
     else:
         return data
 
-def resource_create(data_dict, context):
+def resource_create(context, data_dict):
     model = context['model']
     user = context['user']
 
@@ -85,13 +85,13 @@
                             context)
 
 
-def package_relationship_create(data_dict, context):
+def package_relationship_create(context, data_dict):
 
     model = context['model']
     user = context['user']
-    id = context["id"]
-    id2 = context["id2"]
-    rel_type = context["rel"]
+    id = data_dict["id"]
+    id2 = data_dict["id2"]
+    rel_type = data_dict["rel"]
     api = context.get('api_version') or '1'
     ref_package_by = 'id' if api == '2' else 'name'
 
@@ -124,7 +124,7 @@
     relationship_dicts = rel.as_dict(ref_package_by=ref_package_by)
     return relationship_dicts
 
-def group_create(data_dict, context):
+def group_create(context, data_dict):
     model = context['model']
     user = context['user']
     schema = context.get('schema') or default_group_schema()
@@ -160,7 +160,7 @@
     log.debug('Created object %s' % str(group.name))
     return group_dictize(group, context)
 
-def rating_create(data_dict, context):
+def rating_create(context, data_dict):
 
     model = context['model']
     user = context.get("user") 
@@ -197,12 +197,12 @@
 
 ## Modifications for rest api
 
-def package_create_rest(data_dict, context):
+def package_create_rest(context, data_dict):
 
     api = context.get('api_version') or '1'
 
     dictized_package = package_api_to_dict(data_dict, context)
-    dictized_after = package_create(dictized_package, context) 
+    dictized_after = package_create(context, dictized_package) 
 
     pkg = context['package']
 
@@ -211,14 +211,16 @@
     else:
         package_dict = package_to_api2(pkg, context)
 
+    data_dict['id'] = pkg.id
+
     return package_dict
 
-def group_create_rest(data_dict, context):
+def group_create_rest(context, data_dict):
 
     api = context.get('api_version') or '1'
 
     dictized_group = group_api_to_dict(data_dict, context)
-    dictized_after = group_create(dictized_group, context) 
+    dictized_after = group_create(context, dictized_group) 
 
     group = context['group']
 
@@ -227,5 +229,7 @@
     else:
         group_dict = group_to_api2(group, context)
 
+    data_dict['id'] = group.id
+
     return group_dict
 


--- a/ckan/logic/action/get.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/logic/action/get.py	Wed Jul 13 18:08:30 2011 +0100
@@ -14,7 +14,7 @@
                                                 group_dictize)
 
 
-def package_list(context):
+def package_list(context, data_dict):
     model = context["model"]
     user = context["user"]
     api = context["api_version"]
@@ -24,10 +24,10 @@
     packages = query.all()
     return [getattr(p, ref_package_by) for p in packages]
 
-def current_package_list_with_resources(context):
+def current_package_list_with_resources(context, data_dict):
     model = context["model"]
     user = context["user"]
-    limit = context.get("limit")
+    limit = data_dict.get("limit")
 
     q = ckan.authz.Authorizer().authorized_query(user, model.PackageRevision)
     q = q.filter(model.PackageRevision.state=='active')
@@ -56,15 +56,15 @@
         package_list.append(result_dict)
     return package_list
 
-def revision_list(context):
+def revision_list(context, data_dict):
 
     model = context["model"]
     revs = model.Session.query(model.Revision).all()
     return [rev.id for rev in revs]
 
-def package_revision_list(context):
+def package_revision_list(context, data_dict):
     model = context["model"]
-    id = context["id"]
+    id = data_dict["id"]
     pkg = model.Package.get(id)
     if pkg is None:
         raise NotFound
@@ -76,7 +76,7 @@
                                                      include_packages=False))
     return revision_dicts
 
-def group_list(context):
+def group_list(context, data_dict):
     model = context["model"]
     user = context["user"]
     api = context.get('api_version') or '1'
@@ -86,7 +86,7 @@
     groups = query.all() 
     return [getattr(p, ref_group_by) for p in groups]
 
-def group_list_authz(context):
+def group_list_authz(context, data_dict):
     model = context['model']
     user = context['user']
     pkg = context.get('package')
@@ -95,7 +95,7 @@
     groups = set(query.all())
     return dict((group.id, group.name) for group in groups)
 
-def group_list_availible(context):
+def group_list_availible(context, data_dict):
     model = context['model']
     user = context['user']
     pkg = context.get('package')
@@ -108,30 +108,30 @@
 
     return [(group.id, group.name) for group in groups]
 
-def licence_list(context):
+def licence_list(context, data_dict):
     model = context["model"]
     license_register = model.Package.get_license_register()
     licenses = license_register.values()
     licences = [l.as_dict() for l in licenses]
     return licences
 
-def tag_list(context):
+def tag_list(context, data_dict):
     model = context["model"]
     tags = model.Session.query(model.Tag).all() #TODO
     tag_list = [tag.name for tag in tags]
     return tag_list
 
-def package_relationships_list(context):
+def package_relationships_list(context, data_dict):
 
     ##TODO needs to work with dictization layer
     model = context['model']
     user = context['user']
-    id = context["id"]
-    id2 = context.get("id2")
-    rel = context.get("rel")
     api = context.get('api_version') or '1'
+
+    id = data_dict["id"]
+    id2 = data_dict.get("id2")
+    rel = data_dict.get("rel")
     ref_package_by = 'id' if api == '2' else 'name';
-
     pkg1 = model.Package.get(id)
     pkg2 = None
     if not pkg1:
@@ -157,11 +157,11 @@
 
     return relationship_dicts
 
-def package_show(context):
+def package_show(context, data_dict):
 
     model = context['model']
     api = context.get('api_version') or '1'
-    id = context['id']
+    id = data_dict['id']
 
     pkg = model.Package.get(id)
 
@@ -179,10 +179,10 @@
     return package_dict
 
 
-def revision_show(context):
+def revision_show(context, data_dict):
     model = context['model']
     api = context.get('api_version') or '1'
-    id = context['id']
+    id = data_dict['id']
     ref_package_by = 'id' if api == '2' else 'name'
 
     rev = model.Session.query(model.Revision).get(id)
@@ -192,9 +192,9 @@
                                       ref_package_by=ref_package_by)
     return rev_dict
 
-def group_show(context):
+def group_show(context, data_dict):
     model = context['model']
-    id = context['id']
+    id = data_dict['id']
     api = context.get('api_version') or '1'
 
 
@@ -213,10 +213,10 @@
     return group_dict
 
 
-def tag_show(context):
+def tag_show(context, data_dict):
     model = context['model']
     api = context.get('api_version') or '1'
-    id = context['id']
+    id = data_dict['id']
     ref_package_by = 'id' if api == '2' else 'name'
     obj = model.Tag.get(id) #TODO tags
     if obj is None:
@@ -226,12 +226,11 @@
     return package_list 
 
 
-def package_show_rest(context):
+def package_show_rest(context, data_dict):
 
-    package_show(context)
+    package_show(context, data_dict)
 
     api = context.get('api_version') or '1'
-
     pkg = context['package']
 
     if api == '1':
@@ -241,9 +240,9 @@
 
     return package_dict
 
-def group_show_rest(context):
+def group_show_rest(context, data_dict):
 
-    group_show(context)
+    group_show(context, data_dict)
     api = context.get('api_version') or '1'
     group = context['group']
 


--- a/ckan/logic/action/update.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/logic/action/update.py	Wed Jul 13 18:08:30 2011 +0100
@@ -51,7 +51,7 @@
             error_summary[_(prettify(key))] = error[0]
     return error_summary
 
-def check_group_auth(data_dict, context):
+def check_group_auth(context, data_dict):
     model = context['model']
     pkg = context.get("package")
 
@@ -105,11 +105,11 @@
         context['latest_revision_date'] = latest_rev.revision_timestamp
         context['latest_revision'] = latest_rev.revision_id
 
-def make_latest_pending_package_active(context):
+def make_latest_pending_package_active(context, data_dict):
 
     model = context['model']
     session = model.Session
-    id = context["id"]
+    id = data_dict["id"]
     pkg = model.Package.get(id)
 
     check_access(pkg, model.Action.EDIT, context)
@@ -146,10 +146,11 @@
     session.remove()        
 
 
-def package_update(data_dict, context):
+def package_update(context, data_dict):
     model = context['model']
     user = context['user']
-    id = context["id"]
+    
+    id = data_dict["id"]
     preview = context.get('preview', False)
     schema = context.get('schema') or default_update_package_schema()
     model.Session.remove()
@@ -159,13 +160,13 @@
 
     if pkg is None:
         raise NotFound(_('Package was not found.'))
-    context["id"] = pkg.id
+    data_dict["id"] = pkg.id
 
     check_access(pkg, model.Action.EDIT, context)
 
     data, errors = validate(data_dict, schema, context)
 
-    check_group_auth(data, context)
+    check_group_auth(context, data)
 
     if errors:
         model.Session.rollback()
@@ -205,13 +206,13 @@
                                     ref_package_by=ref_package_by)
     return rel_dict
 
-def package_relationship_update(data_dict, context):
+def package_relationship_update(context, data_dict):
 
     model = context['model']
     user = context['user']
-    id = context["id"]
-    id2 = context["id2"]
-    rel = context["rel"]
+    id = data_dict["id"]
+    id2 = data_dict["id2"]
+    rel = data_dict["rel"]
     api = context.get('api_version') or '1'
     ref_package_by = 'id' if api == '2' else 'name'
 
@@ -236,12 +237,12 @@
     comment = data_dict.get('comment', u'')
     return _update_package_relationship(entity, comment, context)
 
-def group_update(data_dict, context):
+def group_update(context, data_dict):
 
     model = context['model']
     user = context['user']
     schema = context.get('schema') or default_update_group_schema()
-    id = context['id']
+    id = data_dict['id']
 
     group = model.Group.get(id)
     context["group"] = group
@@ -276,16 +277,28 @@
 
 ## Modifications for rest api
 
-def package_update_rest(data_dict, context):
+def package_update_rest(context, data_dict):
 
     model = context['model']
-    id = context["id"]
+    id = data_dict.get("id")
+    request_id = context['id']
     api = context.get('api_version') or '1'
-    pkg = model.Package.get(id)
+    pkg = model.Package.get(request_id)
+
+    if not pkg:
+        raise NotFound
+
+    if id and id != pkg.id:
+        pkg_from_data = model.Package.get(id)
+        if pkg_from_data != pkg:
+            error_dict = {id:('Cannot change value of key from %s to %s. '
+                'This key is read-only') % (pkg.id, id)}
+            raise ValidationError(error_dict)
+
     context["package"] = pkg
     context["allow_partial_update"] = True
     dictized_package = package_api_to_dict(data_dict, context)
-    dictized_after = package_update(dictized_package, context)
+    dictized_after = package_update(context, dictized_package)
 
     pkg = context['package']
 
@@ -296,16 +309,16 @@
 
     return package_dict
 
-def group_update_rest(data_dict, context):
+def group_update_rest(context, data_dict):
 
     model = context['model']
-    id = context["id"]
+    id = data_dict["id"]
     api = context.get('api_version') or '1'
     group = model.Group.get(id)
     context["group"] = group
     context["allow_partial_update"] = True
     dictized_package = group_api_to_dict(data_dict, context)
-    dictized_after = group_update(dictized_package, context)
+    dictized_after = group_update(context, dictized_package)
 
     group = context['group']
 


--- a/ckan/tests/functional/api/model/test_package.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/tests/functional/api/model/test_package.py	Wed Jul 13 18:08:30 2011 +0100
@@ -48,6 +48,7 @@
 
         # Check the value of the Location header.
         location = res.header('Location')
+        
         assert offset in location
         res = self.app.get(location, status=self.STATUS_200_OK)
         # Check the database record.


--- a/ckan/tests/lib/test_dictization.py	Tue Jul 12 11:50:02 2011 +0100
+++ b/ckan/tests/lib/test_dictization.py	Wed Jul 13 18:08:30 2011 +0100
@@ -516,9 +516,8 @@
         anna1 = model.Session.query(model.Package).filter_by(name='annakarenina_changed2').one()
         context = {"model": model,
                    "session": model.Session,
-                   'user': 'testsysadmin',
-                   "id": anna1.id}
-        make_latest_pending_package_active(context)
+                   'user': 'testsysadmin'}
+        make_latest_pending_package_active(context, {'id': anna1.id})
 
         pkgrevisions = model.Session.query(model.PackageRevision).filter_by(id=anna1.id).all()
         sorted_packages = sorted(pkgrevisions, key=lambda x:x.revision_timestamp)[::-1]


http://bitbucket.org/okfn/ckan/changeset/4cdde613aeef/
changeset:   4cdde613aeef
branch:      feature-1211-drupal
user:        kindly
date:        2011-07-15 16:10:46
summary:     [api] action api with tests
affected #:  6 files (3.4 KB)

--- a/ckan/config/routing.py	Wed Jul 13 18:08:30 2011 +0100
+++ b/ckan/config/routing.py	Fri Jul 15 15:10:46 2011 +0100
@@ -104,6 +104,8 @@
     
     map.connect('/api/rest', controller='api', action='index')
 
+    map.connect('/api/action/{logic_function}', controller='api', action='action')
+
     map.connect('/api/rest/{register}', controller='api', action='list',
         requirements=dict(register=register_list_str),
         conditions=dict(method=['GET'])


--- a/ckan/controllers/api.py	Wed Jul 13 18:08:30 2011 +0100
+++ b/ckan/controllers/api.py	Fri Jul 15 15:10:46 2011 +0100
@@ -30,6 +30,8 @@
     }
 class ApiController(BaseController):
 
+    _actions = {}
+
     def __call__(self, environ, start_response):
         self._identify_user()
         if not self.authorizer.am_authorized(c, model.Action.SITE_READ, model.System):
@@ -128,30 +130,40 @@
         response_data['version'] = ver or '1'
         return self._finish_ok(response_data) 
 
+    @classmethod
+    def create_actions(cls):
+        if cls._actions:
+            return 
+        for name, action in get.__dict__.iteritems():
+            if not name.startswith('_') and callable(action):
+                cls._actions[name] = action
+        for name, action in update.__dict__.iteritems():
+            if not name.startswith('_') and callable(action):
+                cls._actions[name] = action
+        for name, action in create.__dict__.iteritems():
+            if not name.startswith('_') and callable(action):
+                cls._actions[name] = action
+
     def get_action(self, action):
-        if hasattr(self, '_actions'):
-            return self._actions[action]
+        self.create_actions()
+        return self._actions[action]
 
-        self._actions = {}
-        for name, function in get.__dict__:
-            if not (name.startswith('_') and callable(action) and name == action):
-                self.action[name] = function
-        for name, function in update.__dict__:
-            if not (name.startswith('_') and callable(action) and name == action):
-                self.action[name] = function
-        for name, function in create.__dict__:
-            if not (name.startswith('_') and callable(action) and name == action):
-                self.action[name] = function
-        return self._actions[action]
+    @classmethod
+    def register_action(cls, name, function):
+        cls.create_actions()
+        cls._actions[name] = function
     
-    def action(self, action):
-        function = self.get_action(action)
+    def action(self, logic_function):
+        function = self.get_action(logic_function)
+        
         context = {'model': model, 'session': model.Session, 'user': c.user}
+        model.Session()._context = context
         return_dict = {'help': function.__doc__}
 
         try:
             request_data = self._get_request_data()
         except ValueError, inst:
+
             return self._finish_bad_request(
                 gettext('JSON Error: %s') % str(inst))
         try:
@@ -165,12 +177,12 @@
         except NotAuthorized:
             return_dict['error'] = {'__type': 'Authorization Error',
                                     'message': _('Access denied')}
-            result['success'] = False
+            return_dict['success'] = False
         except ValidationError, e:
             error_dict = e.error_dict 
             error_dict['__type'] = 'Validtion Error'
             return_dict['error'] = error_dict
-            result['success'] = False
+            return_dict['success'] = False
             log.error('Validation error: %r' % str(e.error_dict))
         return self._finish_ok(return_dict)
 
@@ -558,3 +570,4 @@
             resultSet["ResultSet"]["Result"].append(result)
         return self._finish_ok(resultSet)
 
+


--- a/ckan/logic/action/create.py	Wed Jul 13 18:08:30 2011 +0100
+++ b/ckan/logic/action/create.py	Fri Jul 15 15:10:46 2011 +0100
@@ -37,6 +37,7 @@
     preview = context.get('preview', False)
     schema = context.get('schema') or default_create_package_schema()
     model.Session.remove()
+    model.Session()._context = context
 
     check_access(model.System(), model.Action.PACKAGE_CREATE, context)
     check_group_auth(context, data_dict)
@@ -84,7 +85,6 @@
                             default_resource_schema(),
                             context)
 
-
 def package_relationship_create(context, data_dict):
 
     model = context['model']


--- a/ckan/logic/action/get.py	Wed Jul 13 18:08:30 2011 +0100
+++ b/ckan/logic/action/get.py	Fri Jul 15 15:10:46 2011 +0100
@@ -15,9 +15,10 @@
 
 
 def package_list(context, data_dict):
+    '''Lists the package by name'''
     model = context["model"]
     user = context["user"]
-    api = context["api_version"]
+    api = context.get("api_version", '1')
     ref_package_by = 'id' if api == '2' else 'name'
 
     query = ckan.authz.Authorizer().authorized_query(user, model.Package)


--- a/ckan/logic/action/update.py	Wed Jul 13 18:08:30 2011 +0100
+++ b/ckan/logic/action/update.py	Fri Jul 15 15:10:46 2011 +0100
@@ -154,6 +154,7 @@
     preview = context.get('preview', False)
     schema = context.get('schema') or default_update_package_schema()
     model.Session.remove()
+    model.Session()._context = context
 
     pkg = model.Package.get(id)
     context["package"] = pkg


--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ckan/tests/functional/api/test_action.py	Fri Jul 15 15:10:46 2011 +0100
@@ -0,0 +1,72 @@
+from ckan.lib.create_test_data import CreateTestData
+import ckan.model as model
+from ckan.tests import WsgiAppCase
+import json
+from pprint import pprint, pformat
+
+class TestAction(WsgiAppCase):
+
+    @classmethod
+    def setup_class(self):
+        CreateTestData.create()
+
+    @classmethod
+    def teardown_class(self):
+        model.repo.rebuild_db()
+
+    def test_01_package_list(self):
+        postparams = '%s=1' % json.dumps({})
+        res = self.app.post('/api/action/package_list', params=postparams)
+        assert json.loads(res.body) == {"help": "Lists the package by name",
+                                        "success": True,
+                                        "result": ["annakarenina", "warandpeace"]}
+
+    def test_02_create_update_package(self):
+
+        package = {
+            'author': None,
+            'author_email': None,
+            'extras': [{'key': u'original media','value': u'"book"'}],
+            'license_id': u'other-open',
+            'maintainer': None,
+            'maintainer_email': None,
+            'name': u'annakareninanew',
+            'notes': u'Some test now',
+            'resources': [{'alt_url': u'alt123',
+                           'description': u'Full text.',
+                           'extras': {u'alt_url': u'alt123', u'size': u'123'},
+                           'format': u'plain text',
+                           'hash': u'abc123',
+                           'position': 0,
+                           'url': u'http://www.annakarenina.com/download/'},
+                          {'alt_url': u'alt345',
+                           'description': u'Index of the novel',
+                           'extras': {u'alt_url': u'alt345', u'size': u'345'},
+                           'format': u'json',
+                           'hash': u'def456',
+                           'position': 1,
+                           'url': u'http://www.annakarenina.com/index.json'}],
+            'tags': [{'name': u'russian'}, {'name': u'tolstoy'}],
+            'title': u'A Novel By Tolstoy',
+            'url': u'http://www.annakarenina.com',
+            'version': u'0.7a'
+        }
+
+        wee = json.dumps(package)
+        postparams = '%s=1' % json.dumps(package)
+        res = self.app.post('/api/action/package_create', params=postparams,
+                            extra_environ={'Authorization': 'tester'})
+        package_created = json.loads(res.body)['result']
+        print package_created
+        package_created['name'] = 'moo'
+        postparams = '%s=1' % json.dumps(package_created)
+        res = self.app.post('/api/action/package_update', params=postparams,
+                            extra_environ={'Authorization': 'tester'})
+
+        package_updated = json.loads(res.body)['result']
+        package_updated.pop('revision_id')
+        package_updated.pop('revision_timestamp')
+        package_created.pop('revision_id')
+        package_created.pop('revision_timestamp')
+        assert package_updated == package_created#, (pformat(json.loads(res.body)), pformat(package_created['result']))
+

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