[virtualcommons-svn] commit/vcweb: alllee: implementing group discussion board and added test, stil
Status: Beta
Brought to you by:
alllee
From: Bitbucket <com...@bi...> - 2011-08-11 19:07:05
|
1 new changeset in vcweb: http://bitbucket.org/virtualcommons/vcweb/changeset/96e3a0cdb9af/ changeset: 96e3a0cdb9af user: alllee date: 2011-08-11 21:06:56 summary: implementing group discussion board and added test, still need to add coverage for chat messages affected #: 3 files (3.2 KB) --- a/vcweb/core/models.py Wed Aug 10 17:33:08 2011 -0700 +++ b/vcweb/core/models.py Thu Aug 11 12:06:56 2011 -0700 @@ -1184,6 +1184,7 @@ Many-to-many relationship entity storing a participant, group, their participant number in that group, the round in which they joined the group, and the datetime that they joined the group. """ +# FIXME: should also add a participant_identifier field here in case we want to use something other than numbers..? participant_number = models.PositiveIntegerField() participant = models.ForeignKey(Participant, related_name='participant_group_relationship_set') group = models.ForeignKey(Group, related_name = 'participant_group_relationship_set') --- a/vcweb/lighterprints/tests.py Wed Aug 10 17:33:08 2011 -0700 +++ b/vcweb/lighterprints/tests.py Thu Aug 11 12:06:56 2011 -0700 @@ -37,17 +37,36 @@ activity_performed = participant_group_relationship.participant_data_value_set.create(round_data=current_round_data, parameter=activity_performed_parameter, experiment=e) activity_performed.value = activity.id activity_performed.save() + # FIXME: sender parameter doesn't really matter here, just pass self in as the sender update_active_experiments(self) for group in e.group_set.all(): self.assertEqual(get_carbon_footprint_level(group).value, 2) - self.assertEqual(get_daily_carbon_savings(group), Decimal('58.20')) + self.assertEqual(get_daily_carbon_savings(group), Decimal('55.70')) + +class GroupActivityTest(BaseTest): + def test_group_activity_json(self): + e = self.experiment + e.activate() + e.start_round() + participant_group_relationship = ParticipantGroupRelationship.objects.filter(group__experiment=e)[0] + # do every activity in level 1 for this particular participant + activity_performed_parameter = get_activity_performed_parameter() + current_round_data = e.current_round_data + for activity in Activity.objects.filter(level=1): + activity_performed = participant_group_relationship.participant_data_value_set.create(submitted=True, round_data=current_round_data, parameter=activity_performed_parameter, experiment=e) + activity_performed.value = activity.id + activity_performed.save() + group_activity_json = get_group_activity_json(participant_group_relationship) + logger.debug("group activity json: %s", group_activity_json) + import simplejson as json + group_activity_dict = json.loads(group_activity_json) + logger.debug("group activity dict: %s", group_activity_dict) class DoActivityTest(BaseTest): def test_view(self): logger.debug("testing do activity view") e = self.experiment - create_activity_performed_parameter() e.activate() e.start_round() for participant_group_relationship in ParticipantGroupRelationship.objects.filter(group__experiment=e): @@ -57,7 +76,7 @@ activity = activity_availability.activity logger.debug("participant %s performing activity %s", participant_group_relationship.participant, activity) response = self.client.post('/lighterprints/api/do-activity', { - 'participant_group_relationship_id': participant_group_relationship.id, + 'participant_group_id': participant_group_relationship.id, 'activity_id': activity.id }) logger.debug("response %s", response) @@ -65,7 +84,7 @@ # try to do the same activity again logger.debug("XXX: all activity performed parameters: %s", ParticipantRoundDataValue.objects.filter(parameter=get_activity_performed_parameter())) response = self.client.post('/lighterprints/api/do-activity', { - 'participant_group_relationship_id': participant_group_relationship.id, + 'participant_group_id': participant_group_relationship.id, 'activity_id': activity.pk }) self.assertEqual(response.status_code, 400) --- a/vcweb/lighterprints/views.py Wed Aug 10 17:33:08 2011 -0700 +++ b/vcweb/lighterprints/views.py Thu Aug 11 12:06:56 2011 -0700 @@ -7,16 +7,22 @@ from django.views.generic.list import BaseListView, MultipleObjectTemplateResponseMixin from vcweb.core.forms import LoginForm -from vcweb.core.models import (ChatMessage, Experiment, ParticipantGroupRelationship) +from vcweb.core.models import (ChatMessage, Experiment, ParticipantGroupRelationship, ParticipantRoundDataValue) from vcweb.core.views import JSONResponseMixin, dumps, set_authentication_token # FIXME: move ChatForm to core? from vcweb.lighterprints.forms import ActivityForm, ChatForm -from vcweb.lighterprints.models import (Activity, is_activity_available, do_activity, get_lighterprints_experiment_metadata) +from vcweb.lighterprints.models import (Activity, is_activity_available, do_activity, get_lighterprints_experiment_metadata, get_activity_performed_parameter) import collections import logging logger = logging.getLogger(__name__) +def to_activity_dict(activity, attrs=('pk', 'name', 'summary', 'display_name', 'description', 'savings', 'url', 'available_all_day', 'level', 'group_activity', 'icon_url', 'time_remaining')): + activity_as_dict = {} + for attr_name in attrs: + activity_as_dict[attr_name] = getattr(activity, attr_name, None) + return activity_as_dict + class ActivityListView(JSONResponseMixin, MultipleObjectTemplateResponseMixin, BaseListView): model = Activity @@ -29,9 +35,7 @@ for activity in all_activities: activity_by_level[activity.level].append(activity) #activity_as_dict = collections.OrderedDict() - activity_as_dict = {} - for attr_name in ('pk', 'name', 'summary', 'display_name', 'description', 'savings', 'url', 'available_all_day', 'level', 'group_activity', 'icon_url', 'time_remaining'): - activity_as_dict[attr_name] = getattr(activity, attr_name, None) + activity_as_dict = to_activity_dict(activity) try: if self.request.user.is_authenticated(): # authenticated request, figure out if this activity is available @@ -52,6 +56,32 @@ else: return MultipleObjectTemplateResponseMixin.render_to_response(self, context) +class DiscussionBoardView(JSONResponseMixin, MultipleObjectTemplateResponseMixin, BaseListView): + model = ChatMessage + template_name = 'discussion_board.html' + def get_queryset(self): + # FIXME: stubbed out for now, passing in the participant id for the time + # being + # participant = self.request.user.participant + participant_group_id = self.kwargs['participant_group_id'] +# FIXME: will change once we have proper auth set up + self.participant_group_relationship = get_object_or_404(ParticipantGroupRelationship, pk=participant_group_id) + self.group = self.participant_group_relationship.group + return ChatMessage.objects.filter(participant_group_relationship__group = self.group) + + def render_to_response(self, context, **response_kwargs): + if self.request.GET.get('format', 'html') == 'json': + return JSONResponseMixin.render_to_response(self, context, context_key='group_activity') + else: + return MultipleObjectTemplateResponseMixin.render_to_response(self, context) + + def get_context_data(self, **kwargs): + context = super(DiscussionBoardView, self).get_context_data(**kwargs) + context['group'] = self.group + context['participant_group_relationship'] = self.participant_group_relationship + return context + + class ActivityDetailView(JSONResponseMixin, BaseDetailView): template_name = 'lighterprints/activity_detail.html' @@ -82,6 +112,30 @@ class DoActivityView(FormView): pass +def get_group_activity_json(participant_group_relationship, number_of_activities=5): + group = participant_group_relationship.group + chat_messages = [] + for chat_message in ChatMessage.objects.filter(participant_group_relationship__group=group).order_by('-date_created'): + chat_messages.append({ + 'date_created': chat_message.date_created, + 'message': chat_message.message, + 'participant_number': participant_group_relationship.participant_number, + 'participant': participant_group_relationship.participant + }) + group_activity = [] + performed_activities = ParticipantRoundDataValue.objects.filter(participant_group_relationship__group=group, submitted=True, parameter=get_activity_performed_parameter()).order_by('-date_created') + for activity_prdv in performed_activities[:number_of_activities]: + # FIXME: change this to activity name if we decide to use names instead of + # pks + activity = Activity.objects.get(pk=activity_prdv.value) + performed_activity_dict = to_activity_dict(activity, attrs=('pk', 'name', 'icon_url', 'summary', 'savings')) + performed_activity_dict['date_performed'] = activity_prdv.date_created + group_activity.append(performed_activity_dict) + return dumps({ + 'chat_messages': chat_messages, + 'recent_activity': group_activity + }) + @csrf_exempt def perform_activity(request): logger.debug("performing activity") @@ -107,29 +161,12 @@ chat_message = ChatMessage.objects.create(participant_group_relationship=participant_group_relationship, message=message, round_data=group.current_round_data) logger.debug("Participant %s created chat message %s", request.user.participant, chat_message) + # FIXME: add recent Activity performed data as well as all ChatMessages + # sent, ordered by date. content = dumps(ChatMessage.objects.filter(participant_group_relationship__group=group)) return HttpResponse(content, content_type='text/javascript') return HttpResponseBadRequest(dumps({'response': "Invalid chat message post"})) -class DiscussionBoardView(JSONResponseMixin, MultipleObjectTemplateResponseMixin, BaseListView): - model = ChatMessage - template_name = 'discussion_board.html' - def get_queryset(self): - # FIXME: stubbed out for now, passing in the participant id for the time - # being - # participant = self.request.user.participant - participant_group_id = self.kwargs['participant_group_id'] -# FIXME: will change once we have proper auth set up - self.participant_group_relationship = get_object_or_404(ParticipantGroupRelationship, pk=participant_group_id) - self.group = self.participant_group_relationship.group - return ChatMessage.objects.filter(participant_group_relationship__group = self.group) - - def get_context_data(self, **kwargs): - context = super(DiscussionBoardView, self).get_context_data(**kwargs) - context['group'] = self.group - context['participant_group_relationship'] = self.participant_group_relationship - return context - @csrf_exempt def login(request): form = LoginForm(request.POST or None) Repository URL: https://bitbucket.org/virtualcommons/vcweb/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. |