Skip to content

Commit

Permalink
rest of web/api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Feb 24, 2012
1 parent 303dd6d commit 6f743b6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
6 changes: 4 additions & 2 deletions ckan/controllers/api.py
Expand Up @@ -135,13 +135,16 @@ def get_api(self, ver=None):
return self._finish_ok(response_data)

def action(self, logic_function):
# FIXME this is a hack till ver gets passed
api_version = 3
function = get_action(logic_function)
if not function:
log.error('Can\'t find logic function: %s' % logic_function)
return self._finish_bad_request(
gettext('Action name not known: %s') % str(logic_function))

context = {'model': model, 'session': model.Session, 'user': c.user}
context = {'model': model, 'session': model.Session, 'user': c.user,
'api_version':api_version}
model.Session()._context = context
return_dict = {'help': function.__doc__}
try:
Expand Down Expand Up @@ -267,7 +270,6 @@ def show(self, ver=None, register=None, subregister=None, id=None, id2=None):
return self._finish_bad_request(
gettext('Cannot read entity of this type: %s') % register)
try:

return self._finish_ok(action(context, data_dict))
except NotFound, e:
extra_msg = e.extra_msg
Expand Down
9 changes: 3 additions & 6 deletions ckan/controllers/package.py
Expand Up @@ -465,7 +465,6 @@ def _save_new(self, context, package_type=None):
data_dict = clean_dict(unflatten(
tuplize_dict(parse_params(request.POST))))
data_dict['type'] = package_type
self._check_data_dict(data_dict)
context['message'] = data_dict.get('log_message', '')
pkg = get_action('package_create')(context, data_dict)

Expand All @@ -483,17 +482,15 @@ def _save_new(self, context, package_type=None):
error_summary = e.error_summary
return self.new(data_dict, errors, error_summary)

def _save_edit(self, id, context):
def _save_edit(self, name_or_id, context):
from ckan.lib.search import SearchIndexError
try:
package_type = self._get_package_type(id)
data_dict = clean_dict(unflatten(
tuplize_dict(parse_params(request.POST))))
self._check_data_dict(data_dict, package_type=package_type)
context['message'] = data_dict.get('log_message', '')
if not context['moderated']:
context['pending'] = False
data_dict['id'] = id
data_dict['id'] = name_or_id
pkg = get_action('package_update')(context, data_dict)
if request.params.get('save', '') == 'Approve':
get_action('make_latest_pending_package_active')(context, data_dict)
Expand All @@ -512,7 +509,7 @@ def _save_edit(self, id, context):
except ValidationError, e:
errors = e.error_dict
error_summary = e.error_summary
return self.edit(id, data_dict, errors, error_summary)
return self.edit(name_or_id, data_dict, errors, error_summary)

def _form_save_redirect(self, pkgname, action):
'''This redirects the user to the CKAN package/read page,
Expand Down
19 changes: 16 additions & 3 deletions ckan/lib/plugins.py
Expand Up @@ -54,6 +54,9 @@ def register_package_plugins(map):
exception will be raised.
"""
global _default_package_plugin
if _default_package_plugin:
# we've already set things up
return

# Create the mappings and register the fallback behaviour if one is found.
for plugin in PluginImplementations(IDatasetForm):
Expand Down Expand Up @@ -163,11 +166,20 @@ def package_form(self):
def form_to_db_schema(self):
return schema.package_form_schema()

def form_to_db_schema_options(self, options):
if options.get('api'):
if options.get('type') == 'create':
return schema.default_create_package_schema()
else:
return schema.default_update_package_schema()
else:
return schema.package_form_schema()

def db_to_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''

def check_data_dict(self, data_dict):
def check_data_dict(self, data_dict, schema=None):
'''Check if the return data is correct, mostly for checking out
if spammers are submitting only part of the form'''

Expand All @@ -176,11 +188,12 @@ def check_data_dict(self, data_dict):
'extras_validation', 'save', 'return_to',
'resources', 'type']

schema_keys = self.form_to_db_schema().keys()
if not schema:
schema = self.form_to_db_schema()
schema_keys = schema.keys()
keys_in_schema = set(schema_keys) - set(surplus_keys_schema)

missing_keys = keys_in_schema - set(data_dict.keys())

if missing_keys:
log.info('incorrect form fields posted, missing %s' % missing_keys)
raise DataError(data_dict)
Expand Down
15 changes: 13 additions & 2 deletions ckan/logic/action/create.py
Expand Up @@ -41,19 +41,30 @@
from ckan.lib.navl.dictization_functions import validate
from ckan.logic.action.update import _update_package_relationship
from ckan.logic.action import rename_keys, error_summary
from ckan import logic

log = logging.getLogger(__name__)

def package_create(context, data_dict):

model = context['model']
user = context['user']
schema = lookup_package_plugin().form_to_db_schema()
model.Session.remove()
model.Session()._context = context

package_type = data_dict.get('type')
package_plugin = lookup_package_plugin(package_type)
try:
schema = package_plugin.form_to_db_schema_options({'type':'create',
'api':'api_version' in context})
except AttributeError:
schema = package_plugin.form_to_db_schema()

check_access('package_create', context, data_dict)

if 'api_version' not in context:
package_plugin.check_data_dict(data_dict, schema)

data, errors = validate(data_dict, schema, context)

if errors:
Expand Down Expand Up @@ -309,7 +320,7 @@ def package_create_rest(context, data_dict):
check_access('package_create_rest', context, data_dict)

dictized_package = package_api_to_dict(data_dict, context)
dictized_after = package_create(context, dictized_package)
dictized_after = logic.get_action('package_create')(context, dictized_package)

pkg = context['package']

Expand Down
9 changes: 4 additions & 5 deletions ckan/logic/action/get.py
Expand Up @@ -336,16 +336,15 @@ def package_relationships_list(context, data_dict):
def package_show(context, data_dict):

model = context['model']
api = context.get('api_version') or '1'
id = data_dict['id']
name_or_id = data_dict.get("id") or data_dict['name_or_id']

pkg = model.Package.get(id)

context['package'] = pkg
pkg = model.Package.get(name_or_id)

if pkg is None:
raise logic.NotFound

context['package'] = pkg

logic.check_access('package_show', context, data_dict)

package_dict = package_dictize(pkg, context)
Expand Down
36 changes: 25 additions & 11 deletions ckan/logic/action/update.py
Expand Up @@ -6,7 +6,7 @@
from ckan.logic import get_action, check_access
from lib.plugins import lookup_package_plugin

from ckan.lib.base import _, abort
from ckan.lib.base import _
from vdm.sqlalchemy.base import SQLAlchemySession
import ckan.lib.dictization
from ckan.lib.dictization.model_dictize import (package_dictize,
Expand Down Expand Up @@ -36,6 +36,7 @@
default_update_vocabulary_schema)
from ckan.lib.navl.dictization_functions import validate
import ckan.lib.navl.validators as validators
from ckan.lib.navl.dictization_functions import DataError
from ckan.logic.action import rename_keys, get_domain_object, error_summary
from ckan.logic.action.get import roles_show

Expand Down Expand Up @@ -119,7 +120,6 @@ def make_latest_pending_package_active(context, data_dict):

def resource_update(context, data_dict):
model = context['model']
session = context['session']
user = context['user']
id = data_dict["id"]
schema = context.get('schema') or default_update_resource_schema()
Expand Down Expand Up @@ -158,20 +158,29 @@ def package_update(context, data_dict):

model = context['model']
user = context['user']
id = data_dict["id"]
name_or_id = data_dict.get("id") or data_dict['name_or_id']
model.Session.remove()
model.Session()._context = context

pkg = model.Package.get(id)
context["package"] = pkg

pkg = model.Package.get(name_or_id)
if pkg is None:
raise NotFound(_('Package was not found.'))
context["package"] = pkg
data_dict["id"] = pkg.id

schema = lookup_package_plugin(pkg.type).form_to_db_schema()
check_access('package_update', context, data_dict)

# get the schema
package_plugin = lookup_package_plugin(pkg.type)
try:
schema = package_plugin.form_to_db_schema_options({'type':'update',
'api':'api_version' in context})
except AttributeError:
schema = package_plugin.form_to_db_schema()

if 'api_version' not in context:
package_plugin.check_data_dict(data_dict, schema)

data, errors = validate(data_dict, schema, context)

if errors:
Expand All @@ -198,7 +207,6 @@ def package_update_validate(context, data_dict):
user = context['user']

id = data_dict["id"]
schema = context.get('schema') or default_update_package_schema()
model.Session.remove()
model.Session()._context = context

Expand All @@ -209,6 +217,14 @@ def package_update_validate(context, data_dict):
raise NotFound(_('Package was not found.'))
data_dict["id"] = pkg.id

# get the schema
package_plugin = lookup_package_plugin(pkg.type)
try:
schema = package_plugin.form_to_db_schema_options({'type':'update',
'api':'api_version' in context})
except AttributeError:
schema = package_plugin.form_to_db_schema()

check_access('package_update', context, data_dict)

data, errors = validate(data_dict, schema, context)
Expand Down Expand Up @@ -511,7 +527,6 @@ def package_update_rest(context, data_dict):
if not pkg:
raise NotFound


if id and id != pkg.id:
pkg_from_data = model.Package.get(id)
if pkg_from_data != pkg:
Expand All @@ -525,8 +540,7 @@ def package_update_rest(context, data_dict):

check_access('package_update_rest', context, dictized_package)

dictized_after = package_update(context, dictized_package)

dictized_after = get_action('package_update')(context, dictized_package)

pkg = context['package']

Expand Down

0 comments on commit 6f743b6

Please sign in to comment.