From be6375566e5ad66d6e91b22c5fecc0241dbb46e9 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 16 Feb 2020 07:17:15 +0000 Subject: [PATCH] Allow instructors to sign members up for classes --- apiserver/apiserver/api/serializers.py | 2 +- apiserver/apiserver/api/views.py | 39 ++++++++++++++++++-------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index a0a4d59..fb43881 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -263,7 +263,7 @@ class TrainingSerializer(serializers.ModelSerializer): class Meta: model = models.Training fields = '__all__' - read_only_fields = ['user', 'sign_up_date', 'paid_date', 'member_id'] + read_only_fields = ['user', 'sign_up_date', 'paid_date'] def get_student_name(self, obj): if obj.user: diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index cbe7910..602bae0 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -196,20 +196,37 @@ class TrainingViewSet(Base, Retrieve, Create, Update): return serializers.StudentTrainingSerializer # TODO: turn these into @actions - # TODO: check if full + # TODO: check if full, but not for instructors # TODO: if already paid, skip to confirmed def perform_create(self, serializer): - session_id = self.request.data['session'] - status = self.request.data['attendance_status'] + 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) - training = models.Training.objects.filter(user=self.request.user, session=session) - if training.exists(): - raise exceptions.ValidationError('You have already registered') - if self.request.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=self.request.user, attendance_status=status) + + 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.')) + + 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']