Commit 4be47912 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[accounts] added revalidation endpoint to api and api_urls

parent d092b9f8
Pipeline #11382 passed with stage
in 41 minutes and 25 seconds
......@@ -397,3 +397,91 @@ class SuperviseeAddGodfatherView(BaseCreateSupervisionTrackViewSupervisee):
#----------------------------------------------------------
class BaseUpdateSupervisionTrackFromSuperviseeView(generics.UpdateAPIView):
model = SupervisionTrack
serializer_class = SupervisionTrackUpdateSerializer
def get_permissions(self):
permission_classes = [permissions.IsAuthenticated, IsAuthorAndNotGodfather]
self.permission_classes = permission_classes
return super(BaseUpdateSupervisionTrackFromSuperviseeView, self).get_permissions()
#----------------------------------------------------------
class SuperviseeReValidationView(BaseUpdateSupervisionTrackFromSuperviseeView):
permission_classes = BaseUpdateSupervisionTrackFromSuperviseeView.permission_classes
def put(self, request):
supervisee = request.user
if supervisee.profile.supervision_key is not None:
#There's a key check if there's a valid track
supervisiontrack = SupervisionTrack.objects.get(supervisee=supervisee, supervisee__profile__supervision_key=models.F('supervision_key'))
if supervisiontrack.is_valid:
if supervisee.profile.status == Profile.YEARREVALIDATION:
#Check Godfather validity
godfather = supervisiontrack.godfather
#If Godfather account is not valid. Reject the account (though this should already be done during godfather rejection)
if godfather.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
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
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,
}
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"
result = {
'error': reason,
}
return BadRequestResponse(result)
return Response(status=status.HTTP_204_NO_CONTENT)
#----------------------------------------------------------
......@@ -52,4 +52,11 @@ urlpatterns = [
api.SuperviseeAddGodfatherView.as_view(),
name='add_supervisor'
),
url(
r'^revalidate/$',
api.SuperviseeReValidationView.as_view(),
name='revalidate_account'
),
]
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