Skip to content

Commit

Permalink
Merge branch 'ckan-2609-delete-related-items'
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed Jan 21, 2016
2 parents 1a7885e + 6441e0c commit 602b6f0
Show file tree
Hide file tree
Showing 40 changed files with 34 additions and 1,570 deletions.
1 change: 0 additions & 1 deletion ckan/config/routing.py
Expand Up @@ -108,7 +108,6 @@ def make_map():
'resource',
'tag',
'group',
'related',
'revision',
'licenses',
'rating',
Expand Down
4 changes: 0 additions & 4 deletions ckan/controllers/api.py
Expand Up @@ -281,7 +281,6 @@ def list(self, ver=None, register=None, subregister=None, id=None):
'group': 'group_list',
'dataset': 'package_list',
'tag': 'tag_list',
'related': 'related_list',
'licenses': 'license_list',
('dataset', 'relationships'): 'package_relationships_list',
('dataset', 'revisions'): 'package_revision_list',
Expand Down Expand Up @@ -309,7 +308,6 @@ def show(self, ver=None, register=None, subregister=None,
'revision': 'revision_show',
'group': 'group_show_rest',
'tag': 'tag_show_rest',
'related': 'related_show',
'dataset': 'package_show_rest',
('dataset', 'relationships'): 'package_relationships_list',
}
Expand Down Expand Up @@ -344,7 +342,6 @@ def create(self, ver=None, register=None, subregister=None,
'group': 'group_create_rest',
'dataset': 'package_create_rest',
'rating': 'rating_create',
'related': 'related_create',
('dataset', 'relationships'): 'package_relationship_create_rest',
}
for type in model.PackageRelationship.get_all_types():
Expand Down Expand Up @@ -459,7 +456,6 @@ def delete(self, ver=None, register=None, subregister=None,
action_map = {
'group': 'group_delete',
'dataset': 'package_delete',
'related': 'related_delete',
('dataset', 'relationships'): 'package_relationship_delete_rest',
}
for type in model.PackageRelationship.get_all_types():
Expand Down
11 changes: 2 additions & 9 deletions ckan/controllers/package.py
Expand Up @@ -368,7 +368,6 @@ def read(self, id):

# used by disqus plugin
c.current_package_id = c.pkg.id
c.related_count = c.pkg.related_count

# can the resources be previewed?
for resource in c.pkg_dict['resources']:
Expand Down Expand Up @@ -475,7 +474,6 @@ def history(self, id):

package_type = c.pkg_dict['type'] or 'dataset'

c.related_count = c.pkg.related_count
return render(
self._history_template(c.pkg_dict.get('type', package_type)),
extra_vars={'dataset_type': package_type})
Expand Down Expand Up @@ -783,7 +781,6 @@ def edit(self, id, data=None, errors=None, error_summary=None):

self._setup_template_variables(context, {'id': id},
package_type=package_type)
c.related_count = c.pkg.related_count

# we have already completed stage 1
form_vars['stage'] = ['active']
Expand Down Expand Up @@ -1094,8 +1091,6 @@ def resource_read(self, id, resource_id):
c.datastore_api = '%s/api/action' % \
config.get('ckan.site_url', '').rstrip('/')

c.related_count = c.pkg.related_count

c.resource['can_be_previewed'] = self._resource_preview(
{'resource': c.resource, 'package': c.package})

Expand Down Expand Up @@ -1218,7 +1213,6 @@ def followers(self, id=None):
c.followers = get_action('dataset_follower_list')(
context, {'id': c.pkg_dict['id']})

c.related_count = c.pkg.related_count
dataset_type = c.pkg.type or 'dataset'
except NotFound:
abort(404, _('Dataset not found'))
Expand Down Expand Up @@ -1299,9 +1293,8 @@ def activity(self, id):
c.pkg_dict = get_action('package_show')(context, data_dict)
c.pkg = context['package']
c.package_activity_stream = get_action(
'package_activity_list_html')(context,
{'id': c.pkg_dict['id']})
c.related_count = c.pkg.related_count
'package_activity_list_html')(
context, {'id': c.pkg_dict['id']})
dataset_type = c.pkg_dict['type'] or 'dataset'
except NotFound:
abort(404, _('Dataset not found'))
Expand Down
2 changes: 0 additions & 2 deletions ckan/controllers/user.py
Expand Up @@ -121,8 +121,6 @@ def read(self, id=None):
'include_datasets': True,
'include_num_followers': True}

context['with_related'] = True

self._setup_template_variables(context, data_dict)

# The legacy templates have the user's activity stream on the user
Expand Down
31 changes: 0 additions & 31 deletions ckan/lib/activity_streams.py
Expand Up @@ -48,13 +48,6 @@ def get_snippet_resource(activity, detail):
return h.resource_link(detail['data']['resource'],
activity['data']['package']['id'])

