[virtualcommons-svn] commit/vcweb: alllee: fixing test and adding a double post test that's current
Status: Beta
Brought to you by:
alllee
From: Bitbucket <com...@bi...> - 2011-08-09 15:52:37
|
1 new changeset in vcweb: http://bitbucket.org/virtualcommons/vcweb/changeset/72f1cf82fd14/ changeset: 72f1cf82fd14 user: alllee date: 2011-08-09 17:52:23 summary: fixing test and adding a double post test that's currently failing, need to fix is_activity_available logic to properly check when activities have already been performed by a participant within the current time interval. affected #: 4 files (678 bytes) --- a/vcweb/core/models.py Mon Aug 08 16:28:36 2011 -0700 +++ b/vcweb/core/models.py Tue Aug 09 08:52:23 2011 -0700 @@ -397,7 +397,7 @@ u = User.objects.create_user(username=email, email=email, password=password) users.append(u) for user in users: - logger.debug("registering user %s", user) + #logger.debug("registering user %s", user) (p, created) = Participant.objects.get_or_create(user=user) # FIXME: instead of asking for the email suffix, perhaps we just append the institution URL to keep it simpler? if institution and p.institution != institution: --- a/vcweb/lighterprints/models.py Mon Aug 08 16:28:36 2011 -0700 +++ b/vcweb/lighterprints/models.py Tue Aug 09 08:52:23 2011 -0700 @@ -73,6 +73,9 @@ def get_carbon_footprint_level_parameter(): return Parameter.objects.get(name='carbon_footprint_level') +def get_carbon_footprint_level(group): + return GroupRoundDataValue.objects.get(group=group, parameter=get_carbon_footprint_level_parameter()) + def get_active_experiments(): return Experiment.objects.filter(experiment_metadata=get_lighterprints_experiment_metadata(), status__in=('ACTIVE', 'ROUND_IN_PROGRESS')) @@ -93,25 +96,27 @@ # how often can a participant participate in an activity? # whenever it falls within the ActivityAvailability schedule and if the participant # hasn't already performed this activity during this cycle. - now = datetime.datetime.now() + now = datetime.datetime.today() current_time = now.time() availabilities = ActivityAvailability.objects.filter(Q(activity=activity, activity__available_all_day=True) | Q(available_start_time__lte=current_time, available_end_time__gte=current_time)) -# FIXME: check if this participant has already participated in this activity - data_value_set = ParticipantRoundDataValue.objects.filter(parameter=get_activity_performed_parameter(), +# FIXME: check if this participant has already participated in this activity within this particular interval (for all +# day, today, for time slots, during this particular time slot). + already_performed = ParticipantRoundDataValue.objects.filter(parameter=get_activity_performed_parameter(), participant_group_relationship=participant_group_relationship, submitted=True, - date_created__lte=now, - date_created__gte=now) - return availabilities.count() > 0 and data_value_set.count() == 0 + date_created__lte=now) + return (availabilities.count() > 0) and (already_performed.count() == 0) def do_activity(activity=None, participant_group_relationship=None): if is_activity_available(activity, participant_group_relationship): + logger.debug("activity %s was available", activity) round_data = participant_group_relationship.group.current_round_data return ParticipantRoundDataValue.objects.create(parameter=get_activity_performed_parameter(), participant_group_relationship=participant_group_relationship, round_data=round_data, # FIXME: use activity unique name instead? - value=activity.pk + value=activity.pk, + submitted=True ) @receiver(signals.midnight_tick) @@ -131,16 +136,15 @@ logger.debug("received invalid signal from sender %s", sender) return # FIXME: See if we can push this logic up to core.. - logger.debug("initializing lighter prints") current_round_data = experiment.current_round_data carbon_footprint_level_parameter = get_carbon_footprint_level_parameter() +# only create the carbon footprint level parameter, the participant activity performed data values will be created each +# time. for group in experiment.group_set.all(): carbon_footprint_level_grdv = current_round_data.group_data_value_set.create(group=group, parameter=carbon_footprint_level_parameter) carbon_footprint_level_grdv.value = 1 carbon_footprint_level_grdv.save() - - def get_daily_carbon_savings(group): # grab all of yesterday's participant data values today = datetime.date.today() @@ -153,10 +157,6 @@ logger.debug("total carbon savings: %s", total_savings) return total_savings -def get_carbon_footprint_level(group): - return GroupRoundDataValue.objects.get(group=group, parameter=get_carbon_footprint_level_parameter()) - - def should_advance_level(group, level): if level < 3: daily_carbon_savings = get_daily_carbon_savings(group) --- a/vcweb/lighterprints/tests.py Mon Aug 08 16:28:36 2011 -0700 +++ b/vcweb/lighterprints/tests.py Tue Aug 09 08:52:23 2011 -0700 @@ -33,16 +33,14 @@ activity_performed_parameter = create_activity_performed_parameter() # initialize participant carbon savings for participant_group_relationship in ParticipantGroupRelationship.objects.filter(group__experiment=e): - for activity in available_activities(): + for activity in Activity.objects.filter(level=1): 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() - logger.debug("all activities performed: %s", - participant_group_relationship.participant_data_value_set.all()) 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('19.15')) + self.assertEqual(get_daily_carbon_savings(group), Decimal('58.20')) class DoActivityTest(BaseTest): @@ -64,5 +62,15 @@ }) logger.debug("response %s", response) self.assertEqual(response.status_code, 200) +# 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, + 'activity_id': activity.id + }) + self.assertEqual(response.status_code, 500) + + --- a/vcweb/lighterprints/views.py Mon Aug 08 16:28:36 2011 -0700 +++ b/vcweb/lighterprints/views.py Tue Aug 09 08:52:23 2011 -0700 @@ -83,8 +83,9 @@ activity = get_object_or_404(Activity, pk=activity_id) performed_activity = do_activity(activity=activity, participant_group_relationship=participant_group_relationship) logger.debug("performed activity %s", performed_activity) - return HttpResponse(dumps(performed_activity), content_type='text/javascript') - return HttpResponseBadRequest("Invalid activity post") + if performed_activity is not None: + return HttpResponse(dumps(performed_activity), content_type='text/javascript') + return HttpResponseBadRequest("Could not perform activity") @csrf_exempt 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. |