-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
121 lines (95 loc) · 4.21 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Database models for edx_name_affirmation.
"""
from config_models.models import ConfigurationModel
from model_utils.models import TimeStampedModel
from simple_history.models import HistoricalRecords
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from edx_name_affirmation.statuses import VerifiedNameStatus
try:
from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification
from lms.djangoapps.verify_student.models import VerificationAttempt as PlatformVerificationAttempt
except ImportError:
SoftwareSecurePhotoVerification = None
PlatformVerificationAttempt = None
User = get_user_model()
class VerifiedName(TimeStampedModel):
"""
This model represents a verified name for a user, with a link to the source
through `verification_attempt_id` or `proctored_exam_attempt_id` if applicable.
.. pii: Contains name fields.
.. pii_types: name
.. pii_retirement: local_api
"""
user = models.ForeignKey(User, db_index=True, on_delete=models.CASCADE)
verified_name = models.CharField(max_length=255, db_index=True)
# Snapshot of the user's UserProfile `name` upon creation
profile_name = models.CharField(max_length=255, null=True)
# Reference to an external ID verification or proctored exam attempt
verification_attempt_id = models.PositiveIntegerField(null=True, blank=True)
proctored_exam_attempt_id = models.PositiveIntegerField(null=True, blank=True)
# Reference to a generic VerificationAttempt object in the platform
platform_verification_attempt_id = models.PositiveIntegerField(null=True, blank=True)
status = models.CharField(
max_length=32,
choices=[(st.value, st.value) for st in VerifiedNameStatus],
default=VerifiedNameStatus.PENDING.value,
)
history = HistoricalRecords()
@classmethod
def retire_user(cls, user_id):
"""
Retire user as part of GDPR pipeline
:param user_id: int
"""
verified_names = cls.objects.filter(user_id=user_id)
verified_names.delete()
class Meta:
""" Meta class for this Django model """
db_table = 'nameaffirmation_verifiedname'
verbose_name = 'verified name'
@property
def verification_attempt_status(self):
"Returns the status associated with its SoftwareSecurePhotoVerification with verification_attempt_id if any."
if not self.verification_attempt_id or not SoftwareSecurePhotoVerification:
return None
try:
verification = SoftwareSecurePhotoVerification.objects.get(id=self.verification_attempt_id)
return verification.status
except ObjectDoesNotExist:
return None
@property
def platform_verification_attempt_status(self):
"""
Returns the status associated with its platform VerificationAttempt
"""
if not self.platform_verification_attempt_id or not PlatformVerificationAttempt:
return None
try:
verification = PlatformVerificationAttempt.objects.get(id=self.platform_verification_attempt_id)
return verification.status
except ObjectDoesNotExist:
return None
def save(self, *args, **kwargs):
"""
Validate only one of `verification_attempt_id` or `platform_verification_attempt_id`
can be set.
"""
if self.verification_attempt_id and self.platform_verification_attempt_id:
raise ValueError('Only one of `verification_attempt_id` or `platform_verification_attempt_id` can be set.')
super().save(*args, **kwargs)
class VerifiedNameConfig(ConfigurationModel):
"""
This model provides various configuration fields for users regarding their
verified name.
.. no_pii: This model has no PII.
"""
KEY_FIELDS = ('user',)
user = models.ForeignKey(User, db_index=True, on_delete=models.CASCADE, related_name='verified_name_config')
use_verified_name_for_certs = models.BooleanField(default=False)
class Meta:
""" Meta class for this Django model """
db_table = 'nameaffirmation_verifiednameconfig'
verbose_name = 'verified name config'