Skip to content

Commit

Permalink
Merge branch 'master' into feature-1797-webstore-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
rufuspollock committed Feb 20, 2012
2 parents 695c7bd + 277dc15 commit fc6fef4
Show file tree
Hide file tree
Showing 46 changed files with 2,569 additions and 269 deletions.
2 changes: 1 addition & 1 deletion ckan/controllers/api.py
Expand Up @@ -172,7 +172,7 @@ def action(self, logic_function):
'message': _('Access denied')}
return_dict['success'] = False
return self._finish(403, return_dict, content_type='json')
except NotFound:
except NotFound, e:
return_dict['error'] = {'__type': 'Not Found Error',
'message': _('Not found')}
if e.extra_msg:
Expand Down
3 changes: 2 additions & 1 deletion ckan/controllers/group.py
Expand Up @@ -205,7 +205,8 @@ def read(self, id):
group_type = self._get_group_type(id.split('@')[0])
context = {'model': model, 'session': model.Session,
'user': c.user or c.author,
'schema': self._form_to_db_schema(group_type=type)}
'schema': self._form_to_db_schema(group_type=type),
'for_view': True}
data_dict = {'id': id}
q = c.q = request.params.get('q', '') # unicode format (decoded from utf8)

Expand Down
9 changes: 6 additions & 3 deletions ckan/controllers/package.py
Expand Up @@ -24,7 +24,7 @@
from ckan.logic import tuplize_dict, clean_dict, parse_params, flatten_to_string_key
from ckan.lib.dictization import table_dictize
from ckan.lib.i18n import get_lang
from ckan.plugins import PluginImplementations, IDatasetForm
from ckan.plugins import PluginImplementations, IDatasetForm, IPackageController
import ckan.forms
import ckan.authz
import ckan.rating
Expand Down Expand Up @@ -66,6 +66,8 @@ def register_pluggable_behaviour(map):
exception will be raised.
"""
global _default_controller_behaviour
_default_controller_behaviour = None
_controller_behaviour_for.clear()

# Create the mappings and register the fallback behaviour if one is found.
for plugin in PluginImplementations(IDatasetForm):
Expand Down Expand Up @@ -247,7 +249,7 @@ def pager_url(q=None, page=None):
search_extras[param] = value

context = {'model': model, 'session': model.Session,
'user': c.user or c.author}
'user': c.user or c.author, 'for_view': True}

data_dict = {
'q':q,
Expand Down Expand Up @@ -281,7 +283,8 @@ def read(self, id):
package_type = self._get_package_type(id.split('@')[0])
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'extras_as_string': True,
'schema': self._form_to_db_schema(package_type=package_type)}
'schema': self._form_to_db_schema(package_type=package_type),
'for_view': True}
data_dict = {'id': id}

# interpret @<revision_id> or @<date> suffix
Expand Down
6 changes: 1 addition & 5 deletions ckan/forms/common.py
Expand Up @@ -452,16 +452,12 @@ def get_configured(self):
class TagField(formalchemy.Field):
@property
def raw_value(self):
tag_objects = self.model.tags
tag_objects = self.model.get_tags()
tag_names = [tag.name for tag in tag_objects]
return tag_names

def sync(self):
if not self.is_readonly():
# Note: You might think that you could just assign
# self.model.tags with tag objects, but the model
# (add_stateful_versioned_m2m) doesn't support this -
# you must edit each PackageTag individually.
self._update_tags()

def _update_tags(self):
Expand Down
2 changes: 1 addition & 1 deletion ckan/forms/package_dict.py
Expand Up @@ -47,7 +47,7 @@ def get_package_dict(pkg=None, blank=False, fs=None, user_editable_groups=None):
if field.renderer.name.endswith('-extras'):
indict[field.renderer.name] = dict(pkg.extras) if pkg else {}
if field.renderer.name.endswith('-tags'):
indict[field.renderer.name] = ','.join([tag.name for tag in pkg.tags]) if pkg else ''
indict[field.renderer.name] = ','.join([tag.name for tag in pkg.get_tags()]) if pkg else ''
if field.renderer.name.endswith('-resources'):
indict[field.renderer.name] = [dict([(key, getattr(res, key)) for key in model.Resource.get_columns()]) for res in pkg.resources] if pkg else []

Expand Down
6 changes: 3 additions & 3 deletions ckan/lib/create_test_data.py
Expand Up @@ -161,7 +161,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
tag = model.Tag(name=tag_name)
cls.tag_names.append(tag_name)
model.Session.add(tag)
pkg.tags.append(tag)
pkg.add_tag(tag)
model.Session.flush()
elif attr == 'groups':
model.Session.flush()
Expand Down Expand Up @@ -388,8 +388,8 @@ def create(cls, auth_profile=""):

for obj in [pkg2, tag1, tag2, tag3]:
model.Session.add(obj)
pkg1.tags = [tag1, tag2, tag3]
pkg2.tags = [ tag1, tag3 ]
pkg1.add_tags([tag1, tag2, tag3])
pkg2.add_tags([ tag1, tag3 ])
cls.tag_names = [ t.name for t in (tag1, tag2, tag3) ]
pkg1.license_id = u'other-open'
pkg2.license_id = u'cc-nc' # closed license
Expand Down
47 changes: 33 additions & 14 deletions ckan/lib/dictization/model_dictize.py
@@ -1,5 +1,6 @@
from pylons import config
from sqlalchemy.sql import select, and_
from ckan.plugins import PluginImplementations, IDatasetForm, IPackageController, IGroupController
import datetime

from ckan.model import PackageRevision
Expand Down Expand Up @@ -194,12 +195,26 @@ def package_dictize(pkg, context):
# type
result_dict['type']= pkg.type

# licence
if pkg.license and pkg.license.url:
result_dict['license_url']= pkg.license.url
result_dict['license_title']= pkg.license.title.split('::')[-1]
elif pkg.license:
result_dict['license_title']= pkg.license.title
else:
result_dict['license_title']= pkg.license_id

# creation and modification date
result_dict['metadata_modified'] = pkg.metadata_modified.isoformat() \
if pkg.metadata_modified else None
result_dict['metadata_created'] = pkg.metadata_created.isoformat() \
if pkg.metadata_created else None

if context.get('for_view'):
for item in PluginImplementations(IPackageController):
result_dict = item.before_view(result_dict)


return result_dict

def _get_members(context, group, member_type):
Expand Down Expand Up @@ -242,6 +257,10 @@ def group_dictize(group, context):

context['with_capacity'] = False

if context.get('for_view'):
for item in PluginImplementations(IGroupController):
result_dict = item.before_view(result_dict)

return result_dict

def tag_list_dictize(tag_list, context):
Expand All @@ -262,7 +281,7 @@ def tag_dictize(tag, context):
result_dict = table_dictize(tag, context)

result_dict["packages"] = obj_list_dictize(
tag.packages_ordered, context)
tag.packages, context)

return result_dict

Expand Down Expand Up @@ -340,7 +359,7 @@ def package_to_api1(pkg, context):
dictized.pop("revision_timestamp")

dictized["groups"] = [group["name"] for group in dictized["groups"]]
dictized["tags"] = [tag["name"] for tag in dictized["tags"]]
dictized["tags"] = [tag["name"] for tag in dictized["tags"] if not tag.get('vocabulary_id')]
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized['notes_rendered'] = ckan.misc.MarkdownFormat().to_html(pkg.notes)
Expand Down Expand Up @@ -397,7 +416,7 @@ def package_to_api2(pkg, context):
dictized["groups"] = [group["id"] for group in dictized["groups"]]
dictized.pop("revision_timestamp")

dictized["tags"] = [tag["name"] for tag in dictized["tags"]]
dictized["tags"] = [tag["name"] for tag in dictized["tags"] if not tag.get('vocabulary_id')]
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])

Expand Down Expand Up @@ -441,24 +460,24 @@ def package_to_api2(pkg, context):
dictized['relationships'] = relationships
return dictized

def vocabulary_dictize(vocabulary, context):
vocabulary_dict = table_dictize(vocabulary, context)
return vocabulary_dict

def vocabulary_list_dictize(vocabulary_list, context):
return [vocabulary_dictize(vocabulary, context)
for vocabulary in vocabulary_list]

def activity_dictize(activity, context):
activity_dict = table_dictize(activity, context)
return activity_dict

def activity_list_dictize(activity_list, context):
activity_dicts = []
for activity in activity_list:
activity_dict = activity_dictize(activity, context)
activity_dicts.append(activity_dict)
return activity_dicts
return [activity_dictize(activity, context) for activity in activity_list]

def activity_detail_dictize(activity_detail, context):
return table_dictize(activity_detail, context)

def activity_detail_list_dictize(activity_detail_list, context):
activity_detail_dicts = []
for activity_detail in activity_detail_list:
activity_detail_dict = activity_detail_dictize(activity_detail,
context)
activity_detail_dicts.append(activity_detail_dict)
return activity_detail_dicts
return [activity_detail_dictize(activity_detail, context)
for activity_detail in activity_detail_list]
40 changes: 33 additions & 7 deletions ckan/lib/dictization/model_save.py
Expand Up @@ -138,7 +138,6 @@ def group_extras_save(extras_dicts, context):
return result_dict

def package_tag_list_save(tag_dicts, package, context):

allow_partial_update = context.get("allow_partial_update", False)
if not tag_dicts and allow_partial_update:
return
Expand All @@ -156,13 +155,13 @@ def package_tag_list_save(tag_dicts, package, context):
pt.state in ['deleted', 'pending-deleted'] ]
)

tag_names = set()
tag_name_vocab = set()
tags = set()
for tag_dict in tag_dicts:
if tag_dict.get('name') not in tag_names:
if (tag_dict.get('name'), tag_dict.get('vocabulary_id')) not in tag_name_vocab:
tag_obj = table_dict_save(tag_dict, model.Tag, context)
tags.add(tag_obj)
tag_names.add(tag_obj.name)
tag_name_vocab.add((tag_obj.name, tag_obj.vocabulary_id))

# 3 cases
# case 1: currently active but not in new list
Expand All @@ -173,14 +172,14 @@ def package_tag_list_save(tag_dicts, package, context):
else:
package_tag.state = 'deleted'

# in new list but never used before
# case 2: in new list but never used before
for tag in tags - set(tag_package_tag.keys()):
state = 'pending' if pending else 'active'
package_tag_obj = model.PackageTag(package, tag, state)
session.add(package_tag_obj)
tag_package_tag[tag] = package_tag_obj

# in new list and already used but in deleted state
# case 3: in new list and already used but in deleted state
for tag in tags.intersection(set(tag_package_tag_inactive.keys())):
state = 'pending' if pending else 'active'
package_tag = tag_package_tag[tag]
Expand Down Expand Up @@ -463,7 +462,6 @@ def activity_dict_save(activity_dict, context):

model = context['model']
session = context['session']

user_id = activity_dict['user_id']
object_id = activity_dict['object_id']
revision_id = activity_dict['revision_id']
Expand All @@ -479,3 +477,31 @@ def activity_dict_save(activity_dict, context):
# TODO: Handle activity details.

return activity_obj

def vocabulary_dict_save(vocabulary_dict, context):
model = context['model']
session = context['session']
vocabulary_name = vocabulary_dict['name']

vocabulary_obj = model.Vocabulary(vocabulary_name)
session.add(vocabulary_obj)

return vocabulary_obj

def vocabulary_dict_update(vocabulary_dict, context):

model = context['model']
session = context['session']

vocabulary_obj = model.vocabulary.Vocabulary.get(vocabulary_dict['id'])
vocabulary_obj.name = vocabulary_dict['name']

return vocabulary_obj

def tag_dict_save(tag_dict, context):
model = context['model']
tag = context.get('tag')
if tag:
tag_dict['id'] = tag.id
tag = table_dict_save(tag_dict, model.Tag, context)
return tag
2 changes: 1 addition & 1 deletion ckan/lib/search/query.py
Expand Up @@ -171,7 +171,7 @@ def run(self, query=[], fields={}, options=None, **kwargs):
if options.all_fields:
results['results'] = [r.as_dict() for r in results['results']]
else:
results['results'] = [r.name for r in results['results']]
results['results'] = [r['name'] for r in results['results']]

self.count = results['count']
self.results = results['results']
Expand Down
54 changes: 52 additions & 2 deletions ckan/logic/action/create.py
Expand Up @@ -18,18 +18,23 @@
package_api_to_dict,
package_dict_save,
user_dict_save,
vocabulary_dict_save,
tag_dict_save,
activity_dict_save)

from ckan.lib.dictization.model_dictize import (group_dictize,
package_dictize,
user_dictize,
vocabulary_dictize,
tag_dictize,
activity_dictize)


from ckan.logic.schema import (default_create_package_schema,
default_resource_schema,
default_create_relationship_schema,
default_create_activity_schema)
default_create_vocabulary_schema,
default_create_activity_schema,
default_create_tag_schema)

from ckan.logic.schema import default_group_schema, default_user_schema
from ckan.lib.navl.dictization_functions import validate
Expand Down Expand Up @@ -340,6 +345,32 @@ def group_create_rest(context, data_dict):

return group_dict

def vocabulary_create(context, data_dict):

model = context['model']
user = context['user']
schema = context.get('schema') or default_create_vocabulary_schema()

model.Session.remove()
model.Session()._context = context

check_access('vocabulary_create', context, data_dict)

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

if errors:
model.Session.rollback()
raise ValidationError(errors, package_error_summary(errors))

vocabulary = vocabulary_dict_save(data, context)

if not context.get('defer_commit'):
model.repo.commit()

log.debug('Created Vocabulary %s' % str(vocabulary.name))

return vocabulary_dictize(vocabulary, context)

def activity_create(context, activity_dict, ignore_auth=False):
'''Create a new activity stream activity and return a dictionary
representation of it.
Expand Down Expand Up @@ -383,3 +414,22 @@ def package_relationship_create_rest(context, data_dict):
relationship_dict = package_relationship_create(context, data_dict)
return relationship_dict

def tag_create(context, tag_dict):
'''Create a new tag and return a dictionary representation of it.'''

model = context['model']

check_access('tag_create', context, tag_dict)

schema = context.get('schema') or default_create_tag_schema()
data, errors = validate(tag_dict, schema, context)
if errors:
raise ValidationError(errors)

tag = tag_dict_save(tag_dict, context)

if not context.get('defer_commit'):
model.repo.commit()

log.debug("Created tag '%s' " % tag)
return tag_dictize(tag, context)

0 comments on commit fc6fef4

Please sign in to comment.