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 post(self, request): """ POST /api/user/v1/accounts/retire_misc/ { 'username': '******' } Retires the user with the given username in the LMS. """ username = request.data['username'] if is_username_retired(username): return Response(status=status.HTTP_404_NOT_FOUND) try: retirement = UserRetirementStatus.get_retirement_for_retirement_action( username) RevisionPluginRevision.retire_user(retirement.user) ArticleRevision.retire_user(retirement.user) PendingNameChange.delete_by_user_value(retirement.user, field='user') PasswordHistory.retire_user(retirement.user.id) course_enrollments = CourseEnrollment.objects.filter( user=retirement.user) ManualEnrollmentAudit.retire_manual_enrollments( course_enrollments, retirement.retired_email) CreditRequest.retire_user(retirement.original_username, retirement.retired_username) ApiAccessRequest.retire_user(retirement.user) CreditRequirementStatus.retire_user(retirement.user.username) SurveyAnswer.retire_user(retirement.user.id) except UserRetirementStatus.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) except RetirementStateError as exc: return Response(text_type(exc), status=status.HTTP_400_BAD_REQUEST) except Exception as exc: # pylint: disable=broad-except return Response(text_type(exc), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_204_NO_CONTENT)
def test_retire_user_not_exist(self): survey = self._create_test_survey() self.assertIsNotNone(survey) survey.save_user_answers(self.student, self.student_answers, self.course_id) retire_result = SurveyAnswer.retire_user(self.student2.id) self.assertFalse(retire_result) answers = survey.get_answers(self.student) self.assertEquals(answers[self.student.id], self.student_answers)
def test_retire_user_successful(self): survey = self._create_test_survey() self.assertIsNotNone(survey) survey.save_user_answers(self.student, self.student_answers, self.course_id) survey.save_user_answers(self.student2, self.student2_answers, self.course_id) retire_result = SurveyAnswer.retire_user(self.student.id) self.assertTrue(retire_result) answers = survey.get_answers(self.student) blanked_out_student_answser = {key: '' for key in self.student_answers} self.assertEquals(answers[self.student.id], blanked_out_student_answser) self.assertEquals(survey.get_answers(self.student2)[self.student2.id], self.student2_answers)
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 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 _listen_for_lms_retire(sender, **kwargs): # pylint: disable=unused-argument """ Listener for the USER_RETIRE_LMS_MISC signal, just does the SurveyAnswer retirement """ user = kwargs.get('user') SurveyAnswer.retire_user(user.id)
def post(self, request, *args, **kwargs): serializer = SurveyAnswerSerializer(data=request.data, many=True) if request.data is None or len(request.data) == 0: return HttpResponse("No request body", status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): try: survey_id = None for data in list(serializer.data): survey_id = data['survey_instance_id'] survey_instance = SurveyInstance.objects.filter( id=survey_id).first() if survey_instance is not None and survey_instance.submitted_time is not None: return HttpResponse( "Survey already submitted for survey id " + str(survey_id), status=status.HTTP_400_BAD_REQUEST) else: A1 = 0 A2 = 0 A3 = 0 B = 0 C = 0 for data in list(serializer.data): question_id = data['question_id'] answer = data['answer'] survey_answer = SurveyAnswer( survey_instance_id_id=survey_id, question_id_id=str(question_id), answer=answer) if int(question_id) == 1: A1 = int(answer) if int(question_id) == 2: A2 = int(answer) if int(question_id) == 10: A3 = int(answer) if int(question_id) == 8: C = int(answer) if int(question_id) == 9: B = int(answer) survey_answer.save() if survey_id is not None: survey_instance = SurveyInstance.objects.filter( id=survey_id).first() survey_instance.submitted_time = datetime.datetime.now( ) health_worker = survey_instance.health_worker_id if health_worker is not None \ and health_worker.result_status_id is not None \ and health_worker.result_status_id.id \ in [3, 4, 5, 6]: # todo change to enum survey_instance.result_id = health_worker.result_status_id else: """ triaging logic """ result_id = 1 age = datetime.datetime.now( ).year - health_worker.date_of_birth.year if ((A1 + A2 + A3 == 1) or (A1 + A2 + A3 == 2)) \ and (B + C == 0): if age >= 60: result_id = 7 # quarantine - 72 hours contact else: result_id = 8 # precaution - you have symptoms else: if (A1 + A2 + A3 == 3) or B == 1 or C == 1: result_id = 9 # quarantine - 24 hours contact else: if health_worker.immunodeficiencies.count( ) > 0: if age >= 60: result_id = 11 # stay at home else: result_id = 10 survey_instance.result_id = ResultStatus.objects.filter( id=result_id).first() survey_instance.save() health_worker.result_status_id = survey_instance.result_id health_worker.save() result_id = health_worker.result_status_id.id if result_id != 1: if result_id >= 7: message = "" if result_id == 7: message = "Please follow home quarantine protocol and make a call or update the survey after 72 hours to check for symptoms" if result_id == 8: message = "Please take precautionary measures like handwashing and social distancing. Call you back or update survey immediately if all 3 symptoms show up" if result_id == 9: message = "After 24 hours call or update the survey, to check for symptoms" if result_id == 10: message = "Take precautionary measures like handwashing and social distancing." if result_id == 11: message = "The advice will be to stay at home, and the district office will send a doctor to their home to do a checkup." commHandler = CommunicationHandler() commHandler.send_message( health_worker, 1, {'health_status': message}) else: message = "Your status is still " + health_worker.result_status_id.result_status commHandler = CommunicationHandler() commHandler.send_message(health_worker, 4, {}) except Exception as e: return HttpResponse(e, status=status.HTTP_400_BAD_REQUEST) else: return HttpResponse("Invalid data", status=status.HTTP_400_BAD_REQUEST) return HttpResponse(survey_instance.result_id, status=status.HTTP_201_CREATED)