def test_serializer_valid_participant(self): data = {'isMentor': False} serializer = ParticipantSerializer(data=data) self.assertTrue(serializer.is_valid()) participant = serializer.save(user=self.user, cohort=self.cohort) self.assertFalse(participant.isMatched) self.assertTrue(participant.signUpDate <= timezone.now())
def test_serializer_ignore_duplicate_tags(self): data = {'isMentor': True, 'tags': ['node.js', 'sports', 'sports']} Tag.objects.create(name="node.js") Tag.objects.create(name="sports") serializer = ParticipantSerializer(data=data) if not serializer.is_valid(): self.fail(serializer.errors) participant = serializer.save(user=self.user, cohort=self.cohort) self.assertEqual(len(participant.tags.all()), 2)
def test_serializer_cant_apply_for_same_cohort_twice(self): data = {'isMentor': True} serializer = ParticipantSerializer(data=data) serializer.is_valid() participant = serializer.save(user=self.user, cohort=self.cohort) data = {'isMentor': False} serializer = ParticipantSerializer(data=data) serializer.is_valid() with self.assertRaises(IntegrityError): participant = serializer.save(user=self.user, cohort=self.cohort)
def getTopThree(self, request, **kwargs): try: p = Participant.objects.get( participantId=self.kwargs['participantId']) if not p.user.username == self.request.user.username: return JSONResponse( { 'detail': 'You do not have permission to see this participant\'s details' }, status=status.HTTP_403_FORBIDDEN) if p.isMentor: return JSONResponse( { 'detail': 'Only mentees can view their top three matches' }, status=status.HTTP_403_FORBIDDEN) if timezone.now() < p.cohort.closeDate: return JSONResponse({'detail': 'Matching has not yet begun'}, status=status.HTTP_403_FORBIDDEN) if timezone.now() > p.cohort.matchDate: return JSONResponse( { 'detail': 'Matching is finished, you can no longer view your top three' }, status=status.HTTP_403_FORBIDDEN) topThree = p.getTopThree() ps = ParticipantSerializer(topThree, many=True) return JSONResponse(ps.data, status=status.HTTP_200_OK) except Participant.DoesNotExist: return JSONResponse( {'detail': 'Participant not found with that ID'}, status=status.HTTP_404_NOT_FOUND)
def register(self, request, **kwargs): c = Cohort.objects.get(cohortId=self.kwargs['cohortId']) s = ParticipantSerializer(data=request.data) if not s.is_valid(): return JSONResponse({'detail': s.errors}, status=status.HTTP_400_BAD_REQUEST) try: s.save(user=request.user, cohort=c) except IntegrityError: return JSONResponse( {'detail': 'You have already applied for this cohort.'}, status=status.HTTP_403_FORBIDDEN) except ValidationError as e: return JSONResponse({'detail': e.args[0]}, status=status.HTTP_403_FORBIDDEN) return JSONResponse(s.data, status=status.HTTP_200_OK)
def get_registration(self, request, **kwargs): c = Cohort.objects.get(cohortId=self.kwargs['cohortId']) try: participant = Participant.objects.get(cohort=c, user=self.request.user) except Participant.DoesNotExist: return JSONResponse( {'detail': 'User not signed up for this cohort'}, status=status.HTTP_404_NOT_FOUND) s = ParticipantSerializer(participant) return JSONResponse(s.data)
def test_serializer_cant_apply_for_full_cohort(self): smol_programme = Programme.objects.create( name='Smaller Programme', description='This is a test programme.', defaultCohortSize=1, createdBy=self.user) smol_cohort = Cohort.objects.create( programme=smol_programme, createdBy=self.user, cohortSize=smol_programme.defaultCohortSize) data = {'isMentor': True} serializer = ParticipantSerializer(data=data) serializer.is_valid() participant = serializer.save(user=self.user, cohort=smol_cohort) data = {'isMentor': False} serializer = ParticipantSerializer(data=data) serializer.is_valid() with self.assertRaises(ValidationError): participant = serializer.save(user=self.other_user, cohort=smol_cohort)
def test_serializer_is_mentor_not_set(self): data = {} serializer = ParticipantSerializer(data=data) self.assertFalse(serializer.is_valid())