Skip to content

Commit

Permalink
Merge branch '3028-dashboard-activity-stream-filtering'
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Feb 13, 2013
2 parents 17ba23b + 7e43abb commit f2c8b12
Show file tree
Hide file tree
Showing 22 changed files with 1,070 additions and 117 deletions.
5 changes: 2 additions & 3 deletions ckan/controllers/group.py
Expand Up @@ -628,9 +628,8 @@ def activity(self, id, offset=0):

# Add the group's activity stream (already rendered to HTML) to the
# template context for the group/read.html template to retrieve later.
c.group_activity_stream = \
get_action('group_activity_list_html')(context,
{'id': c.group_dict['id'], 'offset': offset})
c.group_activity_stream = get_action('group_activity_list_html')(
context, {'id': c.group_dict['id'], 'offset': offset})

return render('group/activity_stream.html')

Expand Down
63 changes: 62 additions & 1 deletion ckan/controllers/user.py
Expand Up @@ -503,13 +503,74 @@ def activity(self, id, offset=0):

return render('user/activity_stream.html')

def _get_dashboard_context(self, filter_type=None, filter_id=None,
q=None):
'''Return a dict needed by the dashboard view to determine context.'''

def display_name(followee):
'''Return a display name for a user, group or dataset dict.'''
display_name = followee.get('display_name')
fullname = followee.get('fullname')
title = followee.get('title')
name = followee.get('name')
return display_name or fullname or title or name

if (filter_type and filter_id):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {'id': filter_id}
followee = None

action_functions = {
'dataset': 'package_show',
'user': 'user_show',
'group': 'group_show'
}
action_function = logic.get_action(action_functions.get(filter_type))
# Is this a valid type?
if action_function is None:
raise abort(404, _('Follow item not found'))
try:
followee = action_function(context, data_dict)
except NotFound:
abort(404, _('{0} not found').format(filter_type))
except NotAuthorized:
abort(401, _('Unauthorized to read {0} {1}').format(
filter_type, id))

if followee is not None:
return {
'filter_type': filter_type,
'q': q,
'context': display_name(followee),
'selected_id': followee.get('id'),
'dict': followee,
}

return {
'filter_type': filter_type,
'q': q,
'context': _('Everything'),
'selected_id': False,
'dict': None,
}

def dashboard(self, id=None, offset=0):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {'id': id, 'user_obj': c.userobj, 'offset': offset}
self._setup_template_variables(context, data_dict)

c.dashboard_activity_stream = h.dashboard_activity_stream(id, offset)
q = request.params.get('q', u'')
filter_type = request.params.get('type', u'')
filter_id = request.params.get('name', u'')

c.followee_list = get_action('followee_list')(
context, {'id': c.userobj.id, 'q': q})
c.dashboard_activity_stream_context = self._get_dashboard_context(
filter_type, filter_id, q)
c.dashboard_activity_stream = h.dashboard_activity_stream(
id, filter_type, filter_id, offset)

# Mark the user's new activities as old whenever they view their
# dashboard page.
Expand Down
2 changes: 2 additions & 0 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -220,6 +220,7 @@ def package_dictize(pkg, context):
q = q.where(resource_group.c.package_id == pkg.id)
result = _execute_with_revision(q, res_rev, context)
result_dict["resources"] = resource_list_dictize(result, context)
result_dict['num_resources'] = len(result_dict.get('resources', []))

#tags
tag_rev = model.package_tag_revision_table
Expand All @@ -229,6 +230,7 @@ def package_dictize(pkg, context):
).where(tag_rev.c.package_id == pkg.id)
result = _execute_with_revision(q, tag_rev, context)
result_dict["tags"] = d.obj_list_dictize(result, context, lambda x: x["name"])
result_dict['num_tags'] = len(result_dict.get('tags', []))

# Add display_names to tags. At first a tag's display_name is just the
# same as its name, but the display_name might get changed later (e.g.
Expand Down
24 changes: 20 additions & 4 deletions ckan/lib/helpers.py
Expand Up @@ -1257,21 +1257,37 @@ def user_in_org_or_group(group_id):
return len(query.all()) != 0


def dashboard_activity_stream(user_id, offset=0):
def dashboard_activity_stream(user_id, filter_type=None, filter_id=None,
offset=0):
'''Return the dashboard activity stream of the given user.
:param user_id: the id of the user
:type user_id: string
:param filter_type: the type of thing to filter by
:type filter_type: string
:param filter_id: the id of item to filter by
:type filter_id: string
:returns: an activity stream as an HTML snippet
:rtype: string
'''
import ckan.logic as logic
context = {'model': model, 'session': model.Session, 'user': c.user}
return logic.get_action('dashboard_activity_list_html')(context,
{'id': user_id,
'offset': offset})

if filter_type:
action_functions = {
'dataset': 'package_activity_list_html',
'user': 'user_activity_list_html',
'group': 'group_activity_list_html'
}
action_function = logic.get_action(action_functions.get(filter_type))
return action_function(context, {'id': filter_id, 'offset': offset})
else:
return logic.get_action('dashboard_activity_list_html')(
context, {'id': user_id, 'offset': offset})


def recently_changed_packages_activity_stream():
Expand Down

0 comments on commit f2c8b12

Please sign in to comment.