Пример #1
0
    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))

        form = ResponseForm(survey=survey, user=request.user, step=step)
        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,
        }

        return render(request, template_name, context)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)