def post(self, request, *args, **kwargs): survey = get_object_or_404(Survey, is_published=True, id=kwargs['id']) if survey.need_logged_user and not request.user.is_authenticated: return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by('order') video = get_object_or_404(Video, id=request.POST.get("videoID")) form = ResponseForm(request.POST, survey=survey, video=video, user=request.user, step=kwargs.get('step', 0)) context = { 'response_form': form, 'survey': survey, 'categories': categories, 'video': video } if form.is_valid(): session_key = 'survey_%s' % (kwargs['id'], ) if session_key not in request.session: request.session[session_key] = {} for key, value in list(form.cleaned_data.items()): request.session[session_key][key] = value request.session.modified = True next_url = form.next_step_url() response = None if survey.display_by_question: if not form.has_next_step(): save_form = ResponseForm(request.session[session_key], video=video, survey=survey, user=request.user) response = save_form.save() else: response = form.save() if next_url is not None: return redirect(next_url) else: del request.session[session_key] if response is None: return redirect('/') else: next_ = request.session.get('next', None) if next_ is not None: if 'next' in request.session: del request.session['next'] return redirect(next_) else: return redirect('survey-confirmation', uuid=response.interview_uuid) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = 'survey/survey.html' else: template_name = 'survey/one_page_survey.html' return render(request, template_name, context)
def post(self, request, *args, **kwargs): # @todo dedup, this is just copy/pasted from SurveyDetail question = get_object_or_404(Question, id=kwargs["id"]) survey = question.survey if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by("order") form = ResponseForm(request.POST, survey=survey, user=request.user, step=kwargs.get("step", 0)) context = { "response_form": form, "survey": survey, "categories": categories } if form.is_valid(): session_key = "survey_%s" % (kwargs["id"], ) if session_key not in request.session: request.session[session_key] = {} for key, value in list(form.cleaned_data.items()): request.session[session_key][key] = value request.session.modified = True # pass the question id so other questions are not updated form.poll_question_id = question.id next_url = form.next_step_url() response = None if survey.display_by_question: if not form.has_next_step(): save_form = ResponseForm(request.session[session_key], survey=survey, user=request.user) response = save_form.save() else: response = form.save() messages.success( self.request, _('Your answer has been saved, thank you for the feedback!')) if next_url is not None: return redirect(next_url) else: del request.session[session_key] if response is None: return redirect("/") else: next_ = request.session.get("next", None) if next_ is not None: if "next" in request.session: del request.session["next"] return redirect(next_) else: return redirect("dashboard") raise Exception('Error')
def post(self, request, *args, **kwargs): survey = get_object_or_404(Survey, is_published=True, id=kwargs["id"]) if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by("order") form = ResponseForm(request.POST, survey=survey, user=request.user, step=kwargs.get("step", 0)) context = { "response_form": form, "survey": survey, "categories": categories } if form.is_valid(): session_key = "survey_%s" % (kwargs["id"], ) if session_key not in request.session: request.session[session_key] = {} for key, value in list(form.cleaned_data.items()): request.session[session_key][key] = value request.session.modified = True next_url = form.next_step_url() response = None if survey.display_by_question: if not form.has_next_step(): save_form = ResponseForm(request.session[session_key], survey=survey, user=request.user) response = save_form.save() else: response = form.save() if next_url is not None: return redirect(next_url) else: del request.session[session_key] if response is None: return redirect("/") else: next_ = request.session.get("next", None) if next_ is not None: if "next" in request.session: del request.session["next"] return redirect(next_) else: return redirect("survey-confirmation", uuid=response.interview_uuid) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = "survey/survey.html" else: template_name = "survey/one_page_survey.html" return render(request, template_name, context)
def get(self, request, *args, **kwargs): survey = get_object_or_404( Survey.objects.prefetch_related("questions", "questions__category"), is_published=True, id=kwargs["id"], ) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = "survey/survey.html" else: template_name = "survey/one_page_survey.html" if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by("order") form = ResponseForm(survey=survey, user=request.user, step=kwargs.get("step", 0)) context = { "response_form": form, "survey": survey, "categories": categories } return render(request, template_name, context)
def post(self, request, *args, **kwargs): survey = kwargs.get("survey") if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) form = ResponseForm(request.POST, survey=survey, user=request.user, step=kwargs.get("step", 0)) categories = form.current_categories() if not survey.editable_answers and form.response is not None: LOGGER.info( "Redirects to survey list after trying to edit non editable answer." ) return redirect(reverse("survey-list")) context = { "response_form": form, "survey": survey, "categories": categories } if form.is_valid(): if form.clean_choices(survey): return self.treat_valid_form(form, kwargs, request, survey) return self.handle_invalid_form(context, form, request, survey)
def get(self, request, *args, **kwargs): survey = get_object_or_404(Survey, is_published=True, id=kwargs['id']) if request.GET.get("videoID") is not None: video = get_object_or_404(Video, id=request.GET.get("videoID")) else: video = Video.objects.random(survey.video_cat.id) responses = Response.objects.filter(video=video) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = 'survey/survey.html' else: template_name = 'survey/one_page_survey.html' if survey.need_logged_user and not request.user.is_authenticated: return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by('order') form = ResponseForm(survey=survey, user=request.user, video=video, step=kwargs.get('step', 0)) context = { 'response_form': form, 'survey': survey, 'categories': categories, 'video': video, 'num_responses': len(responses), # to display the number of votes for this video } return render(request, template_name, context)
def treat_valid_form(self, form, kwargs, request, survey): session_key = "survey_%s" % (kwargs["id"], ) if session_key not in request.session: request.session[session_key] = {} for key, value in list(form.cleaned_data.items()): request.session[session_key][key] = value request.session.modified = True next_url = form.next_step_url() response = None if survey.display_by_question: # when it's the last step if not form.has_next_step(): save_form = ResponseForm(request.session[session_key], survey=survey, user=request.user) if save_form.is_valid(): response = save_form.save() else: LOGGER.warning( "A step of the multipage form failed but should have been discovered before." ) else: response = form.save() # if there is a next step if next_url is not None: return redirect(next_url) del request.session[session_key] if response is None: return redirect(reverse("survey-list")) next_ = request.session.get("next", None) if next_ is not None: if "next" in request.session: del request.session["next"] return redirect(next_) return redirect("survey-confirmation", uuid=response.interview_uuid)
def get(self, request, *args, **kwargs): survey = kwargs.get("survey") step = kwargs.get("step", 0) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = "survey/survey.html" else: template_name = "survey/one_page_survey.html" if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by("order") form = ResponseForm(survey=survey, user=request.user, step=step) asset_context = { # If any of the widgets of the current form has a "date" class, flatpickr will be loaded into the template "flatpickr": any([ field.widget.attrs.get("class") == "date" for _, field in form.fields.items() ]) } context = { "response_form": form, "survey": survey, "categories": categories, "step": step, "asset_context": asset_context, } return render(request, template_name, context)
def post(self, request, *args, **kwargs): survey = get_object_or_404(Survey, is_published=True, id=kwargs["id"]) if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by("order") form = ResponseForm(request.POST, survey=survey, user=request.user, step=kwargs.get("step", 0)) if not survey.editable_answers and form.response is not None: LOGGER.info("Redirects to survey list after trying to edit non editable answer.") return redirect(reverse("survey-list")) context = {"response_form": form, "survey": survey, "categories": categories} if form.is_valid(): return self.treat_valid_form(form, kwargs, request, survey) return self.handle_invalid_form(context, form, request, survey)
def get(self, request, *args, **kwargs): query_params = urllib.parse.parse_qs(request.GET.urlencode()) logger.info(query_params) if "custom_user" in query_params.keys(): custom_user = query_params["custom_user"][0] query_params["custom_user"] = custom_user else: custom_user = "" survey = kwargs.get("survey") step = kwargs.get("step", 0) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.is_all_in_one_page(): template_name = "survey/one_page_survey.html" else: template_name = "survey/survey.html" if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) form = ResponseForm(survey=survey, user=request.user, step=step, custom_user=custom_user) logger.warning(kwargs) categories = form.current_categories() asset_context = { # If any of the widgets of the current form has a "date" class, flatpickr will be loaded into the template "flatpickr": any([field.widget.attrs.get("class") == "date" for _, field in form.fields.items()]) } context = { "response_form": form, "survey": survey, "categories": categories, "step": step, "asset_context": asset_context, "query_params": urllib.parse.urlencode(query_params), "embed": kwargs.get("embed", False), } return render(request, template_name, context)
def treat_valid_form(self, form, kwargs, request, survey): session_key = "survey_%s" % (kwargs["slug"],) if session_key not in request.session: request.session[session_key] = {} for key, value in list(form.cleaned_data.items()): request.session[session_key][key] = value request.session.modified = True next_url = form.next_step_url() response = None query_params = urllib.parse.parse_qs(request.GET.urlencode()) # logger.info(query_params) if "custom_user" in query_params.keys(): custom_user = query_params["custom_user"][0] query_params["custom_user"] = custom_user else: custom_user = "" if survey.is_all_in_one_page(): response = form.save() else: # when it's the last step if not form.has_next_step(): save_form = ResponseForm( request.session[session_key], survey=survey, user=request.user, custom_user=custom_user ) if save_form.is_valid(): response = save_form.save() else: LOGGER.warning("A step of the multipage form failed but should have been discovered before.") # if there is a next step if next_url is not None: return redirect(next_url) del request.session[session_key] if response is None: return redirect(reverse("survey-list")) next_ = request.session.get("next", None) if next_ is not None: if "next" in request.session: del request.session["next"] return redirect(next_) return redirect("survey:survey-confirmation", uuid=response.interview_uuid)
def survey_widget(user, survey_id): """ Displays a random question from the specified survey which the logged in user has not been answered yet. """ survey = Survey.objects.get(pk=survey_id) question = survey.get_unanswered_question(user) if question: response_form = ResponseForm(survey=survey, user=user, step=0) question_field = response_form['question_' + str(question.id)] else: response_form = question_field = None return { 'response_form': response_form, 'question': question, 'question_field': question_field }
def get(self, request, *args, **kwargs): survey = get_object_or_404(Survey, status='Active', id=kwargs['id']) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = 'survey/survey.html' else: template_name = 'survey/one_page_survey.html' if survey.need_logged_user and not request.user.is_authenticated(): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by('order') form = ResponseForm(survey=survey, user=request.user, step=kwargs.get('step', 0)) context = { 'response_form': form, 'survey': survey, 'categories': categories, } return render(request, template_name, context)
def get(self, request, *args, **kwargs): # import pdb; pdb.set_trace() survey = get_object_or_404(Survey, is_published=True, id=kwargs['id']) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = 'survey/survey.html' else: template_name = 'survey/one_page_survey.html' if survey.need_logged_user and not request.user.is_authenticated(): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by('order') form = ResponseForm(survey=survey, user=request.user, step=kwargs.get('step', 0)) numb_of_questions = int( Survey.objects.get(id=kwargs['id']).questions.all().count()) progress_score = ( (int(kwargs.get('step', 0)) + 1) / numb_of_questions) * 100 try: get_scale = form.get_multiple_scale() widget_type = form.get_widget_type() except: get_scale = None widget_type = None context = { 'response_form': form, 'survey': survey, 'categories': categories, 'scales': get_scale, 'type': widget_type, 'progress_score': progress_score, } return render(request, template_name, context)
def post(self, request, *args, **kwargs): survey = kwargs.get("survey") if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) query_params = urllib.parse.parse_qs(request.GET.urlencode()) if "custom_user" in query_params.keys(): custom_user = query_params["custom_user"][0] query_params["custom_user"] = custom_user else: custom_user = "" form = ResponseForm( request.POST, survey=survey, user=request.user, step=kwargs.get("step", 0), custom_user=custom_user ) categories = form.current_categories() if not survey.editable_answers and form.response is not None: LOGGER.info("Redirects to survey list after trying to edit non editable answer.") return redirect(reverse("survey-list")) context = {"response_form": form, "survey": survey, "categories": categories} if form.is_valid(): return self.treat_valid_form(form, kwargs, request, survey) return self.handle_invalid_form(context, form, request, survey)
def get(self, request, *args, **kwargs): survey = kwargs.get("survey") step = kwargs.get("step", 0) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.is_all_in_one_page(): template_name = "survey/one_page_survey.html" else: template_name = "survey/survey.html" if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) # create a random number to use for ordering for this session only if step == 0 or "random" not in request.session: request.session["random"] = random.randrange(0, 100000) form = ResponseForm(survey=survey, user=request.user, step=step, seed=request.session["random"]) categories = form.current_categories() # is the use a farmer? farmer = "" try: q = Question.objects.get(code=Question.USER_FARMER, survey=survey) session_key = "survey_%s" % (kwargs["id"], ) if session_key in request.session: question_id = "question_" + str(q.id) if question_id in request.session[session_key]: farmer = request.session[session_key][question_id] except Question.DoesNotExist: farmer = "no user is farmer question" # get insects by location # insects = [] # location = "" # try: # q=Question.objects.get(code=Question.USER_LOCATION,survey=survey) # session_key = "survey_%s" % (kwargs["id"],) # if session_key in request.session: # question_id = "question_"+str(q.id) # if question_id in request.session[session_key]: # location = request.session[session_key][question_id] # # need to get randomize from original question ideally # insects = Insect.objects.filter(location__icontains=location).order_by('?') # except Question.DoesNotExist: # location="no location question" location = "no location question" insects = Insect.objects.all().order_by('?') # get list of insects from current insect select question #print(form.fields) # load known insects if they have been recorded yet known_insects = [] try: q = Question.objects.get(code=Question.WASPS_KNOWN, survey=survey) session_key = "survey_%s" % (kwargs["id"], ) if session_key in request.session: question_id = "question_" + str(q.id) if question_id in request.session[session_key]: i = request.session[session_key][question_id] # need to get randomize from original question ideally known_list = i.split(",") if len(known_list) > 0 and i != "none": known_insects = Insect.objects.filter( name__in=known_list).order_by('?') #else: # if none have been picked through them all in #known_insects = Insect.objects.filter(location__icontains=location).order_by('?') #known_insects = Insect.objects.filter().order_by('?') except Question.DoesNotExist: pass # skip name insects, if we don't know any insects for qname, field in form.fields.items(): if field.widget.attrs["type"] == "name-insect" and len( known_insects) == 0: return redirect("/survey/%s-%s" % (survey.id, str(int(step) + 1))) wasp_id = 0 # check the current categories for a wasp ID for cat in categories: if cat.wasp_index > 0: wasp_id = cat.wasp_index - 1 wasp = 0 wasp_id_field = "none" for qname, field in form.fields.items(): if field.widget.attrs["code"] == "wasp-id": try: wasp_id_field = "id_" + qname wasp = insects[wasp_id] except IndexError: # maybe could check here and skip/incr step?? print(field.label + " out of bounds for current insects") except ValueError: print(field.label + " does not look like an int") asset_context = { # If any of the widgets of the current form has a "date" class, flatpickr will be loaded into the template "flatpickr": any([ field.widget.attrs.get("class") == "date" for _, field in form.fields.items() ]) } context = { "response_form": form, "survey": survey, "categories": categories, "step": step, "steps": form.num_steps(), "asset_context": asset_context, "farmer": farmer, "location": location, "insects": insects, "known_insects": known_insects, "wasp": wasp, "wasp_id_field": wasp_id_field } return render(request, template_name, context)
def post(self, request, *args, **kwargs): survey = get_object_or_404(Survey, is_published=True, id=kwargs["id"]) if survey.need_logged_user and not request.user.is_authenticated: return redirect("%s?next=%s" % (settings.LOGIN_URL, request.path)) categories = Category.objects.filter(survey=survey).order_by("order") form = ResponseForm( request.POST, survey=survey, user=request.user, step=kwargs.get("step", 0) ) if not survey.editable_answers and form.response is not None: LOGGER.info( "Redirects to survey list after trying to edit non editable answer." ) return redirect(reverse("survey-list")) context = {"response_form": form, "survey": survey, "categories": categories} if form.is_valid(): session_key = "survey_%s" % (kwargs["id"],) if session_key not in request.session: request.session[session_key] = {} for key, value in list(form.cleaned_data.items()): request.session[session_key][key] = value request.session.modified = True next_url = form.next_step_url() response = None if survey.display_by_question: # when it's the last step if not form.has_next_step(): save_form = ResponseForm( request.session[session_key], survey=survey, user=request.user ) if save_form.is_valid(): response = save_form.save() else: LOGGER.warning( "A step of the multipage form failed \ but should have been discovered before." ) else: response = form.save() # if there is a next step if next_url is not None: return redirect(next_url) else: del request.session[session_key] if response is None: return redirect(reverse("survey-list")) else: next_ = request.session.get("next", None) if next_ is not None: if "next" in request.session: del request.session["next"] return redirect(next_) else: return redirect( "survey-confirmation", uuid=response.interview_uuid ) else: LOGGER.info("Non valid form: <%s>", form) if survey.template is not None and len(survey.template) > 4: template_name = survey.template else: if survey.display_by_question: template_name = "survey/survey.html" else: template_name = "survey/one_page_survey.html" return render(request, template_name, context)