From 131a8a46a6e578ee2cb55029ee8adc5fa77a598c Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Tue, 15 Sep 2020 20:00:53 +0000 Subject: [PATCH] Abstract updating certs to a method --- apiserver/apiserver/api/utils_ldap.py | 2 +- apiserver/apiserver/api/views.py | 128 +++++++++++--------------- 2 files changed, 54 insertions(+), 76 deletions(-) diff --git a/apiserver/apiserver/api/utils_ldap.py b/apiserver/apiserver/api/utils_ldap.py index ac8f1d4..62ad5ee 100644 --- a/apiserver/apiserver/api/utils_ldap.py +++ b/apiserver/apiserver/api/utils_ldap.py @@ -71,6 +71,6 @@ def remove_from_group(member, group): except BaseException as e: logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e))) m = '{} {} ({})'.format(member.first_name, member.last_name, member.id) - msg = 'Problem adding {} to group {}!'.format(m, group) + msg = 'Problem removing {} from group {}!'.format(m, group) utils.alert_tanner(msg) logger.info(msg) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index cdb88f1..4e7d3ac 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -200,81 +200,8 @@ class TrainingViewSet(Base, Retrieve, Create, Update): else: return serializers.StudentTrainingSerializer - # TODO: turn these into @actions - # TODO: check if full, but not for instructors - # TODO: if already paid, skip to confirmed - def perform_create(self, serializer): - user = self.request.user - data = self.request.data - session_id = data['session'] - status = data['attendance_status'] - session = get_object_or_404(models.Session, id=session_id) - - if data.get('member_id', None): - if not (is_admin_director(user) or session.instructor == user): - raise exceptions.ValidationError('Not allowed to register others') - - member = get_object_or_404(models.Member, id=data['member_id']) - user = getattr(member, 'user', None) - - training1 = models.Training.objects.filter(user=user, session=session) - training2 = models.Training.objects.filter(member_id=member.id, session=session) - if (user and training1.exists()) or training2.exists(): - raise exceptions.ValidationError(dict(non_field_errors='Already registered.')) - - if session.course.id == 249: - member.orientation_date = utils.today_alberta_tz() if status == 'Attended' else None - elif session.course.id == 261: - member.wood_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - elif session.course.id == 401: - member.wood2_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - elif session.course.id == 281: - member.lathe_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - elif session.course.id == 283: - member.mill_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - elif session.course.id == 259: - member.cnc_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - elif session.course.id == 247: - member.rabbit_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - - if status == 'Attended': - utils_ldap.add_to_group(member, 'Laser Users') - else: - utils_ldap.remove_from_group(member, 'Laser Users') - elif session.course.id == 321: - member.trotec_cert_date = utils.today_alberta_tz() if status == 'Attended' else None - - if status == 'Attended': - utils_ldap.add_to_group(member, 'Trotec Users') - else: - utils_ldap.remove_from_group(member, 'Trotec Users') - member.save() - - serializer.save(user=user, member_id=member.id, attendance_status=status) - else: - training = models.Training.objects.filter(user=user, session=session) - if training.exists(): - raise exceptions.ValidationError('Already registered') - if user == session.instructor: - raise exceptions.ValidationError('You are teaching this session') - if status == 'Waiting for payment' and session.cost == 0: - status = 'Confirmed' - serializer.save(user=user, attendance_status=status) - - def perform_update(self, serializer): - session_id = self.request.data['session'] - status = self.request.data['attendance_status'] - session = get_object_or_404(models.Session, id=session_id) - if status == 'Waiting for payment' and session.cost == 0: - status = 'Confirmed' - - training = serializer.save(attendance_status=status) - - if training.user: - member = training.user.member - else: - member = models.Member.objects.get(id=training.member_id) - + def update_cert(self, session, member, status): + # always update cert date incase member is returning and gets recertified if session.course.id == 249: member.orientation_date = utils.today_alberta_tz() if status == 'Attended' else None elif session.course.id == 261: @@ -303,6 +230,57 @@ class TrainingViewSet(Base, Retrieve, Create, Update): utils_ldap.remove_from_group(member, 'Trotec Users') member.save() + # TODO: turn these into @actions + # TODO: check if full, but not for instructors + # TODO: if already paid, skip to confirmed + def perform_create(self, serializer): + user = self.request.user + data = self.request.data + session_id = data['session'] + status = data['attendance_status'] + session = get_object_or_404(models.Session, id=session_id) + + if data.get('member_id', None): + if not (is_admin_director(user) or session.instructor == user): + raise exceptions.ValidationError('Not allowed to register others') + + member = get_object_or_404(models.Member, id=data['member_id']) + user = getattr(member, 'user', None) + + training1 = models.Training.objects.filter(user=user, session=session) + training2 = models.Training.objects.filter(member_id=member.id, session=session) + if (user and training1.exists()) or training2.exists(): + raise exceptions.ValidationError(dict(non_field_errors='Already registered.')) + + self.update_cert(session, member, status) + + serializer.save(user=user, member_id=member.id, attendance_status=status) + else: + training = models.Training.objects.filter(user=user, session=session) + if training.exists(): + raise exceptions.ValidationError('Already registered') + if user == session.instructor: + raise exceptions.ValidationError('You are teaching this session') + if status == 'Waiting for payment' and session.cost == 0: + status = 'Confirmed' + serializer.save(user=user, attendance_status=status) + + def perform_update(self, serializer): + session_id = self.request.data['session'] + status = self.request.data['attendance_status'] + session = get_object_or_404(models.Session, id=session_id) + if status == 'Waiting for payment' and session.cost == 0: + status = 'Confirmed' + + training = serializer.save(attendance_status=status) + + if training.user: + member = training.user.member + else: + member = models.Member.objects.get(id=training.member_id) + + self.update_cert(session, member, status) + class TransactionViewSet(Base, List, Create, Retrieve, Update): permission_classes = [AllowMetadata | IsAuthenticated, IsObjOwnerOrAdmin]