From d62120843ca7777feb2a5046450dfb74a2d10390 Mon Sep 17 00:00:00 2001 From: tobes Date: Mon, 30 Jul 2012 12:29:47 +0100 Subject: [PATCH] [#2536] move snippets into separate lib --- ckan/lib/activity_streams.py | 43 ++++++++++++++++++++++++++++++++++++ ckan/logic/action/get.py | 28 +++-------------------- 2 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 ckan/lib/activity_streams.py diff --git a/ckan/lib/activity_streams.py b/ckan/lib/activity_streams.py new file mode 100644 index 00000000000..c64a403341e --- /dev/null +++ b/ckan/lib/activity_streams.py @@ -0,0 +1,43 @@ +import ckan.lib.helpers as h + +def get_snippet_actor(activity, detail): + return h.linked_user(activity['user_id']) + +def get_snippet_dataset(activity, detail): + data = activity['data'] + return h.dataset_link(data.get('package') or data.get('dataset')) + +def get_snippet_tag(activity, detail): + return h.tag_link(detail['data']['tag']) + +def get_snippet_group(activity, detail): + return h.group_link(activity['data']['group']) + +def get_snippet_extra(activity, detail): + return '"%s"' % detail['data']['package_extra']['key'] + +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.relate_item_link(activity['data']['related']) + +def get_snippet_related_type(activity, detail): + # FIXME this needs to be translated + return activity['data']['related']['type'] + +snippet_functions = { + 'actor': get_snippet_actor, + 'dataset': get_snippet_dataset, + 'tag': get_snippet_tag, + 'group': get_snippet_group, + 'extra': get_snippet_extra, + 'resource': get_snippet_resource, + 'related_item': get_snippet_related_item, + 'related_type': get_snippet_related_type, +} + +def get_snippet(name, activity, detail): + ''' get the snippet for the required data ''' + return snippet_functions[name](activity, detail) diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index 03719f430aa..fbbe84ef9df 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -20,6 +20,7 @@ import ckan.plugins as plugins import ckan.lib.search as search import ckan.lib.plugins as lib_plugins +import ckan.lib.activity_streams as activity_streams import lib.helpers as h log = logging.getLogger('ckan.logic') @@ -1837,32 +1838,9 @@ def _activity_list_to_html(context, activity_stream): 'new related item': _("{actor} created the link to related {related_type} {related_item}"), } - def get_snippet(name): - ''' get the snippet for the required data ''' - if name == 'actor': - return h.linked_user(activity['user_id']) - elif name == 'dataset': - data = activity['data'] - return h.dataset_link(data.get('package') or data.get('dataset')) - elif name == 'tag': - return h.tag_link(detail['data']['tag']) - elif name == 'group': - return h.group_link(activity['data']['group']) - elif name == 'extra': - return '"%s"' % detail['data']['package_extra']['key'] - elif name == 'resource': - return h.resource_link(detail['data']['resource'], - activity['data']['package']['id']) - elif name == 'related_item': - return h.relate_item_link(activity['data']['related']) - elif name == 'related_type': - # FIXME this needs to be translated - return activity['data']['related']['type'] - else: - raise Exception('Unknown key') - activity_list = [] for activity in activity_stream: + detail = None activity_type = activity['activity_type'] # if package changed then we may have extra details if activity_type == 'changed package': @@ -1886,7 +1864,7 @@ def get_snippet(name): matches = re.findall('\{([^}]*)\}', activity_msg) data = {} for match in matches: - data[str(match)] = get_snippet(match) + data[str(match)] = activity_streams.get_snippet(match, activity, detail) activity_list.append(dict(msg=activity_msg, data=data, timestamp=activity['timestamp'])) return webhelpers.html.literal(_render('activity_streams/general.html', extra_vars = {'activities': activity_list}))