def get_snippet_related_item(activity, detail):
return h.related_item_link(activity['data']['related'])

def get_snippet_related_type(activity, detail):
# FIXME this needs to be translated
return activity['data']['related']['type']

# activity_stream_string_*() functions return translatable string
# representations of activity types, the strings contain placeholders like
# {user}, {dataset} etc. to be replaced with snippets from the get_snippet_*()
Expand All @@ -81,13 +74,6 @@ def activity_stream_string_changed_resource(context, activity):
def activity_stream_string_changed_user(context, activity):
return _("{actor} updated their profile")

def activity_stream_string_changed_related_item(context, activity):
if activity['data'].get('dataset'):
return _("{actor} updated the {related_type} {related_item} of the "
"dataset {dataset}")
else:
return _("{actor} updated the {related_type} {related_item}")

def activity_stream_string_deleted_group(context, activity):
return _("{actor} deleted the group {group}")

Expand Down Expand Up @@ -125,9 +111,6 @@ def activity_stream_string_new_user(context, activity):
def activity_stream_string_removed_tag(context, activity):
return _("{actor} removed the tag {tag} from the dataset {dataset}")

def activity_stream_string_deleted_related_item(context, activity):
return _("{actor} deleted the related item {related_item}")

def activity_stream_string_follow_dataset(context, activity):
return _("{actor} started following {dataset}")

Expand All @@ -137,13 +120,6 @@ def activity_stream_string_follow_user(context, activity):
def activity_stream_string_follow_group(context, activity):
return _("{actor} started following {group}")

def activity_stream_string_new_related_item(context, activity):
if activity['data'].get('dataset'):
return _("{actor} added the {related_type} {related_item} to the "
"dataset {dataset}")
else:
return _("{actor} added the {related_type} {related_item}")

# A dictionary mapping activity snippets to functions that expand the snippets.
activity_snippet_functions = {
'actor': get_snippet_actor,
Expand All @@ -154,8 +130,6 @@ def activity_stream_string_new_related_item(context, activity):
'organization': get_snippet_organization,
'extra': get_snippet_extra,
'resource': get_snippet_resource,
'related_item': get_snippet_related_item,
'related_type': get_snippet_related_type,
}

# A dictionary mapping activity types to functions that return translatable
Expand All @@ -168,7 +142,6 @@ def activity_stream_string_new_related_item(context, activity):
'changed package_extra': activity_stream_string_changed_package_extra,
'changed resource': activity_stream_string_changed_resource,
'changed user': activity_stream_string_changed_user,
'changed related item': activity_stream_string_changed_related_item,
'deleted group': activity_stream_string_deleted_group,
'deleted organization': activity_stream_string_deleted_organization,
'deleted package': activity_stream_string_deleted_package,
Expand All @@ -181,11 +154,9 @@ def activity_stream_string_new_related_item(context, activity):
'new resource': activity_stream_string_new_resource,
'new user': activity_stream_string_new_user,
'removed tag': activity_stream_string_removed_tag,
'deleted related item': activity_stream_string_deleted_related_item,
'follow dataset': activity_stream_string_follow_dataset,
'follow user': activity_stream_string_follow_user,
'follow group': activity_stream_string_follow_group,
'new related item': activity_stream_string_new_related_item,
}

# A dictionary mapping activity types to the icons associated to them
Expand All @@ -206,11 +177,9 @@ def activity_stream_string_new_related_item(context, activity):
'new resource': 'file',
'new user': 'user',
'removed tag': 'tag',
'deleted related item': 'picture',
'follow dataset': 'sitemap',
'follow user': 'user',
'follow group': 'group',
'new related item': 'picture',
'changed organization': 'briefcase',
'deleted organization': 'briefcase',
'new organization': 'briefcase',
Expand Down
19 changes: 0 additions & 19 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -71,16 +71,6 @@ def resource_list_dictize(res_list, context):

return sorted(result_list, key=lambda x: x["position"])

def related_list_dictize(related_list, context):
result_list = []
for res in related_list:
related_dict = related_dictize(res, context)
result_list.append(related_dict)
if context.get('sorted'):
return result_list
return sorted(result_list, key=lambda x: x["created"], reverse=True)


def extras_dict_dictize(extras_dict, context):
result_list = []
for name, extra in extras_dict.iteritems():
Expand Down Expand Up @@ -127,9 +117,6 @@ def resource_dictize(res, context):
resource['url'] = u'http://' + url.lstrip('/')
return resource

def related_dictize(rel, context):
return d.table_dictize(rel, context)


