From 2d69197c1140d08eda1d89f7e5938e8139549208 Mon Sep 17 00:00:00 2001 From: Flavio Tarsetti <flavio.tarsetti@idiap.ch> Date: Wed, 5 Jul 2017 15:31:52 +0200 Subject: [PATCH] [accounts/ui-registration-templates] Added endpoint to let supervisees change/add a godfather --- beat/web/accounts/api.py | 256 ++++++++++-------- beat/web/accounts/permissions.py | 13 + .../mail.godfather_rejection.message.txt | 12 + .../mail.godfather_rejection.subject.txt | 1 + ...idation_supervisee_add_request.message.txt | 18 ++ 5 files changed, 188 insertions(+), 112 deletions(-) create mode 100644 beat/web/ui/registration/templates/registration/mail.godfather_rejection.message.txt create mode 100644 beat/web/ui/registration/templates/registration/mail.godfather_rejection.subject.txt create mode 100644 beat/web/ui/registration/templates/registration/mail.godfather_validation_supervisee_add_request.message.txt diff --git a/beat/web/accounts/api.py b/beat/web/accounts/api.py index cfa2c90db..e60396eb4 100644 --- a/beat/web/accounts/api.py +++ b/beat/web/accounts/api.py @@ -135,6 +135,7 @@ class GodfatherAddSuperviseeView(BaseUpdateSupervisionTrackView): supervisiontrack.start_date = now supervisiontrack.last_validation_date = now supervisee.profile.status = Profile.ACCEPTED + supervisee.profile.rejection_date = None supervisiontrack.save() supervisee.profile.save() @@ -248,115 +249,146 @@ class GodfatherRemoveSuperviseeView(BaseUpdateSupervisionTrackView): return Response(status=status.HTTP_204_NO_CONTENT) -##---------------------------------------------------------- -# -# -#class BaseCreateSupervisionTrackViewSupervisee(generics.CreateAPIView): -# model = SupervisionTrack -# serializer_class = SupervisionTrackUpdateSerializer -# -# def get_permissions(self): -# permission_classes = [permissions.IsAuthenticated, IsAuthorAndNotGodfather] -# -# self.permission_classes = permission_classes -# -# return super(BaseCreateSupervisionTrackViewSupervisee, self).get_permissions() -# -# -##---------------------------------------------------------- -# -# -#class SuperviseeAddGodfatherView(BaseCreateSupervisionTrackViewSupervisee): -# permission_classes = BaseCreateSupervisionTrackViewSupervisee.permission_classes -# -# def post(self, request, supervisor_name): -# godfather = User.objects.get(username=supervisor_name) -# supervisee = request.user -# print godfather -# print supervisee -# supervisee.profile.supervision_key = supervisee.profile._generate_current_supervision_key() -# #godfather = User.objects.get(username = self.cleaned_data['godfather']) -# supervisiontrack = SupervisionTrack.objects.create( -# supervisee = supervisee, -# godfather = godfather, -# is_valid = False, -# ) -# -# #Assign key to supervision track -# supervisiontrack.supervision_key = supervisee.profile.supervision_key -# supervisiontrack.save() -# supervisee.profile.supervision.add(supervisiontrack) -# supervisee.save() -# -# #supervisiontrack = SupervisionTrack.objects.get(godfather=request.user, supervisee=supervisee, supervisee__profile__supervision_key=models.F('supervision_key')) -# -# #from django.core.mail import send_mail -# -# #parsed_url = urlparse(settings.URL_PREFIX) -# #server_address = '%s://%s' % (parsed_url.scheme, parsed_url.hostname) -# -# #c = Context({ 'supervisor': supervisiontrack.godfather, -# # 'supervisee': supervisee, -# # 'prefix': server_address, -# # }) -# -# #if supervisee.profile.status == Profile.WAITINGVALIDATION: -# # #New user account waiting validation, so delete this account and inform by email the user -# # try: -# # t = loader.get_template('registration/mail.godfather_rejected.subject.txt') -# # subject = t.render(c) -# -# # # Note: e-mail subject *must not* contain newlines -# # subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines()) -# -# # t = loader.get_template('registration/mail.godfather_rejected_delete_account.message.txt') -# # message = t.render(c) -# -# # send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisee.email]) -# # except: -# # pass -# -# # registration_profile = RegistrationProfile.objects.get(user=supervisee) -# # supervisee.profile.delete() -# # supervisee.delete() -# # supervisiontrack.delete() -# # 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 -# -# # supervisee.profile.status = Profile.REJECTED -# # supervisee.profile.supervision_key = None -# # if supervisee.profile.rejection_date == None: -# # supervisee.profile.rejection_date = now + expiration_date_delta -# -# # supervisiontrack.save() -# # supervisee.profile.save() -# # supervisee.save() -# -# # try: -# # t = loader.get_template('registration/mail.godfather_rejected.subject.txt') -# # subject = t.render(c) -# -# # # Note: e-mail subject *must not* contain newlines -# # subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines()) -# -# # t = loader.get_template('registration/mail.godfather_rejected.message.txt') -# # message = t.render(c) -# -# # send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisee.email]) -# # except: -# # pass -# -# # #if supervisiontrack.start_date is None: -# # # supervisiontrack.delete() -# -# return Response(status=status.HTTP_204_NO_CONTENT) -# -# -##---------------------------------------------------------- +#---------------------------------------------------------- + + +class BaseCreateSupervisionTrackViewSupervisee(generics.CreateAPIView): + model = SupervisionTrack + serializer_class = SupervisionTrackUpdateSerializer + + def get_permissions(self): + permission_classes = [permissions.IsAuthenticated, IsAuthorAndNotGodfather] + + self.permission_classes = permission_classes + + return super(BaseCreateSupervisionTrackViewSupervisee, self).get_permissions() + + +#---------------------------------------------------------- + + +class SuperviseeAddGodfatherView(BaseCreateSupervisionTrackViewSupervisee): + permission_classes = BaseCreateSupervisionTrackViewSupervisee.permission_classes + + def post(self, request, supervisor_name): + godfather = User.objects.get(username=supervisor_name) + 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.WAITINGVALIDATION and supervisee.profile.status != Profile.NEWUSER and supervisee.profile.status != Profile.BLOCKED: + #Stop the current supervision + 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 + supervisiontrack.save() + + #Inform by email the revoked supervisor + from django.core.mail import send_mail + + parsed_url = urlparse(settings.URL_PREFIX) + server_address = '%s://%s' % (parsed_url.scheme, parsed_url.hostname) + + c = Context({ 'supervisor': supervisiontrack.godfather, + 'supervisee': supervisiontrack.supervisee, + 'prefix': server_address, + }) + + try: + t = loader.get_template('registration/mail.godfather_rejection.subject.txt') + subject = t.render(c) + + # Note: e-mail subject *must not* contain newlines + subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines()) + + t = loader.get_template('registration/mail.godfather_rejection.message.txt') + message = t.render(c) + + send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisiontrack.godfather.email]) + except: + pass + + #Reject this account and inform by email the supervisor and the user + + supervisee.profile.status = Profile.REJECTED + supervisee.profile.supervision_key = None + if supervisee.profile.rejection_date == None: + supervisee.profile.rejection_date = now + expiration_date_delta + + supervisee.profile.save() + supervisee.save() + else: + #Not allowed to do this (unproper profile.status) + reason = "You are not able to perform this action, your profile is %s"%(supervisee.profile.status) + 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 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() + + #Create and assign key + supervisee.profile.supervision_key = supervisee.profile._generate_current_supervision_key() + supervisiontrack = SupervisionTrack.objects.create( + supervisee = supervisee, + godfather = godfather, + is_valid = False, + ) + + #Assign key to supervision track + supervisiontrack.supervision_key = supervisee.profile.supervision_key + supervisiontrack.save() + supervisee.profile.supervision.add(supervisiontrack) + supervisee.save() + + #Inform by email the supervisor that he has a new supervisee request + from django.core.mail import send_mail + + parsed_url = urlparse(settings.URL_PREFIX) + server_address = '%s://%s' % (parsed_url.scheme, parsed_url.hostname) + + c = Context({ 'supervisor': godfather, + 'supervisee': supervisee, + 'prefix': server_address, + }) + + try: + t = loader.get_template('registration/mail.godfather_validation.subject.txt') + subject = t.render(c) + + # Note: e-mail subject *must not* contain newlines + subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines()) + + t = loader.get_template('registration/mail.godfather_validation_supervisee_add_request.message.txt') + message = t.render(c) + + send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [godfather.email]) + except: + pass + + return Response(status=status.HTTP_204_NO_CONTENT) + + +#---------------------------------------------------------- diff --git a/beat/web/accounts/permissions.py b/beat/web/accounts/permissions.py index 146b16f1d..18f8c351a 100644 --- a/beat/web/accounts/permissions.py +++ b/beat/web/accounts/permissions.py @@ -39,3 +39,16 @@ class IsGodfatherAndAuthor(permissions.BasePermission): def has_permission(self, request, view): return request.user.profile.is_godfather + + +#---------------------------------------------------------- + + +class IsAuthorAndNotGodfather(permissions.BasePermission): + """ + The logged in user should also be the author + """ + message = 'Not a supervisee account' + + def has_permission(self, request, view): + return not request.user.profile.is_godfather diff --git a/beat/web/ui/registration/templates/registration/mail.godfather_rejection.message.txt b/beat/web/ui/registration/templates/registration/mail.godfather_rejection.message.txt new file mode 100644 index 000000000..af9db2166 --- /dev/null +++ b/beat/web/ui/registration/templates/registration/mail.godfather_rejection.message.txt @@ -0,0 +1,12 @@ +Dear {{ supervisor.first_name }}, + +One of your supervisee has made a request to end your current supervision and request for a new one towards another supervisor related to his current situation. + +First Name: {{ supervisee.first_name }} +Last Name: {{ supervisee.last_name }} +Email: {{ supervisee.email }} +Username: {{ supervisee.username }} + +This message is to inform you that you will not be responsible towards this supervisee in the current future. + +BEAT Administrators at the Idiap Research Institute diff --git a/beat/web/ui/registration/templates/registration/mail.godfather_rejection.subject.txt b/beat/web/ui/registration/templates/registration/mail.godfather_rejection.subject.txt new file mode 100644 index 000000000..bab40169e --- /dev/null +++ b/beat/web/ui/registration/templates/registration/mail.godfather_rejection.subject.txt @@ -0,0 +1 @@ +Account validation - Revoked Supervision diff --git a/beat/web/ui/registration/templates/registration/mail.godfather_validation_supervisee_add_request.message.txt b/beat/web/ui/registration/templates/registration/mail.godfather_validation_supervisee_add_request.message.txt new file mode 100644 index 000000000..d490fbc6e --- /dev/null +++ b/beat/web/ui/registration/templates/registration/mail.godfather_validation_supervisee_add_request.message.txt @@ -0,0 +1,18 @@ +Dear {{ supervisor.first_name }}, + +Thank you in advance for validating the request of one your supervisee at the Idiap Research Institute's Biometric +Evaluation and Testing (BEAT) platform. Before we can activate this +supervision requesti, you must login to your account and under supervision tab validate the following supervisee: + +First Name: {{ supervisee.first_name }} +Last Name: {{ supervisee.last_name }} +Email: {{ supervisee.email }} +Username: {{ supervisee.username }} + +If you don't do this the supervisee will not be able to use his account in +future and will not be recognized as your supervisee. + +If you are having problems to activate your supervisee account, contact a member of our +support staff at {{ prefix }}{% url 'contact' %}. + +BEAT Administrators at the Idiap Research Institute -- GitLab