[virtualcommons-svn] commit/vcweb: alllee: updated sanitation model code for pollutifying an experi
Status: Beta
Brought to you by:
alllee
From: Bitbucket <com...@bi...> - 2011-07-11 22:55:41
|
1 new changeset in vcweb: http://bitbucket.org/virtualcommons/vcweb/changeset/d60d5771e1ae/ changeset: d60d5771e1ae user: alllee date: 2011-07-12 00:55:25 summary: updated sanitation model code for pollutifying an experiment (periodically, will eventually be invoked via celery and with the appropriate pollution_amount) and generating a pollutified string. Also fixed some incorrect pk linkages with the sanitation initial_data affected #: 4 files (2.5 KB) --- a/vcweb/core/models.py Sat Jul 09 01:05:12 2011 -0700 +++ b/vcweb/core/models.py Mon Jul 11 15:55:25 2011 -0700 @@ -396,6 +396,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) (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: @@ -464,7 +465,9 @@ # FIXME: record previous mappings in activity log. self.group_set.all().delete() # seed the initial group. - current_group = self.group_set.create(number=1, max_size=self.experiment_configuration.max_group_size) + max_group_size = self.experiment_configuration.max_group_size + logger.debug("creating group with max size %d", max_group_size) + current_group = self.group_set.create(number=1, max_size=max_group_size) participants = list(self.participant_set.all()) if randomize: random.shuffle(participants) --- a/vcweb/sanitation/fixtures/initial_data.json Sat Jul 09 01:05:12 2011 -0700 +++ b/vcweb/sanitation/fixtures/initial_data.json Mon Jul 11 15:55:25 2011 -0700 @@ -48,7 +48,7 @@ "current_round_elapsed_time": 0, "current_round_sequence_number": 1, "duration": "1h", - "experiment_configuration": 1, + "experiment_configuration": 3, "experiment_metadata": [ "sanitation" ], @@ -61,5 +61,31 @@ }, "model": "core.experiment", "pk": 3 + }, + { + "fields": { + "name":"sanitation.pollution", + "experiment_metadata": 3, + "creator": 4, + "type": "int", + "date_created": "2011-01-01 15:13:07", + "scope": "group", + "description": "The integer values for this parameter represent locations in a text corpus (i.e., single coordinates OK)" + }, + "model": "core.parameter", + "pk": 6 + }, + { + "fields": { + "name":"sanitation.collectedpollution", + "experiment_metadata": 3, + "creator": 4, + "type": "int", + "date_created": "2011-01-01 15:13:07", + "scope": "participant", + "description": "The integer values for this parameter are informal foreign keys to the sanitation.pollution location data value pks" + }, + "model": "core.parameter", + "pk": 7 } ] --- a/vcweb/sanitation/models.py Sat Jul 09 01:05:12 2011 -0700 +++ b/vcweb/sanitation/models.py Mon Jul 11 15:55:25 2011 -0700 @@ -1,21 +1,42 @@ -from django.db import models - +from vcweb.core.models import * import random -def pollutify(resource_string, pollution_amount, pollution_symbol, group=None): - resource_index = xrange(1,(len(resource_string) + 1)) - pollution_locations = sorted(random.sample(resource_index, pollution_amount)) -# pollution_parameter = Parameter.objects.get(name='sanitation.pollution') +def pollutify(experiment, pollution_amount, resource_string_length): + for group in experiment.group_set.all(): + resource_index = xrange(1,(resource_string_length + 1)) + pollution_locations = sorted(random.sample(resource_index, pollution_amount)) + pollution_parameter = Parameter.objects.get(name='sanitation.pollution') + round_data = group.current_round_data + for i, location in enumerate(pollution_locations): +# generate a GroupRoundDataValue for this location (this can be shortened) + # round_data.group_data_value_set.create(group=group, parameter=pollution_parameter, value=location) + grdv = group.data_value_set.create(round_data=round_data, parameter=pollution_parameter, value=location) + #grdv = GroupRoundDataValue.objects.create(group=group, round_data=round_data, parameter=pollution_parameter, value=location) + logger.debug("grdv is %s", grdv) + +def pollution_url_maker(pk, pollution_symbol): + pollution_url = "<a id='%d' class='pollution' href='#'>%s</a>" % (pk, pollution_symbol) + #pollution_url = '<a id='' href="#"> ' + pollution_symbol + ' </a>' + # use jQuery to bind a click handler to all DOM elements with class='pollution', + # look at jquery selector documentation + #$('.pollution').click(function() { + # ajax call here + # }); + # will probably need to use jQuery .delegate or .live to handle this properly + return pollution_url + +# returns a new string with pollution embedded in it +def get_pollution_string(group, resource_string, pollution_symbol="@"): resource_string_list = list(resource_string) - offset = len(pollution_symbol) - for i, location in enumerate(pollution_locations): -# generate a GroupRoundDataValue for this location (this can be shortened) -# grdv = GroupRoundDataValue.objects.create(group=group, round_data=group.current_round_data, parameter=pollution_parameter, value=location) -# logger.debug("grdv is %s", grdv) -# FIXME: since we're inserting more than one character we need to be careful not to insert into a location where +# XXX: since we're inserting more than one character we need to be careful not to insert into a location where # we appended text.. - resource_string_list.insert(location + (i * offset), pollution_symbol) + for i, grdv in enumerate(GroupRoundDataValue.objects.filter(group=group, round_data=group.current_round_data, is_active=True)): + pollution_url = pollution_url_maker(grdv.pk, pollution_symbol) + offset = len(pollution_url) + offset_location = grdv.value + (i * offset) + resource_string_list.insert(offset_location, pollution_url) return ''.join(resource_string_list) + --- a/vcweb/sanitation/views.py Sat Jul 09 01:05:12 2011 -0700 +++ b/vcweb/sanitation/views.py Mon Jul 11 15:55:25 2011 -0700 @@ -1,5 +1,8 @@ # importing external methods and classes +#from vcweb.core.models import Experiment from vcweb.core.models import Experiment +from vcweb.core.models import ParticipantExperimentRelationship +from vcweb.sanitation.models import pollutify from django.shortcuts import render_to_response, redirect from django.template.context import RequestContext import logging @@ -7,10 +10,6 @@ #FIXME come from database(user) treatment = "In-group" -current_location = "consent" - -#FIXME comes from type of experiment -sequence = ["consent","survey","instructions","quiz","play"] #FIXME Globals... comes from somewhere logger = logging.getLogger(__name__) @@ -23,32 +22,12 @@ #FIXME rename object to something like "experimental settings" #FIXME replace 'one week' with a calculation of duration consent = [venue, symbol,'one week', growth_rate] -resource = "Sanitation is vital for health: Readers of a prestigious medical journal were recently asked to name the greatest medical advance in the last century and a half. The result: better sanitation. In nineteenth-century Europe and North America, diarrhoea, cholera, and typhoid spread through poor sanitation was the leading cause of childhood illness and death; today, such deaths are rare in these regions. In developing countries, however, they are all too common, and recent research suggests that poor sanitation and hygiene are either the chief or the underlying cause in over half of the annual 10 million child deaths. Compelling, evidence-based analysis shows that hygiene and sanitation are among the most cost-effective public health interventions to reduce childhood mortality. Access to a toilet alone can reduce child diarrhoeal deaths by over 30 percent, and hand-washing by more than 40 percent." +resource_string = "Sanitation is vital for health: Readers of a prestigious medical journal were recently asked to name the greatest medical advance in the last century and a half. The result: better sanitation. In nineteenth-century Europe and North America, diarrhoea, cholera, and typhoid spread through poor sanitation was the leading cause of childhood illness and death; today, such deaths are rare in these regions. In developing countries, however, they are all too common, and recent research suggests that poor sanitation and hygiene are either the chief or the underlying cause in over half of the annual 10 million child deaths. Compelling, evidence-based analysis shows that hygiene and sanitation are among the most cost-effective public health interventions to reduce childhood mortality. Access to a toilet alone can reduce child diarrhoeal deaths by over 30 percent, and hand-washing by more than 40 percent." -#FIXME mode to models, make method "current_game_state" that returns game_state -resource_index = range(1,(len(resource) + 1)) pollution_amount = random.randint(1,200) -pollution = random.sample(resource_index, pollution_amount) -game_state = "" -for i, char in enumerate(resource): - if i in pollution: -#FIXME turn pollution symbol into - symbol_url = str("" + symbol + "") - game_state = game_state + symbol_url - game_state = game_state + char -#FIXME list of index out of range - -def next_url(absolute_url,current_location,sequence): - a = sequence.index(current_location) - a = a + 1 - if a == len(sequence): - a = 0 - next_page = sequence[a] - else: - next_page = sequence[a] - logger.debug("next_page: %s is valid", next_page) - return "/%s/%s" % (absolute_url, next_page) +pollution_symbol = '@' +#game_state = pollutify(resource_string, pollution_amount, pollution_symbol) #FIXME propose that quiz has a limit of 10 questions #FIXME Find a better place to put the declaration of questions quiz/survey @@ -98,11 +77,11 @@ context_instance=RequestContext(request)) #Participant - #Pages in experiment sequence + def consent(request, experiment): logger.debug("handling consent") consent = ['Introduction to Global Health Class', symbol,'one week', growth_rate] - next_url = "instructions" + next_url = "consent" return render_to_response('sanitation/consent.html', locals(), context_instance=RequestContext(request)) def survey(request, experiment): @@ -129,9 +108,16 @@ participant = request.user.participant experiment = Experiment.objects.get(pk=experiment_id) -# absolute_url = experiment.namespace -# n_url = next_url(absolute_url,current_location,sequence) + participantrelationship = ParticipantExperimentRelationship.objects.get(id = participant.id) + current_location = participantrelationship.current_location.lower() + if request.method == 'POST': + current_location = request.POST['next'] + participantrelationship.current_location = current_location + participantrelationship.save() + + + sequence = ["consent","survey","instructions","quiz","play"] if current_location in sequence: logger.debug("current location %s is valid", current_location) location_method = globals()[current_location] 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. |