Commit efc8efeb authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[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)
......
......@@ -47,6 +47,7 @@ from .models import Profile
from .models import TemporaryUrl
from ..utils import mail
from ..common.responses import BadRequestResponse
import datetime
from urllib.parse import urlparse
......@@ -171,6 +172,155 @@ def account_settings(request):
)
def emergency_rejection(request, supervisee):
# 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)
error_message = "You are not allowed to perform this action, you first need to get a valid supervision"
return error_message
def supervisee_rejection(supervisiontrack, supervisee, 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()
def perform_revalidation(request, supervisiontrack, supervisee, now):
success = False
error_message = None
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],
)
success = True
return(success, error_message)
else:
# Track already valid
error_message = "You don't need to revalidate at the moment, your supervision is still valid"
return(success, error_message)
else:
# A pending request already exist
error_message = "You are not able to perform this action as you already have a pending supervision request"
return(success, error_message)
def accept_supervisee(supervisiontrack, supervisee, now):
supervisiontrack.is_valid = True
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],
)
def generate_load_response(request, temp_url, template, parameters):
answer = {}
answer["template"] = template
......@@ -191,38 +341,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.is_valid = True
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 generate_load_response(
request,
......@@ -238,101 +357,16 @@ def load_temporary_url_validation(request, hash_url):
elif temp_url.status == TemporaryUrl.YEARREVALIDATION:
# Supervisee re-validation
if supervisee.profile.supervision_key is not None and now < temp_url.expires:
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
error_message = "You don't need to revalidate at the moment, your supervision is still valid"
return generate_load_response(
request,
temp_url,
"accounts/url_revalidation_failed.html",
{"error_message": error_message},
)
else:
# A pending request already exist
error_message = "You are not able to perform this action as you already have a pending supervision request"
success, error_message = perform_revalidation(request, supervisiontrack, supervisee, now)
if not success:
return generate_load_response(
request,
temp_url,
"accounts/url_revalidation_failed.html",
{"error_message": error_message},
)
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)
error_message = "You are not allowed to perform this action, you first need to get a valid supervision"
error_message = emergency_rejection(request, supervisee)
return generate_load_response(
request,
temp_url,
......@@ -374,26 +408,7 @@ def load_temporary_url_rejection(request, hash_url):
# Supervisor rejects Supervisee
if supervisee.profile.supervision_key is not None and now < temp_url.expires:
if supervisiontrack.is_valid:
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)
mail.send_email(
"registration/mail.supervisor_rejected.subject.txt",
"registration/mail.supervisor_rejected.message.txt",
......
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