Skip to content

Commit

Permalink
Merge branch 'master' into 1251-resource-view
Browse files Browse the repository at this point in the history
Conflicts:
	ckan/new_tests/logic/action/test_create.py
  • Loading branch information
vitorbaptista committed May 5, 2014
2 parents 9767d33 + 3f88691 commit 8ddcb61
Show file tree
Hide file tree
Showing 24 changed files with 347 additions and 198 deletions.
3 changes: 2 additions & 1 deletion ckan/config/middleware.py
Expand Up @@ -345,7 +345,8 @@ def __init__(self, app, config):

def __call__(self, environ, start_response):
path = environ['PATH_INFO']
if path == '/_tracking':
method = environ.get('REQUEST_METHOD')
if path == '/_tracking' and method == 'POST':
# do the tracking
# get the post data
payload = environ['wsgi.input'].read()
Expand Down
5 changes: 0 additions & 5 deletions ckan/logic/__init__.py
Expand Up @@ -259,7 +259,6 @@ def check_access(action, context, data_dict=None):
authorized to call the named action
'''
action = new_authz.clean_action_name(action)

# Auth Auditing. We remove this call from the __auth_audit stack to show
# we have called the auth function
Expand Down Expand Up @@ -341,8 +340,6 @@ def get_action(action):
:rtype: callable
'''
# clean the action names
action = new_authz.clean_action_name(action)

if _actions:
if not action in _actions:
Expand All @@ -365,7 +362,6 @@ def get_action(action):
if (hasattr(v, '__call__')
and (v.__module__ == module_path
or hasattr(v, '__replaced'))):
k = new_authz.clean_action_name(k)
_actions[k] = v

