def test_form_not_found_raise_exception(self): """ Asserts that when looking up a form that does not exist """ with self.assertRaises(SurveyFormNotFound): SurveyForm.get(self.test_survey_name)
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 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, {}) 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": microsite.get_value("email_from_address", settings.CONTACT_EMAIL), "course": course, } return render_to_response("survey/survey.html", context)
def test_form_not_found_none(self): """ Asserts that when looking up a form that does not exist """ self.assertIsNone( SurveyForm.get(self.test_survey_name, throw_if_not_found=False))
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 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()
def is_survey_required_for_course(course_descriptor): """ Returns whether a Survey is required for this course """ # check to see that the Survey name has been defined in the CourseDescriptor # and that the specified Survey exists return course_descriptor.course_survey_required and \ SurveyForm.get(course_descriptor.course_survey_name, throw_if_not_found=False)
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 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)
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 False # Check that the specified Survey for the course exists. return SurveyForm.get(course_descriptor.course_survey_name, throw_if_not_found=False)
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.assertEquals(survey.form, self.test_form_update)
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")
def must_answer_survey(course_descriptor, user): """ Returns whether a user needs to answer a required survey """ if not is_survey_required_for_course(course_descriptor): return False # this will throw exception if not found, but a non existing survey name will # be trapped in the above is_survey_required_for_course() method survey = SurveyForm.get(course_descriptor.course_survey_name) has_staff_access = has_access(user, "staff", course_descriptor) # survey is required and it exists, let's see if user has answered the survey # course staff do not need to answer survey answered_survey = SurveyAnswer.do_survey_answers_exist(survey, user) return not answered_survey and not has_staff_access
def must_answer_survey(course_descriptor, user): """ Returns whether a user needs to answer a required survey """ if not is_survey_required_for_course(course_descriptor): return False # this will throw exception if not found, but a non existing survey name will # be trapped in the above is_survey_required_for_course() method survey = SurveyForm.get(course_descriptor.course_survey_name) has_staff_access = has_access(user, 'staff', course_descriptor) # survey is required and it exists, let's see if user has answered the survey # course staff do not need to answer survey answered_survey = SurveyAnswer.do_survey_answers_exist(survey, user) return not answered_survey and not has_staff_access
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")
def is_survey_required_and_unanswered(user, course_descriptor): """ Returns whether a user is required to answer the survey and has yet to do so. """ if not is_survey_required_for_course(course_descriptor): return False # anonymous users do not need to answer the survey if user.is_anonymous: return False # course staff do not need to answer survey has_staff_access = has_access(user, 'staff', course_descriptor) if has_staff_access: return False # 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 not answered_survey: return True
def is_survey_required_and_unanswered(user, course_descriptor): """ Returns whether a user is required to answer the survey and has yet to do so. """ if not is_survey_required_for_course(course_descriptor): return False # anonymous users do not need to answer the survey if user.is_anonymous(): return False # course staff do not need to answer survey has_staff_access = has_access(user, 'staff', course_descriptor) if has_staff_access: return False # 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 not answered_survey: return True
def test_form_not_found_none(self): """ Asserts that when looking up a form that does not exist """ self.assertIsNone(SurveyForm.get(self.test_survey_name, throw_if_not_found=False))