[virtualcommons-svn] commit/vcweb: alllee: adding jsonp callback validator from
Status: Beta
Brought to you by:
alllee
From: Bitbucket <com...@bi...> - 2011-06-28 19:17:58
|
1 new changeset in vcweb: http://bitbucket.org/virtualcommons/vcweb/changeset/54dd750d33c9/ changeset: 54dd750d33c9 user: alllee date: 2011-06-28 21:17:53 summary: adding jsonp callback validator from https://github.com/tav/scripts/blob/master/validate_jsonp.py and basic jsonp support to JSONResponseMixin that expects a callback parameter. affected #: 6 files (6.7 KB) --- a/vcweb/core/auth.py Mon Jun 27 23:59:03 2011 -0700 +++ b/vcweb/core/auth.py Tue Jun 28 12:17:53 2011 -0700 @@ -1,23 +1,16 @@ -''' - -General VCWEB authentication backend to allow users to login with their email -as their username. - -code adapted from http://djangosnippets.org/snippets/74/ -and http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/ - -''' - - from django.contrib.auth.backends import ModelBackend from django.contrib.auth.models import User from django.core.validators import email_re import logging logger = logging.getLogger(__name__) - # FIXME: check for and handle Participant experiment auth codes. class AuthenticationBackend(ModelBackend): + ''' + General VCWEB authentication backend to allow users to login with their email as their username. + Code adapted from http://djangosnippets.org/snippets/74/ and + http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/ + ''' def authenticate(self, username=None, password=None): if email_re.search(username): try: @@ -25,6 +18,8 @@ if user.check_password(password): return user # check for and handle participants logging in with an auth code? - except User.DoesNotExist: - return None + except User.DoesNotExist as e: + logger.debug("no user found with username %s: %s", username, e) + else: + logger.debug("username failed the email regex: %s", username) return None --- a/vcweb/core/models.py Mon Jun 27 23:59:03 2011 -0700 +++ b/vcweb/core/models.py Tue Jun 28 12:17:53 2011 -0700 @@ -22,16 +22,15 @@ FIXME: getting a bit monolithically unwieldy. Consider splitting into models subdirectory """ -@receiver(signals.second_tick, sender=None) -def second_tick_handler(sender, time=None, **kwargs): +@receiver(signals.minute_tick, sender=None) +def minute_tick_handler(sender, time=None, **kwargs): """ - tick handlers. handles each second tick. Might rethink this and use timed / + tick handlers. handles each minute tick. Might rethink this and use timed / delayed tasks in celery execute at the end of each round for controlled experiments and for longer-scale experiments use 1 minute granularity for performance sake. """ - logger.debug("handling second tick signal at %s", time) - logger.debug("kwargs: %s", kwargs) + logger.debug("handling minute tick signal at %s with kwargs %s", time, kwargs) # inspect all active experiments and update their time left Experiment.objects.increment_elapsed_time(status='ROUND_IN_PROGRESS') @@ -161,11 +160,11 @@ def get_all_active(self): return self.filter(status='ACTIVE') - def increment_elapsed_time(self, status='ROUND_IN_PROGRESS'): + def increment_elapsed_time(self, status='ROUND_IN_PROGRESS', amount=60): if status: es = self.filter(status=status) - es.update(current_round_elapsed_time=models.F('current_round_elapsed_time') + 1, - total_elapsed_time=models.F('total_elapsed_time') + 1) + es.update(current_round_elapsed_time=models.F('current_round_elapsed_time') + amount, + total_elapsed_time=models.F('total_elapsed_time') + amount) # check each experiment's total_elapsed_time against the total allotted time and # issue round_stopped signals to experiments that need to be stopped. for experiment in es.all(): --- a/vcweb/core/signals.py Mon Jun 27 23:59:03 2011 -0700 +++ b/vcweb/core/signals.py Tue Jun 28 12:17:53 2011 -0700 @@ -4,7 +4,7 @@ experiment_started = Signal(providing_args=["experiment", "time", "experimenter"]) round_started = Signal(providing_args=["experiment", 'time', 'round_configuration']) round_ended = Signal(providing_args=['experiment', 'time', 'round_configuration']) -second_tick = Signal(providing_args=['time']) +minute_tick = Signal(providing_args=['time']) post_login = Signal(providing_args=['user']) post_logout = Signal(providing_args=['user']) --- a/vcweb/core/tasks.py Mon Jun 27 23:59:03 2011 -0700 +++ b/vcweb/core/tasks.py Tue Jun 28 12:17:53 2011 -0700 @@ -1,19 +1,18 @@ +from celery.decorators import periodic_task +from datetime import datetime, timedelta +from vcweb.core import signals + ''' -The updater module is invoked periodically from an external process to set up the signaling and timing / processing -of experiments in progress. +The updater module is invoked periodically from an external process to set up the +signaling and timing / processing of experiments in progress. @author: alllee ''' -from celery.decorators import periodic_task -from datetime import datetime, timedelta -from vcweb.core import signals +@periodic_task(run_every=timedelta(seconds=60), ignore_result=True) +def every_minute(): + # use signal or just update experiment instance models directly here? + signals.minute_tick.send(sender=None, time=datetime.now()) -@periodic_task(run_every=timedelta(seconds=1), ignore_result=True) -def every_second(): - # use signal or just update experiment instance models directly here? - signals.second_tick.send(sender=None, time=datetime.now()) - - --- a/vcweb/core/views.py Mon Jun 27 23:59:03 2011 -0700 +++ b/vcweb/core/views.py Tue Jun 28 12:17:53 2011 -0700 @@ -22,6 +22,7 @@ from vcweb.core.models import (Participant, Experimenter, Experiment, Institution, is_participant, is_experimenter) from vcweb.core.decorators import anonymous_required, experimenter_required, participant_required from vcweb.core import unicodecsv +from vcweb.core.validate_jsonp import is_valid_jsonp_callback_value import itertools import logging logger = logging.getLogger(__name__) @@ -44,13 +45,21 @@ def get_json_response(self, content, **httpresponse_kwargs): "Construct an `HttpResponse` object." + callback = self.request.GET.get('callback', '') + if is_valid_jsonp_callback_value(callback): + content = '%s(%s)' % (callback, content) + logger.debug("returning json content %s", content) return HttpResponse(content, content_type='application/json', **httpresponse_kwargs) def convert_context_to_json(self, context, context_key='object_list', **kwargs): - "Convert the context dictionary into a JSON object" - return dumps(context[context_key]) + """ + Converts the data object associated with context_key in the context dict + into a JSON object and returns it. If context_key is None, converts the + entire context dict. + """ + return dumps( context if context_key is None else context[context_key] ) class AnonymousMixin(object): """ provides the anonymous_required decorator """ 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. |