Abstract updating certs to a method

This commit is contained in:
Tanner Collin 2020-09-15 20:00:53 +00:00
parent ddb83a1b58
commit 131a8a46a6
2 changed files with 54 additions and 76 deletions

View File

@ -71,6 +71,6 @@ def remove_from_group(member, group):
except BaseException as e: except BaseException as e:
logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e))) logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e)))
m = '{} {} ({})'.format(member.first_name, member.last_name, member.id) 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) utils.alert_tanner(msg)
logger.info(msg) logger.info(msg)

View File

@ -200,28 +200,8 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
else: else:
return serializers.StudentTrainingSerializer return serializers.StudentTrainingSerializer
# TODO: turn these into @actions def update_cert(self, session, member, status):
# TODO: check if full, but not for instructors # always update cert date incase member is returning and gets recertified
# 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: if session.course.id == 249:
member.orientation_date = utils.today_alberta_tz() if status == 'Attended' else None member.orientation_date = utils.today_alberta_tz() if status == 'Attended' else None
elif session.course.id == 261: elif session.course.id == 261:
@ -250,6 +230,30 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
utils_ldap.remove_from_group(member, 'Trotec Users') utils_ldap.remove_from_group(member, 'Trotec Users')
member.save() 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) serializer.save(user=user, member_id=member.id, attendance_status=status)
else: else:
training = models.Training.objects.filter(user=user, session=session) training = models.Training.objects.filter(user=user, session=session)
@ -275,33 +279,7 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
else: else:
member = models.Member.objects.get(id=training.member_id) member = models.Member.objects.get(id=training.member_id)
if session.course.id == 249: self.update_cert(session, member, status)
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()
class TransactionViewSet(Base, List, Create, Retrieve, Update): class TransactionViewSet(Base, List, Create, Retrieve, Update):