Commit efc8efeb authored by Flavio TARSETTI's avatar Flavio TARSETTI

[accounts][api/views] Code refactoring - remove duplicates

parent 1004f75a
Pipeline #39873 canceled with stage
in 4 minutes and 27 seconds
......@@ -42,6 +42,10 @@ from .serializers import UserSerializer
from .models import Profile
from .models import SupervisionTrack
from .models import TemporaryUrl
from .views import accept_supervisee
from .views import perform_revalidation
from .views import supervisee_rejection
from .views import emergency_rejection
from ..ui.registration.models import RegistrationProfile
from ..utils import mail
......@@ -124,6 +128,7 @@ class SupervisorAddSuperviseeView(BaseUpdateSupervisionTrackView):
permission_classes = BaseUpdateSupervisionTrackView.permission_classes
def put(self, request, supervisee_name):
now = datetime.datetime.now()
supervisee = User.objects.get(username=supervisee_name)
if supervisee.profile.status != Profile.ACCEPTED:
if not request.user.is_superuser:
......@@ -152,39 +157,7 @@ class SupervisorAddSuperviseeView(BaseUpdateSupervisionTrackView):
supervisee.profile.supervision_key
)
supervisiontrack.is_valid = True
now = datetime.datetime.now()
expiration_date_delta = datetime.timedelta(
days=settings.ACCOUNT_EXPIRATION_DAYS
)
supervisiontrack.expiration_date = now + expiration_date_delta
supervisiontrack.start_date = now
supervisiontrack.last_validation_date = now
supervisee.profile.status = Profile.ACCEPTED
supervisee.profile.rejection_date = None
supervisiontrack.save()
supervisee.profile.save()
supervisee.is_active = True
supervisee.save()
parsed_url = urlparse(settings.URL_PREFIX)
server_address = "%s://%s" % (parsed_url.scheme, parsed_url.hostname)
context = {
"supervisor": supervisiontrack.supervisor,
"supervisee": supervisee,
"prefix": server_address,
}
mail.send_email(
"registration/mail.supervisor_validated.subject.txt",
"registration/mail.supervisor_validated.message.txt",
context,
[supervisee.email],
)
accept_supervisee(supervisiontrack, supervisee, now)
return Response(status=status.HTTP_204_NO_CONTENT)
else:
......@@ -206,6 +179,7 @@ class SupervisorRemoveSuperviseeView(BaseUpdateSupervisionTrackView):
def put(self, request, supervisee_name):
supervisee = User.objects.get(username=supervisee_name)
now = datetime.datetime.now()
if not supervisee.profile.is_supervisor:
supervisiontrack = SupervisionTrack.objects.get(
supervisor=request.user,
......@@ -270,26 +244,7 @@ class SupervisorRemoveSuperviseeView(BaseUpdateSupervisionTrackView):
registration_profile.delete()
else:
# Reject this account and inform by email the user
now = datetime.datetime.now()
expiration_date_delta = datetime.timedelta(
days=settings.ACCOUNT_BLOCKAGE_AFTER_FIRST_REJECTION_DAYS
)
supervisiontrack.expiration_date = now
supervisiontrack.is_valid = False
if supervisee.profile.status != Profile.BLOCKED:
supervisee.profile.status = Profile.REJECTED
if supervisee.profile.rejection_date is None:
supervisee.profile.rejection_date = now + expiration_date_delta
else:
supervisee.profile.rejection_date = None
supervisee.profile.supervision_key = None
supervisiontrack.save()
supervisee.profile.save()
supervisee.save()
supervisee_rejection(supervisiontrack, supervisee, now)
if not supervisee.profile.is_supervisor:
......@@ -627,97 +582,18 @@ class SuperviseeReValidationView(BaseUpdateSupervisionTrackFromSuperviseeView):
supervisee=supervisee,
supervisee__profile__supervision_key=models.F("supervision_key"),
)
if supervisiontrack.is_valid:
if supervisee.profile.status == Profile.YEARREVALIDATION:
# Check Supervisor validity
supervisor = supervisiontrack.supervisor
# If Supervisor account is not valid. Reject the account (though this should already be done during supervisor rejection)
if supervisor.profile.status != Profile.BLOCKED:
# Change status
supervisee.profile.status = Profile.ACCEPTED
# Extend supervisiontrack validity for another 12 months
expiration_date_delta = datetime.timedelta(
days=settings.ACCOUNT_EXPIRATION_DAYS
)
new_expiration_date = (
supervisiontrack.expiration_date + expiration_date_delta
)
supervisiontrack.expiration_date = new_expiration_date
supervisiontrack.last_validation_date = now
else:
# Change status
expiration_date_delta = datetime.timedelta(
days=settings.ACCOUNT_BLOCKAGE_AFTER_FIRST_REJECTION_DAYS
)
supervisiontrack.expiration_date = now
supervisiontrack.is_valid = False
supervisee.profile.status = Profile.REJECTED
supervisee.profile.rejection_date = now + expiration_date_delta
supervisee.profile.supervision_key = None
# save
supervisiontrack.save()
supervisee.profile.save()
supervisee.save()
# Inform supervisor about supervisee revalidation
# Possible supervisor rejection available
parsed_url = urlparse(settings.URL_PREFIX)
server_address = "%s://%s" % (
parsed_url.scheme,
parsed_url.hostname,
)
temp_url_rejection = TemporaryUrl.objects.create_temporary_url(
TemporaryUrl.REJECTION, supervisiontrack
)
context = {
"supervisor": supervisiontrack.supervisor,
"supervisee": supervisee,
"prefix": server_address,
"temp_url": temp_url_rejection.url_hash,
}
mail.send_email(
"registration/mail.account_revalidation_supervisor.subject.txt",
"registration/mail.supervisor_possible_supervisee_rejection.message.txt",
context,
[supervisiontrack.supervisor.email],
)
else:
# Track already valid
reason = "You don't need to revalidate at the moment, your supervision is still valid"
result = {
"error": reason,
}
return BadRequestResponse(result)
else:
# A pending request already exist
reason = "You are not able to perform this action as you already have a pending supervision request"
result = {
"error": reason,
}
success, error_message = perform_revalidation(request, supervisiontrack, supervisee, now)
result = {
"error": error_message,
}
if not success:
return BadRequestResponse(result)
else:
# No key is present in supervisee
# Make sure all tracks are invalid
supervisiontracks = SupervisionTrack.objects.filter(
supervisee=supervisee, is_valid=True
)
# This should never be the case but if it happens invalidate all tracks
if supervisiontracks.count() > 0:
now = datetime.datetime.now()
for track in supervisiontracks:
track.is_valid = False
track.expiration_date = now
track.save()
# Not allowed to do this (unproper profile.status)
reason = "You are not allowed to perform this action, you first need to get a valid supervision"
else:
error_message = emergency_rejection(request, supervisee)
result = {
"error": reason,
"error": error_message,
}
return BadRequestResponse(result)
......
This diff is collapsed.
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