Commit 8235a2f2 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[accounts][views] load temporary url

parent 0e82886b
......@@ -25,19 +25,42 @@
# #
###############################################################################
from django.conf import settings
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib import messages
from django.db import models
from django.db.models import Q
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework import status
from .forms import AccountSettingsForm
from .models import AccountSettings
from .models import SupervisionTrack, Profile
from .models import SupervisionTrack
from .models import Profile
from .models import TemporaryUrl
from ..utils import mail
from ..common.responses import BadRequestResponse, ForbiddenResponse
from datetime import datetime, timedelta
import datetime
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
@login_required
def account_settings(request):
......@@ -114,3 +137,128 @@ def account_settings(request):
'supervisors_valid': supervisors_valid,
'supervisors_pending': supervisors_pending,
'token' : user.auth_token})
def load_temporary_url(request, hash_url):
temp_url = get_object_or_404(TemporaryUrl, url_hash=hash_url)
supervisiontrack = temp_url.supervision_track
supervisee = supervisiontrack.supervisee
now = datetime.datetime.now()
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])
temp_url.delete()
return render(request,
'accounts/url_validation.html',
{'supervisiontrack': supervisiontrack})
else:
#Track already valid
temp_url.delete()
return render(request,
'accounts/url_validation_failed.html')
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()
else:
#Track already valid
error_message = "You don't need to revalidate at the moment, your supervision is still valid"
temp_url.delete()
return render(request,
'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"
temp_url.delete()
return render(request,
'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"
temp_url.delete()
return render(request,
'accounts/url_revalidation_failed.html',
{'error_message': error_message})
temp_url.delete()
return render(request,
'accounts/url_validation.html',
{'supervisiontrack': supervisiontrack})
else:
#Track already valid
temp_url.delete()
return render(request,
'accounts/url_validation_failed.html')
Supports Markdown
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