Skip to content

Commit

Permalink
[#1631] Add simple updated group activities
Browse files Browse the repository at this point in the history
Just:

    USER updated the group GROUP
    DESCRIPTION
    DATE

These could be improved, e.g.  specific messages for renamed group,
changed description of group, added dataset to group, etc.
  • Loading branch information
Sean Hammond committed Jan 13, 2012
1 parent 2e0b88f commit 9d4b9c7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -919,6 +919,10 @@ def render_new_group_activity(context, activity):
return render('activity_streams/new_group.html',
extra_vars = {'activity': activity})

def render_changed_group_activity(context, activity):
return render('activity_streams/changed_group.html',
extra_vars = {'activity': activity})

# Global dictionary mapping activity types to functions that render activity
# dicts to HTML snippets for including in HTML pages.
activity_renderers = {
Expand All @@ -927,6 +931,7 @@ def render_new_group_activity(context, activity):
'new user' : render_new_user_activity,
'changed user' : render_changed_user_activity,
'new group' : render_new_group_activity,
'changed group' : render_changed_group_activity,
}

def user_activity_list_html(context, data_dict):
Expand Down
11 changes: 11 additions & 0 deletions ckan/logic/action/update.py
Expand Up @@ -332,6 +332,17 @@ def group_update(context, data_dict):
for item in PluginImplementations(IGroupController):
item.edit(group)

activity_dict = {
'user_id': model.User.by_name(user.decode('utf8')).id,
'object_id': group.id,
'activity_type': 'changed group',
}
activity_dict['data'] = {'group': group_dictize(group, context)}
from ckan.logic.action.create import activity_create
activity_create(context, activity_dict)
# TODO: Also create an activity detail recording what exactly changed in
# the group.

if not context.get('defer_commit'):
model.repo.commit()
if errors:
Expand Down
23 changes: 23 additions & 0 deletions ckan/templates/activity_streams/changed_group.html
@@ -0,0 +1,23 @@
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:i18n="http://genshi.edgewall.org/i18n"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip=""
>
<xi:include href="../_util.html" />
<div>
<div style="font-weight:bold;">
${h.linked_user(activity.user_id)}
<span style="background:yellow;">updated</span>
the group
<a href="${h.url_for(controller='group', action='read', id=activity.data.group.name)}">
${activity.data.group.display_name}
</a>
<div style="color:#999;">
${h.truncate(activity.data.group.description, length=80, whole_word=True)}
</div>
${h.render_datetime(activity.timestamp, '%B %d %Y')}
</div>
</div>
</html>
51 changes: 49 additions & 2 deletions ckan/tests/models/test_activity.py
Expand Up @@ -6,7 +6,7 @@
import ckan.model as model
from ckan.logic.action.create import package_create, user_create, group_create
from ckan.logic.action.update import package_update, resource_update
from ckan.logic.action.update import user_update
from ckan.logic.action.update import user_update, group_update
from ckan.logic.action.delete import package_delete
from ckan.lib.dictization.model_dictize import resource_list_dictize
from ckan.logic.action.get import user_activity_list, activity_detail_list
Expand Down Expand Up @@ -690,7 +690,7 @@ def test_create_group(self):

before = record_details(user.id)

# Create a new package.
# Create a new group.
context = {'model': model, 'session': model.Session, 'user': user.name}
request_data = {'name': 'a-new-group', 'title': 'A New Group'}
group_created = group_create(context, request_data)
Expand All @@ -717,3 +717,50 @@ def test_create_group(self):
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'] and timestamp <= after['time'], \
str(activity['timestamp'])

def _update_group(self, group, user):
"""
Update the given group and test that the correct activity stream
item and detail are emitted.
"""
before = record_details(user.id)

# Update the group.
context = {'model': model, 'session': model.Session, 'user': user.name,
'allow_partial_update': True}
group_dict = {'id': group.id, 'title': 'edited'}
group_update(context, group_dict)

after = record_details(user.id)

# Find the new activity.
new_activities = find_new_activities(before, after)
assert len(new_activities) == 1, ("There should be 1 new activity in "
"the user's activity stream, but found %i" % len(new_activities))
activity = new_activities[0]

# Check that the new activity has the right attributes.
assert activity['object_id'] == group.id, str(activity['object_id'])
assert activity['user_id'] == user.id, str(activity['user_id'])
assert activity['activity_type'] == 'changed group', \
str(activity['activity_type'])
if not activity.has_key('id'):
assert False, "activity object has no id value"
# TODO: Test for the _correct_ revision_id value.
if not activity.has_key('revision_id'):
assert False, "activity has no revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'] and timestamp <= after['time'], \
str(activity['timestamp'])

def test_update_group(self):
"""
Test updated group activity stream.
Test that correct activity stream item and detail items are created
when groups are updated.
"""
for group in model.Session.query(model.Group).all():
self._update_group(group, user=self.sysadmin_user)

0 comments on commit 9d4b9c7

Please sign in to comment.