Commit 3ae11144 authored by Flavio TARSETTI's avatar Flavio TARSETTI

[accounts] add Supervisor(Manager/QuerySet) to models

parent dd22571e
Pipeline #46712 passed with stage
in 18 minutes and 45 seconds
......@@ -25,8 +25,6 @@
# #
###############################################################################
import datetime
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
......@@ -96,7 +94,6 @@ class BaseUpdateSupervisionTrackView(generics.UpdateAPIView):
class SupervisorAddSuperviseeView(BaseUpdateSupervisionTrackView):
def put(self, request, supervisee_name):
now = datetime.datetime.now()
supervisee = User.objects.get(username=supervisee_name)
if supervisee.profile.status != Profile.ACCEPTED:
try:
......@@ -116,7 +113,7 @@ class SupervisorAddSuperviseeView(BaseUpdateSupervisionTrackView):
supervisee.profile.supervision_key
)
supervisiontrack.accept_supervisee(now)
supervisiontrack.accept_supervisee()
return Response(status=status.HTTP_204_NO_CONTENT)
else:
......@@ -177,7 +174,7 @@ class SupervisorRemoveSuperviseeView(BaseUpdateSupervisionTrackView):
}
if supervisee.profile.status == Profile.WAITINGVALIDATION:
SupervisionTrack.supervisiontracks.terminate_supervisee(supervisiontrack)
SupervisionTrack.objects.terminate_supervisee(supervisiontrack)
supervisiontrack.delete()
else:
# Reject this account and inform by email the user
......@@ -234,7 +231,6 @@ class SuperviseeReValidationView(generics.UpdateAPIView):
def put(self, request):
supervisee = request.user
now = datetime.datetime.now()
if supervisee.profile.supervision_key is not None:
# There's a key check if there's a valid track
......@@ -244,7 +240,7 @@ class SuperviseeReValidationView(generics.UpdateAPIView):
)
success, error_message = perform_revalidation(
request, supervisiontrack, supervisee, now
request, supervisiontrack, supervisee
)
if not success:
raise drf_exceptions.PermissionDenied(error_message)
......@@ -296,7 +292,7 @@ class RemoveSupervisorModeView(BaseUpdateSupervisorModeView):
user.save()
user.profile.reject_supervisees()
SupervisionTrack.supervisiontracks.delete_new_requests_for_supervisor(user)
SupervisionTrack.objects.delete_new_requests_for_supervisor(user)
else:
# Not allowed to do this (unproper profile.status - not admin)
raise drf_exceptions.PermissionDenied()
......@@ -348,7 +344,6 @@ class UpdateSupervisorCandidateView(generics.UpdateAPIView):
def put(self, request):
user = request.user
now = datetime.datetime.now()
if user.is_superuser:
try:
......@@ -359,7 +354,7 @@ class UpdateSupervisorCandidateView(generics.UpdateAPIView):
raise drf_exceptions.NotFound(reason)
supervisee = User.objects.get(username=supervisee_username)
grant_supervisor_status(user, supervisee, now)
grant_supervisor_status(user, supervisee)
return Response(status=status.HTTP_204_NO_CONTENT)
......
......@@ -183,7 +183,8 @@ def reject_supervisee(supervisiontrack, supervisee, context):
)
def perform_revalidation(request, supervisiontrack, supervisee, now):
def perform_revalidation(request, supervisiontrack, supervisee):
now = datetime.now()
success = False
error_message = None
if supervisiontrack.is_valid:
......@@ -255,9 +256,11 @@ def perform_revalidation(request, supervisiontrack, supervisee, now):
return (success, error_message)
def grant_supervisor_status(admin, user, now):
def grant_supervisor_status(admin, user):
# Invalidate all valid tracks
# This should never be the case but if it happens invalidate all tracks
now = datetime.now()
SupervisionTrack.objects.filter(supervisee=user, is_valid=True).update(
is_valid=False, expiration_date=now
)
......
......@@ -72,6 +72,24 @@ class AccountSettings(models.Model):
class SupervisionTrackQuerySet(models.QuerySet):
def get_rejected_tracks(self, supervisor):
supervisiontracks = (
self.filter(supervisor=supervisor)
.filter(
Q(supervisee__profile__status=Profile.ACCEPTED)
| Q(supervisee__profile__status=Profile.YEARREVALIDATION)
| Q(supervisee__profile__status=Profile.BLOCKED)
)
.filter(Q(supervisee__profile__supervision_key=models.F("supervision_key")))
)
if supervisor.is_superuser:
supervisiontracks = supervisiontracks.exclude(
Q(supervisee__profile__is_supervisor=True)
)
return supervisiontracks
def new_requests_for_supervisor(self, supervisor):
return self.filter(supervisor=supervisor).filter(
Q(supervisee__profile__status=Profile.NEWUSER)
......@@ -101,9 +119,17 @@ class SupervisionTrackQuerySet(models.QuerySet):
class SupervisionTrackManager(models.Manager):
def get_rejected_supervisees_tracks(self, supervisor):
return SupervisionTrackQuerySet(self.model, using=self._db).get_rejected_tracks(
supervisor
)
def get_new_requests(self):
return SupervisionTrackQuerySet(self.model, using=self._db)
def rejected_supervisees_tracks(self, supervisor):
return self.get_rejected_supervisees_tracks(supervisor)
def delete_new_requests_for_supervisor(self, supervisor):
return self.get_new_requests().delete_new_requests(supervisor)
......@@ -126,8 +152,7 @@ class SupervisionTrack(models.Model):
expiration_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)
supervisiontracks = SupervisionTrackManager()
objects = models.Manager()
objects = SupervisionTrackManager()
def __str__(self):
return u"Supervisor: %s, Supervisee, %s, Validity: %s" % (
......@@ -136,7 +161,9 @@ class SupervisionTrack(models.Model):
self.is_valid,
)
def accept_supervisee(self, now):
def accept_supervisee(self):
now = datetime.datetime.now()
supervisee = self.supervisee
self.is_valid = True
......@@ -166,6 +193,34 @@ class SupervisionTrack(models.Model):
)
class SupervisorQuerySet(models.QuerySet):
def revoke_supervisor(self, supervisor):
if supervisor in self.filter(is_supervisor=True):
supervisor.reject_supervisees_and_requests()
context = {
"supervisee": supervisor.user,
}
# Transform supervisor account to normal account and inform by email
supervisor.is_supervisor = False
supervisor.save()
supervisor.user.save()
account_send_email(
AccountEmail.SUPERVISOR_WARNING_REJECTED_BLOCKED_BY_BEATADMIN,
context,
supervisor.user.email,
)
class SupervisorManager(models.Manager):
def get_queryset(self):
return SupervisorQuerySet(self.model, using=self._db)
def revoke_supervisor(self, supervisor):
return self.get_queryset().revoke_supervisor(supervisor)
class ProfileManager(models.Manager):
def get_by_natural_key(self, user):
return self.get(user=user)
......@@ -209,6 +264,7 @@ class Profile(models.Model):
rejection_date = models.DateTimeField(null=True, blank=True)
objects = ProfileManager()
supervisors = SupervisorManager()
def __str__(self):
return "User: %s" % self.user.username
......@@ -223,23 +279,6 @@ class Profile(models.Model):
def natural_key(self):
return (self.user,)
def cancel_supervisor(self):
if self.is_supervisor:
context = {
"supervisee": self.user,
}
# Transform supervisor account to normal account and inform by email
self.is_supervisor = False
self.save()
self.user.save()
account_send_email(
AccountEmail.SUPERVISOR_WARNING_REJECTED_BLOCKED_BY_BEATADMIN,
context,
self.user.email,
)
def block_user_and_supervisiontrack(self, supervisiontrack):
# if supervisor account reject all supervisees and cancel supervisor
self.revoke_supervisor()
......@@ -262,26 +301,14 @@ class Profile(models.Model):
def revoke_supervisor(self):
# if supervisor account reject all supervisees and cancel supervisor
if self.is_supervisor:
self.reject_supervisees_and_requests()
self.cancel_supervisor()
Profile.supervisors.revoke_supervisor(self)
def reject_supervisees(self):
# Reject all his supervisees and inform by email
# Make sure all supervision tracks are invalid for rejected supervisor
supervisiontracks = (
SupervisionTrack.objects.filter(supervisor=self.user)
.filter(
Q(supervisee__profile__status=Profile.ACCEPTED)
| Q(supervisee__profile__status=Profile.YEARREVALIDATION)
| Q(supervisee__profile__status=Profile.BLOCKED)
)
.filter(Q(supervisee__profile__supervision_key=models.F("supervision_key")))
supervisiontracks = SupervisionTrack.objects.rejected_supervisees_tracks(
self.user
)
if self.user.is_superuser:
supervisiontracks = supervisiontracks.exclude(
Q(supervisee__profile__is_supervisor=True)
)
now = datetime.datetime.now()
expiration_date_delta = datetime.timedelta(
......@@ -342,7 +369,7 @@ class Profile(models.Model):
Nothing is returned
"""
self.reject_supervisees()
SupervisionTrack.supervisiontracks.delete_new_requests_for_supervisor(self.user)
SupervisionTrack.objects.delete_new_requests_for_supervisor(self.user)
@receiver(post_save, sender=User)
......
......@@ -178,7 +178,7 @@ def load_temporary_url_validation(request, hash_url):
if temp_url.status == TemporaryUrl.VALIDATION:
# Supervisor validates new Supervisee
if supervisee.profile.status != Profile.ACCEPTED and now < temp_url.expires:
supervisiontrack.accept_supervisee(now)
supervisiontrack.accept_supervisee()
return generate_answer(
request,
......@@ -195,7 +195,7 @@ def load_temporary_url_validation(request, hash_url):
# Supervisee re-validation
if supervisee.profile.supervision_key is not None and now < temp_url.expires:
success, error_message = perform_revalidation(
request, supervisiontrack, supervisee, now
request, supervisiontrack, supervisee
)
if not success:
return generate_answer(
......
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