Skip to content
Snippets Groups Projects
Unverified Commit 4efd355b authored by Simon Chen's avatar Simon Chen Committed by GitHub
Browse files

Merge pull request #18091 from edx/schen/EDUCATOR-2698

Implement retire users function in the SurveyAnswer model for GDPR
parents 75f94f97 174c0175
No related merge requests found
......@@ -239,8 +239,6 @@ class SurveyAnswer(TimeStampedModel):
supplied to this method is presumed to be previously validated
"""
for name in answers.keys():
value = answers[name]
# See if there is an answer stored for this user, form, field_name pair or not
# this will allow for update cases. This does include an additional lookup,
# but write operations will be relatively infrequent
......@@ -261,3 +259,19 @@ class SurveyAnswer(TimeStampedModel):
answer.field_value = value
answer.course_key = course_key
answer.save()
@classmethod
def retire_user(cls, user_id):
"""
With the parameter user_id, blank out the survey answer values for all survey questions
This is to fulfill our GDPR obligations
Return True if there are data to be blanked
Return False if there are no matching data
"""
answers = cls.objects.filter(user=user_id)
if not answers:
return False
answers.update(field_value='')
return True
......@@ -280,3 +280,28 @@ class SurveyModelsTests(TestCase):
names = survey.get_field_names()
self.assertEqual(sorted(names), ['ddl', 'field1', 'field2'])
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 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment