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,27 +33,38 @@ from django.dispatch import receiver
import random
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:
verbose_name_plural = "account settings"
owner = models.OneToOneField(User)
daily_summary = models.BooleanField(default=True)
experiment_mail_notifications_enabled = models.BooleanField(default=True)
database_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):
#_____ Fields __________
# _____ Fields __________
supervisee = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='supervisors')
supervisor = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='supervisees')
supervisee = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="supervisors"
)
supervisor = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="supervisees"
)
is_valid = models.BooleanField(default=False)
start_date = models.DateTimeField(null=True, blank=True)
expiration_date = models.DateTimeField(null=True, blank=True)
......@@ -61,32 +72,41 @@ class SupervisionTrack(models.Model):
supervision_key = models.CharField(max_length=40, null=True, blank=True)
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):
#_____ Constants __________
#New account creation 'N'/'W'
#Supervisor acceptance/rejection 'A'/'R'
#Yearly revalidation/blockage 'Y'/'B'
NEWUSER = 'N'
WAITINGVALIDATION = 'W'
ACCEPTED = 'A'
REJECTED = 'R'
YEARREVALIDATION = 'Y'
BLOCKED = 'B'
# _____ Constants __________
# New account creation 'N'/'W'
# Supervisor acceptance/rejection 'A'/'R'
# Yearly revalidation/blockage 'Y'/'B'
NEWUSER = "N"
WAITINGVALIDATION = "W"
ACCEPTED = "A"
REJECTED = "R"
YEARREVALIDATION = "Y"
BLOCKED = "B"
PROFILE_STATUS = (
(NEWUSER, 'New User'),
(WAITINGVALIDATION, 'Waiting Validation'),
(ACCEPTED, 'Accepted'),
(REJECTED, 'Rejected'),
(YEARREVALIDATION, 'Yearly revalidation'),
(BLOCKED, 'Blocked no supervisor'),
(NEWUSER, "New User"),
(WAITINGVALIDATION, "Waiting Validation"),
(ACCEPTED, "Accepted"),
(REJECTED, "Rejected"),
(YEARREVALIDATION, "Yearly revalidation"),
(BLOCKED, "Blocked no supervisor"),
)
#_____ Fields __________
# _____ Fields __________
# This field is required.
user = models.OneToOneField(User, on_delete=models.CASCADE)
......@@ -94,18 +114,28 @@ class Profile(models.Model):
# Other fields here
status = models.CharField(max_length=1, choices=PROFILE_STATUS, default=BLOCKED)
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)
registration_date = models.DateTimeField(null=True, blank=True)
rejection_date = models.DateTimeField(null=True, blank=True)
objects = ProfileManager()
def __unicode__(self):
return u'User: %s' % self.user.username
return u"User: %s" % self.user.username
def _generate_current_supervision_key(self):
length = 40
return ''.join(random.choice(string.ascii_letters + string.digits) for _
in range(length))
return "".join(
random.choice(string.ascii_letters + string.digits) # nosec
for _ in range(length)
)
def natural_key(self):
return (self.user,)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
......@@ -114,6 +144,7 @@ def create_user_profile(sender, instance, created, **kwargs):
else:
return
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
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