# Whitelist all actions defined in logic/action/get.py as
Expand All @@ -380,7 +376,6 @@ def get_action(action):
fetched_actions = {}
for plugin in p.PluginImplementations(p.IActions):
for name, auth_function in plugin.get_actions().items():
name = new_authz.clean_action_name(name)
if name in resolved_action_plugins:
raise Exception(
'The action %r is already implemented in %r' % (
Expand Down
6 changes: 3 additions & 3 deletions ckan/logic/action/create.py
Expand Up @@ -216,13 +216,12 @@ def package_create(context, data_dict):
def resource_create(context, data_dict):
'''Appends a new resource to a datasets list of resources.
:param package_id: id of package that the resource needs
should be added to.
:param package_id: id of package that the resource should be added to.
:type package_id: string
:param url: url of resource
:type url: string
:param revision_id: (optional)
:type revisiion_id: string
:type revision_id: string
:param description: (optional)
:type description: string
:param format: (optional)
Expand Down Expand Up @@ -263,6 +262,7 @@ def resource_create(context, data_dict):

package_id = _get_or_bust(data_dict, 'package_id')
data_dict.pop('package_id')
_get_or_bust(data_dict, 'url')

pkg_dict = _get_action('package_show')(context, {'id': package_id})

Expand Down
9 changes: 5 additions & 4 deletions ckan/logic/action/get.py
Expand Up @@ -1106,10 +1106,11 @@ def _group_or_org_show(context, data_dict, is_org=False):
{'model': model, 'session': model.Session},
{'id': group_dict['id']})

if schema:
group_dict, errors = lib_plugins.plugin_validate(
group_plugin, context, group_dict, schema,
'organization_show' if is_org else 'group_show')
if schema is None:
schema = logic.schema.default_show_group_schema()
group_dict, errors = lib_plugins.plugin_validate(
group_plugin, context, group_dict, schema,
'organization_show' if is_org else 'group_show')
return group_dict


Expand Down
16 changes: 16 additions & 0 deletions ckan/logic/schema.py
Expand Up @@ -328,6 +328,22 @@ def default_update_group_schema():
schema["name"] = [ignore_missing, group_name_validator, unicode]
return schema

def default_show_group_schema():
schema = default_group_schema()

# make default show schema behave like when run with no validation
schema['num_followers'] = []
schema['created'] = []
schema['display_name'] = []
schema['extras'] = {'__extras': [ckan.lib.navl.validators.keep_extras]}
schema['package_count'] = []
schema['packages'] = {'__extras': [ckan.lib.navl.validators.keep_extras]}
schema['revision_id'] = []
schema['state'] = []
schema['users'] = {'__extras': [ckan.lib.navl.validators.keep_extras]}

return schema


def default_related_schema():
schema = {
Expand Down
68 changes: 44 additions & 24 deletions ckan/logic/validators.py
Expand Up @@ -62,14 +62,34 @@ def package_id_not_changed(value, context):
return value

def int_validator(value, context):
if isinstance(value, int):
return value
'''
Return an integer for value, which may be a string in base 10 or
a numeric type (e.g. int, long, float, Decimal, Fraction). Return
None for None or empty/all-whitespace string values.
:raises: ckan.lib.navl.dictization_functions.Invalid for other
inputs or non-whole values
'''
if value is None:
return None
if hasattr(value, 'strip') and not value.strip():
return None

try:
if value.strip() == '':
return None
return int(value)
except (AttributeError, ValueError), e:
raise Invalid(_('Invalid integer'))
whole, part = divmod(value, 1)
except TypeError:
try:
return int(value)
except ValueError:
pass
else:
if not part:
try:
return int(whole)
except TypeError:
pass # complex number: fail like int(complex) does

raise Invalid(_('Invalid integer'))

def natural_number_validator(value, context):
value = int_validator(value, context)
Expand Down Expand Up @@ -163,10 +183,10 @@ def resource_id_exists(value, context):


def user_id_exists(user_id, context):
"""Raises Invalid if the given user_id does not exist in the model given
'''Raises Invalid if the given user_id does not exist in the model given
in the context, otherwise returns the given user_id.
"""
'''
model = context['model']
session = context['session']

Expand All @@ -193,10 +213,10 @@ def user_id_or_name_exists(user_id_or_name, context):
return user_id_or_name

def group_id_exists(group_id, context):
"""Raises Invalid if the given group_id does not exist in the model given
'''Raises Invalid if the given group_id does not exist in the model given
in the context, otherwise returns the given group_id.
"""
'''
model = context['model']
session = context['session']

Expand All @@ -207,10 +227,10 @@ def group_id_exists(group_id, context):


def related_id_exists(related_id, context):
"""Raises Invalid if the given related_id does not exist in the model
'''Raises Invalid if the given related_id does not exist in the model
given in the context, otherwise returns the given related_id.
"""
'''
model = context['model']
session = context['session']

Expand All @@ -220,23 +240,23 @@ def related_id_exists(related_id, context):
return related_id

def group_id_or_name_exists(reference, context):
"""
'''
Raises Invalid if a group identified by the name or id cannot be found.
"""
'''
model = context['model']
result = model.Group.get(reference)
if not result:
raise Invalid(_('That group name or ID does not exist.'))
return reference

def activity_type_exists(activity_type):
"""Raises Invalid if there is no registered activity renderer for the
'''Raises Invalid if there is no registered activity renderer for the
given activity_type. Otherwise returns the given activity_type.
This just uses object_id_validators as a lookup.
very safe.
"""
'''
if activity_type in object_id_validators:
return activity_type
else:
Expand Down Expand Up @@ -275,7 +295,7 @@ def resource_id_exists(value, context):
}

def object_id_validator(key, activity_dict, errors, context):
"""Validate the 'object_id' value of an activity_dict.
'''Validate the 'object_id' value of an activity_dict.
Uses the object_id_validators dict (above) to find and call an 'object_id'
validator function for the given activity_dict's 'activity_type' value.
Expand All @@ -287,7 +307,7 @@ def object_id_validator(key, activity_dict, errors, context):
Raises Invalid if there is no object_id_validator for the activity_dict's
'activity_type' value.
"""
'''
activity_type = activity_dict[('activity_type',)]
if object_id_validators.has_key(activity_type):
object_id = activity_dict[('object_id',)]
Expand Down Expand Up @@ -337,15 +357,15 @@ def name_validator(value, context):
return value

def package_name_validator(key, data, errors, context):
model = context["model"]
session = context["session"]
package = context.get("package")
model = context['model']
session = context['session']
package = context.get('package')

query = session.query(model.Package.name).filter_by(name=data[key])
if package:
package_id = package.id
else:
package_id = data.get(key[:-1] + ("id",))
package_id = data.get(key[:-1] + ('id',))
if package_id and package_id is not missing:
query = query.filter(model.Package.id <> package_id)
result = query.first()
Expand Down Expand Up @@ -665,7 +685,7 @@ def tag_not_in_vocabulary(key, tag_dict, errors, context):
return

def url_validator(key, data, errors, context):
""" Checks that the provided value (if it is present) is a valid URL """
''' Checks that the provided value (if it is present) is a valid URL '''
import urlparse
import string

Expand Down
10 changes: 0 additions & 10 deletions ckan/new_authz.py
Expand Up @@ -57,7 +57,6 @@ def _build(self):

for key, v in module.__dict__.items():
if not key.startswith('_'):
key = clean_action_name(key)
# Whitelist all auth functions defined in
# logic/auth/get.py as not requiring an authorized user,
# as well as ensuring that the rest do. In both cases, do
Expand All @@ -75,7 +74,6 @@ def _build(self):
fetched_auth_functions = {}
for plugin in p.PluginImplementations(p.IAuthFunctions):
for name, auth_function in plugin.get_auth_functions().items():
name = clean_action_name(name)
if name in resolved_auth_function_plugins:
raise Exception(
'The auth function %r is already implemented in %r' % (
Expand Down Expand Up @@ -106,13 +104,6 @@ def auth_functions_list():
return _AuthFunctions.keys()


def clean_action_name(action_name):
''' Used to convert old style action names into new style ones '''
new_action_name = re.sub('package', 'dataset', action_name)
# CS: bad_spelling ignore
return re.sub('licence', 'license', new_action_name)


def is_sysadmin(username):
''' Returns True is username is a sysadmin '''
user = _get_user(username)
Expand Down Expand Up @@ -157,7 +148,6 @@ def is_authorized(action, context, data_dict=None):
if context.get('ignore_auth'):
return {'success': True}

action = clean_action_name(action)
auth_function = _AuthFunctions.get(action)
if auth_function:
username = context.get('user')
Expand Down

0 comments on commit 8ddcb61

Please sign in to comment.