def form_valid(self, form): """Create user answer records using form data.""" stotal = self.get_list_queryset().count() quest = self.get_detail_object() ip_address = get_ip(self.request) user_agent = unicode(self.request.META.get('HTTP_USER_AGENT', '')[:255], errors='ignore') if hasattr(self.request, 'session') and self.request.session.session_key: session_key = self.request.session.session_key else: session_key = '%s:%s' % (ip_address, user_agent) if isinstance(self.user, AnonymousUser): self.user = User.objects.get(username='******') uquest = UserQuestionnaire.obj.get_or_create( questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] else: uquest = UserQuestionnaire.obj.get_or_create( questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] section = self.get_section() if not "Stages" in section.name: for order, value in form.cleaned_data.items(): question = section.questions.get(order=int(order)) answer = Answer.obj.get_or_create(user_questionnaire=uquest, question=question)[0] answer.update(answer=value) else: for order, value in form.cleaned_data.items(): if 'help' == order.split('-')[-1]: type = 1 else: type = 2 question = section.questions.get( order=int(order.split('-')[0])) answer = Answer.obj.create(user_questionnaire=uquest, question=question) answer.update(answer=value, type=type) # Redirect to the next section or to 'done' page: if self.snum >= stotal: return redir("done") else: return redir(quest.get_absolute_url(self.snum + 1))
def form_valid(self, form): """Create user answer records using form data.""" stotal = self.get_list_queryset().count() quest = self.get_detail_object() ip_address = get_ip(self.request) user_agent = unicode(self.request.META.get('HTTP_USER_AGENT', '')[:255], errors='ignore') if hasattr(self.request, 'session') and self.request.session.session_key: session_key = self.request.session.session_key else: session_key = '%s:%s' % (ip_address, user_agent) if isinstance(self.user, AnonymousUser): self.user = User.objects.get(username='******') uquest = UserQuestionnaire.obj.get_or_create(questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] else: uquest = UserQuestionnaire.obj.get_or_create(questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] section = self.get_section() if not "Stages" in section.name: for order, value in form.cleaned_data.items(): question = section.questions.get(order=int(order)) answer = Answer.obj.get_or_create(user_questionnaire=uquest, question=question)[0] answer.update(answer=value) else: for order, value in form.cleaned_data.items(): if 'help' == order.split('-')[-1]: type = 1 else: type = 2 question = section.questions.get(order=int(order.split('-')[0])) answer = Answer.obj.create(user_questionnaire=uquest, question=question) answer.update(answer=value, type=type) # Redirect to the next section or to 'done' page: if self.snum >= stotal: return redir("done") else: return redir(quest.get_absolute_url(self.snum+1))
def form_valid(self, form): """Create user answer records using form data.""" stotal = self.get_list_queryset().count() quest = self.get_detail_object() ip_address = get_ip(self.request) user_agent = unicode(self.request.META.get('HTTP_USER_AGENT', '')[:255], errors='ignore') if hasattr(self.request, 'session') and self.request.session.session_key: session_key = self.request.session.session_key else: session_key = '%s:%s' % (ip_address, user_agent) if isinstance(self.user, AnonymousUser): self.user = User.objects.get(username='******') uquest = UserQuestionnaire.obj.get_or_create(questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] else: uquest = UserQuestionnaire.obj.get_or_create(questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] for section in form.questionnaire.sections.all(): #section = self.get_section() section_name = section.name.lower() #print form.cleaned_data.items() if not "Stages" in section.name: for order, value in form.cleaned_data.items(): if section_name in order.lower(): question = section.questions.get(order=int(order.split('::')[1])) answer = Answer.obj.get_or_create(user_questionnaire=uquest, question=question)[0] answer.update(answer=value) else: for order, value in form.cleaned_data.items(): if section_name in order.lower(): if 'help' == order.split('::')[1].split('-')[1]: type = 1 else: type = 2 question = section.questions.get(order=int(order.split('::')[1].split('-')[0])) answer = Answer.obj.create(user_questionnaire=uquest, question=question) #[0] answer.update(answer=value, type=type) thanks = form.questionnaire.thanks.all() if thanks: pk = thanks[0].pk else: pk = 1 return redir("thanks", pk)
def form_valid(self, form): """Create user answer records using form data.""" stotal = self.get_list_queryset().count() quest = self.get_detail_object() ip_address = get_ip(self.request) user_agent = unicode(self.request.META.get('HTTP_USER_AGENT', '')[:255], errors='ignore') if hasattr(self.request, 'session') and self.request.session.session_key: session_key = self.request.session.session_key else: session_key = '%s:%s' % (ip_address, user_agent) if isinstance(self.user, AnonymousUser): self.user = User.objects.get(username='******') uquest = UserQuestionnaire.obj.get_or_create( questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] else: uquest = UserQuestionnaire.obj.get_or_create( questionnaire=quest, user=self.user, session_key=session_key, ip_address=ip_address, user_agent=user_agent)[0] for section in form.questionnaire.sections.all(): #section = self.get_section() section_name = section.name.lower() #print form.cleaned_data.items() if not "Stages" in section.name: for order, value in form.cleaned_data.items(): if section_name in order.lower(): question = section.questions.get( order=int(order.split('::')[1])) answer = Answer.obj.get_or_create( user_questionnaire=uquest, question=question)[0] answer.update(answer=value) else: for order, value in form.cleaned_data.items(): if section_name in order.lower(): if 'help' == order.split('::')[1].split('-')[1]: type = 1 else: type = 2 question = section.questions.get( order=int(order.split('::')[1].split('-')[0])) answer = Answer.obj.create(user_questionnaire=uquest, question=question) #[0] answer.update(answer=value, type=type) thanks = form.questionnaire.thanks.all() if thanks: pk = thanks[0].pk else: pk = 1 return redir("thanks", pk)