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 _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)