def generate_run(request, questionnaire_id, subject_id=None): """ A view that can generate a RunID instance anonymously, and then redirect to the questionnaire itself. It uses a Subject with the givenname of 'Anonymous' and the surname of 'User'. If this Subject does not exist, it will be created. This can be used with a URL pattern like: (r'^take/(?P<questionnaire_id>[0-9]+)/$', 'questionnaire.views.generate_run'), """ qu = get_object_or_404(Questionnaire, id=questionnaire_id) qs = qu.questionsets()[0] if subject_id is not None: su = get_object_or_404(Subject, pk=subject_id) else: su = Subject.objects.filter(givenname='Anonymous', surname='User')[0:1] if su: su = su[0] else: su = Subject(givenname='Anonymous', surname='User') su.save() str_to_hash = "".join( map(lambda i: chr(random.randint(0, 255)), range(16)) ) str_to_hash += settings.SECRET_KEY key = md5(str_to_hash).hexdigest() run = RunInfo(subject=su, random=key, runid=key, questionset=qs) run.save() if not use_session: kwargs = {'runcode': key} else: kwargs = {} request.session['runcode'] = key questionnaire_start.send(sender=None, runinfo=run, questionnaire=qu) return HttpResponseRedirect(reverse('questionnaire', kwargs=kwargs))
def generate_run(request, questionnaire_id, subject_id=None): """ A view that can generate a RunID instance anonymously, and then redirect to the questionnaire itself. It uses a Subject with the givenname of 'Anonymous' and the surname of 'User'. If this Subject does not exist, it will be created. This can be used with a URL pattern like: (r'^take/(?P<questionnaire_id>[0-9]+)/$', 'questionnaire.views.generate_run'), """ qu = get_object_or_404(Questionnaire, id=questionnaire_id) qs = qu.questionsets()[0] if subject_id is not None: su = get_object_or_404(Subject, pk=subject_id) else: su = Subject.objects.filter(givenname='Anonymous', surname='User')[0:1] if su: su = su[0] else: su = Subject(givenname='Anonymous', surname='User') su.save() str_to_hash = "".join(map(lambda i: chr(random.randint(0, 255)), range(16))) str_to_hash += settings.SECRET_KEY key = md5(str_to_hash).hexdigest() run = RunInfo(subject=su, random=key, runid=key, questionset=qs) run.save() if not use_session: kwargs = {'runcode': key} else: kwargs = {} request.session['runcode'] = key questionnaire_start.send(sender=None, runinfo=run, questionnaire=qu) return HttpResponseRedirect(reverse('questionnaire', kwargs=kwargs))
def generate_campaign_run(questionnaire_id, email=None, campaign_id=None): qu = get_object_or_404(Questionnaire, id=questionnaire_id) qs = qu.questionsets()[0] str_to_hash = "".join(map(lambda i: chr(random.randint(0, 255)), range(16))) str_to_hash += settings.SECRET_KEY key = md5(str_to_hash).hexdigest() if email is not None: su = Subject.objects.filter(email__iexact=email) if su.exists(): su = su[0] else: su = Subject.objects.create(email=email) else: su = Subject(givenname=key, surname='Anonymous User') su.save() run = RunInfo(subject=su, random=key, runid=key, questionset=qs, campaign_id=campaign_id) run.save() questionnaire_start.send(sender=None, runinfo=run, questionnaire=qu) return str(current_site) + "/q/%s/" % key