def _execute(q, table, context):
'''
Expand Down Expand Up @@ -600,12 +587,6 @@ def user_dictize(user, context, include_password_hash=False):
model = context['model']
session = model.Session

if context.get('with_related'):
related_items = session.query(model.Related).\
filter(model.Related.owner_id==user.id).all()
result_dict['related_items'] = related_list_dictize(related_items,
context)

return result_dict

def task_status_dictize(task_status, context):
Expand Down
7 changes: 0 additions & 7 deletions ckan/lib/dictization/model_save.py
Expand Up @@ -449,13 +449,6 @@ def user_dict_save(user_dict, context):
return user


def related_dict_save(related_dict, context):
model = context['model']
session = context['session']

return d.table_dict_save(related_dict,model.Related, context)


def package_api_to_dict(api1_dict, context):

package = context.get("package")
Expand Down
15 changes: 0 additions & 15 deletions ckan/lib/helpers.py
Expand Up @@ -1217,14 +1217,6 @@ def resource_link(resource_dict, package_id):
return link_to(text, url)


def related_item_link(related_item_dict):
text = related_item_dict.get('title', '')
url = url_for(controller='related',
action='read',
id=related_item_dict['id'])
return link_to(text, url)


def tag_link(tag):
url = url_for(controller='tag', action='read', id=tag['name'])
return link_to(tag.get('title', tag['name']), url)
Expand Down Expand Up @@ -2037,12 +2029,6 @@ def get_site_statistics():
stats['group_count'] = len(logic.get_action('group_list')({}, {}))
stats['organization_count'] = len(
logic.get_action('organization_list')({}, {}))
result = model.Session.execute(
'''select count(*) from related r
left join related_dataset rd on r.id = rd.related_id
where rd.status = 'active' or rd.id is null''').first()[0]
stats['related_count'] = result

return stats

_RESOURCE_FORMATS = None
Expand Down Expand Up @@ -2182,7 +2168,6 @@ def get_translated(data_dict, field):
'dataset_link',
'resource_display_name',
'resource_link',
'related_item_link',
'tag_link',
'group_link',
'dump_json',
Expand Down
4 changes: 0 additions & 4 deletions ckan/lib/plugins.py
Expand Up @@ -275,10 +275,6 @@ def setup_template_variables(self, context, data_dict):
maintain.deprecate_context_item('licences', 'Use `c.licenses` instead')
c.is_sysadmin = ckan.authz.is_sysadmin(c.user)

if c.pkg:
# Used by the disqus plugin
c.related_count = c.pkg.related_count

if context.get('revision_id') or context.get('revision_date'):
if context.get('revision_id'):
rev = base.model.Session.query(base.model.Revision) \
Expand Down
80 changes: 0 additions & 80 deletions ckan/logic/action/create.py
Expand Up @@ -467,86 +467,6 @@ def package_create_default_resource_views(context, data_dict):
create_datastore_views=create_datastore_views)


def related_create(context, data_dict):
'''Add a new related item to a dataset.
You must provide your API key in the Authorization header.
:param title: the title of the related item
:type title: string
:param type: the type of the related item, e.g. ``'Application'``,
``'Idea'`` or ``'Visualisation'``
:type type: string
:param id: the id of the related item (optional)
:type id: string
:param description: the description of the related item (optional)
:type description: string
:param url: the URL to the related item (optional)
:type url: string
:param image_url: the URL to the image for the related item (optional)
:type image_url: string
:param dataset_id: the name or id of the dataset that the related item
belongs to (optional)
:type dataset_id: string
:returns: the newly created related item
:rtype: dictionary
'''
model = context['model']
session = context['session']
user = context['user']
userobj = model.User.get(user)

_check_access('related_create', context, data_dict)

data_dict["owner_id"] = userobj.id
data, errors = _validate(
data_dict, ckan.logic.schema.default_related_schema(), context)
if errors:
model.Session.rollback()
raise ValidationError(errors)

related = model_save.related_dict_save(data, context)
if not context.get('defer_commit'):
model.repo.commit_and_remove()

dataset_dict = None
if 'dataset_id' in data_dict:
dataset = model.Package.get(data_dict['dataset_id'])
dataset.related.append(related)
model.repo.commit_and_remove()
dataset_dict = ckan.lib.dictization.table_dictize(dataset, context)

session.flush()

related_dict = model_dictize.related_dictize(related, context)
activity_dict = {
'user_id': userobj.id,
'object_id': related.id,
'activity_type': 'new related item',
}
activity_dict['data'] = {
'related': related_dict,
'dataset': dataset_dict,
}
activity_create_context = {
'model': model,
'user': user,
'defer_commit': True,
'ignore_auth': True,
'session': session
}
logic.get_action('activity_create')(activity_create_context,
activity_dict)
session.commit()

context["related"] = related
context["id"] = related.id
log.debug('Created object %s' % related.title)
return related_dict


def package_relationship_create(context, data_dict):
'''Create a relationship between two datasets (packages).
Expand Down

0 comments on commit 602b6f0

Please sign in to comment.