Commit df4024ce authored by Samuel GAIST's avatar Samuel GAIST

[accounts][models] Implement natural key and foreign key for Profile and AccountSettings

This fixes an issue on restore with unique constraints violation.
parent 9f92f26e
Pipeline #28007 passed with stage
in 15 minutes and 27 seconds
...@@ -33,79 +33,109 @@ from django.dispatch import receiver ...@@ -33,79 +33,109 @@ from django.dispatch import receiver
import random import random
import string import string
class AccountSettings(models.Model):
class AccountSettingsManager(models.Manager):
def get_by_natural_key(self, owner):
return self.get(owner=owner)
class AccountSettings(models.Model):
class Meta: class Meta:
verbose_name_plural = "account settings" verbose_name_plural = "account settings"
owner = models.OneToOneField(User) owner = models.OneToOneField(User)
daily_summary = models.BooleanField(default=True) daily_summary = models.BooleanField(default=True)
experiment_mail_notifications_enabled = models.BooleanField(default=True) experiment_mail_notifications_enabled = models.BooleanField(default=True)
database_notifications_enabled = models.BooleanField(default=True) database_notifications_enabled = models.BooleanField(default=True)
environment_notifications_enabled = models.BooleanField(default=True) environment_notifications_enabled = models.BooleanField(default=True)
objects = AccountSettingsManager()
def natural_key(self):
return (self.owner,)
class SupervisionTrack(models.Model): class SupervisionTrack(models.Model):
#_____ Fields __________ # _____ Fields __________
supervisee = models.ForeignKey(User, on_delete=models.CASCADE, supervisee = models.ForeignKey(
related_name='supervisors') User, on_delete=models.CASCADE, related_name="supervisors"
supervisor = models.ForeignKey(User, on_delete=models.CASCADE, )
related_name='supervisees') supervisor = models.ForeignKey(
is_valid = models.BooleanField(default=False) User, on_delete=models.CASCADE, related_name="supervisees"
start_date = models.DateTimeField(null=True, blank=True) )
expiration_date = models.DateTimeField(null=True, blank=True) is_valid = models.BooleanField(default=False)
start_date = models.DateTimeField(null=True, blank=True)
expiration_date = models.DateTimeField(null=True, blank=True)
last_validation_date = models.DateTimeField(null=True, blank=True) last_validation_date = models.DateTimeField(null=True, blank=True)
supervision_key = models.CharField(max_length=40, null=True, blank=True) supervision_key = models.CharField(max_length=40, null=True, blank=True)
def __unicode__(self): def __unicode__(self):
return u'Supervisor: %s, Supervisee, %s, Validity: %s' % (self.supervisor.username, self.supervisee.username, self.is_valid) return u"Supervisor: %s, Supervisee, %s, Validity: %s" % (
self.supervisor.username,
self.supervisee.username,
self.is_valid,
)
class ProfileManager(models.Manager):
def get_by_natural_key(self, user):
return self.get(user=user)
class Profile(models.Model): class Profile(models.Model):
#_____ Constants __________ # _____ Constants __________
#New account creation 'N'/'W' # New account creation 'N'/'W'
#Supervisor acceptance/rejection 'A'/'R' # Supervisor acceptance/rejection 'A'/'R'
#Yearly revalidation/blockage 'Y'/'B' # Yearly revalidation/blockage 'Y'/'B'
NEWUSER = 'N' NEWUSER = "N"
WAITINGVALIDATION = 'W' WAITINGVALIDATION = "W"
ACCEPTED = 'A' ACCEPTED = "A"
REJECTED = 'R' REJECTED = "R"
YEARREVALIDATION = 'Y' YEARREVALIDATION = "Y"
BLOCKED = 'B' BLOCKED = "B"
PROFILE_STATUS = ( PROFILE_STATUS = (
(NEWUSER, 'New User'), (NEWUSER, "New User"),
(WAITINGVALIDATION, 'Waiting Validation'), (WAITINGVALIDATION, "Waiting Validation"),
(ACCEPTED, 'Accepted'), (ACCEPTED, "Accepted"),
(REJECTED, 'Rejected'), (REJECTED, "Rejected"),
(YEARREVALIDATION, 'Yearly revalidation'), (YEARREVALIDATION, "Yearly revalidation"),
(BLOCKED, 'Blocked no supervisor'), (BLOCKED, "Blocked no supervisor"),
) )
#_____ Fields __________ # _____ Fields __________
# This field is required. # This field is required.
user = models.OneToOneField(User, on_delete=models.CASCADE) user = models.OneToOneField(User, on_delete=models.CASCADE)
# Other fields here # Other fields here
status = models.CharField(max_length=1, choices=PROFILE_STATUS, default=BLOCKED) status = models.CharField(max_length=1, choices=PROFILE_STATUS, default=BLOCKED)
is_supervisor = models.BooleanField(default=False) is_supervisor = models.BooleanField(default=False)
supervision = models.ManyToManyField(SupervisionTrack, related_name='profiles', blank=True) supervision = models.ManyToManyField(
SupervisionTrack, related_name="profiles", blank=True
)
supervision_key = models.CharField(max_length=40, null=True, blank=True) supervision_key = models.CharField(max_length=40, null=True, blank=True)
registration_date = models.DateTimeField(null=True, blank=True) registration_date = models.DateTimeField(null=True, blank=True)
rejection_date = models.DateTimeField(null=True, blank=True) rejection_date = models.DateTimeField(null=True, blank=True)
objects = ProfileManager()
def __unicode__(self): def __unicode__(self):
return u'User: %s' % self.user.username return u"User: %s" % self.user.username
def _generate_current_supervision_key(self): def _generate_current_supervision_key(self):
length = 40 length = 40
return ''.join(random.choice(string.ascii_letters + string.digits) for _ return "".join(
in range(length)) random.choice(string.ascii_letters + string.digits) # nosec
for _ in range(length)
)
def natural_key(self):
return (self.user,)
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs): def create_user_profile(sender, instance, created, **kwargs):
...@@ -114,6 +144,7 @@ def create_user_profile(sender, instance, created, **kwargs): ...@@ -114,6 +144,7 @@ def create_user_profile(sender, instance, created, **kwargs):
else: else:
return return
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs): def save_user_profile(sender, instance, **kwargs):
player, created = Profile.objects.get_or_create(user=instance) player, created = Profile.objects.get_or_create(user=instance)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment