def test_form_not_found_raise_exception(self):
        """
        Asserts that when looking up a form that does not exist
        """

        with pytest.raises(SurveyFormNotFound):
            SurveyForm.get(self.test_survey_name)
    def test_form_not_found_none(self):
        """
        Asserts that when looking up a form that does not exist
        """

        assert SurveyForm.get(self.test_survey_name,
                              throw_if_not_found=False) is None
Exemplo n.º 3
0
def check_survey_required_and_unanswered(user, course_descriptor):
    """
    Checks whether a user is required to answer the survey and has yet to do so.

    Returns:
        AccessResponse: Either ACCESS_GRANTED or SurveyRequiredAccessError.
    """

    if not is_survey_required_for_course(course_descriptor):
        return ACCESS_GRANTED

    # anonymous users do not need to answer the survey
    if user.is_anonymous:
        return ACCESS_GRANTED

    # course staff do not need to answer survey
    has_staff_access = has_access(user, 'staff', course_descriptor)
    if has_staff_access:
        return ACCESS_GRANTED

    # survey is required and it exists, let's see if user has answered the survey
    survey = SurveyForm.get(course_descriptor.course_survey_name)
    answered_survey = SurveyAnswer.do_survey_answers_exist(survey, user)
    if answered_survey:
        return ACCESS_GRANTED

    return SurveyRequiredAccessError()
Exemplo n.º 4
0
def view_student_survey(user, survey_name, course=None, redirect_url=None, is_required=False, skip_redirect_url=None):
    """
    Shared utility method to render a survey form
    NOTE: This method is shared between the Survey and Courseware Djangoapps
    """

    redirect_url = redirect_url if redirect_url else reverse('dashboard')
    dashboard_redirect_url = reverse('dashboard')
    skip_redirect_url = skip_redirect_url if skip_redirect_url else dashboard_redirect_url

    survey = SurveyForm.get(survey_name, throw_if_not_found=False)
    if not survey:
        return HttpResponseRedirect(redirect_url)

    # the result set from get_answers, has an outer key with the user_id
    # just remove that outer key to make the JSON payload simplier
    existing_answers = survey.get_answers(user=user).get(user.id, {})

    platform_name = configuration_helpers.get_value('platform_name', settings.PLATFORM_NAME)

    context = {
        'existing_data_json': json.dumps(existing_answers),
        'postback_url': reverse('submit_answers', args=[survey_name]),
        'redirect_url': redirect_url,
        'skip_redirect_url': skip_redirect_url,
        'dashboard_redirect_url': dashboard_redirect_url,
        'survey_form': survey.form,
        'is_required': is_required,
        'mail_to_link': configuration_helpers.get_value('email_from_address', settings.CONTACT_EMAIL),
        'platform_name': platform_name,
        'course': course,
    }

    return render_to_response("survey/survey.html", context)
    def test_create_new_form(self):
        """
        Make sure we can create a new form a look it up
        """

        survey = self._create_test_survey()
        assert survey is not None

        new_survey = SurveyForm.get(self.test_survey_name)
        assert new_survey is not None
        assert new_survey.form == self.test_form
Exemplo n.º 6
0
def is_survey_required_for_course(course_descriptor):
    """
    Returns whether a Survey is required for this course
    """

    # Check to see that the survey is required in the CourseDescriptor.
    if not getattr(course_descriptor, 'course_survey_required', False):
        return SurveyRequiredAccessError()

    # Check that the specified Survey for the course exists.
    return SurveyForm.get(course_descriptor.course_survey_name, throw_if_not_found=False)
Exemplo n.º 7
0
    def test_create_new_form(self):
        """
        Make sure we can create a new form a look it up
        """

        survey = self._create_test_survey()
        self.assertIsNotNone(survey)

        new_survey = SurveyForm.get(self.test_survey_name)
        self.assertIsNotNone(new_survey)
        self.assertEqual(new_survey.form, self.test_form)
    def test_create_form_update_existing(self):
        """
        Make sure we can update an existing form
        """
        survey = self._create_test_survey()
        assert survey is not None

        survey = SurveyForm.create(self.test_survey_name,
                                   self.test_form_update,
                                   update_if_exists=True)
        assert survey is not None

        survey = SurveyForm.get(self.test_survey_name)
        assert survey is not None
        assert survey.form == self.test_form_update
Exemplo n.º 9
0
    def test_create_form_update_existing(self):
        """
        Make sure we can update an existing form
        """
        survey = self._create_test_survey()
        self.assertIsNotNone(survey)

        survey = SurveyForm.create(self.test_survey_name,
                                   self.test_form_update,
                                   update_if_exists=True)
        self.assertIsNotNone(survey)

        survey = SurveyForm.get(self.test_survey_name)
        self.assertIsNotNone(survey)
        self.assertEqual(survey.form, self.test_form_update)
Exemplo n.º 10
0
def submit_answers(request, survey_name):
    """
    Form submission post-back endpoint.

    NOTE: We do not have a formal definition of a Survey Form, it's just some authored HTML
    form fields (via Django Admin site). Therefore we do not do any validation of the submission server side. It is
    assumed that all validation is done via JavaScript in the survey.html file
    """
    survey = SurveyForm.get(survey_name, throw_if_not_found=False)

    if not survey:
        return HttpResponseNotFound()

    answers = {}
    for key in request.POST.keys():
        # support multi-SELECT form values, by string concatenating them with a comma separator
        array_val = request.POST.getlist(key)
        answers[key] = request.POST[key] if len(array_val) == 0 else ','.join(
            array_val)

    # the URL we are supposed to redirect to is
    # in a hidden form field
    redirect_url = answers[
        '_redirect_url'] if '_redirect_url' in answers else reverse(
            'dashboard')

    course_key = CourseKey.from_string(
        answers['course_id']) if 'course_id' in answers else None

    allowed_field_names = survey.get_field_names()

    # scrub the answers to make sure nothing malicious from the user gets stored in
    # our database, e.g. JavaScript
    filtered_answers = {}
    for answer_key in answers.keys():
        # only allow known input fields
        if answer_key in allowed_field_names:
            filtered_answers[answer_key] = escape(answers[answer_key])

    survey.save_user_answers(request.user, filtered_answers, course_key)

    response_params = json.dumps({
        # The HTTP end-point for the payment processor.
        "redirect_url": redirect_url,
    })

    return HttpResponse(response_params, content_type="text/json")