def test_manual_create_credit_distribution(self): url = '/api/manage/credit/manual_create/' data = { 'society_id_set': [401, 301], 'year': SettingsService.get('year'), 'semester': SettingsService.get('semester'), 'credit': 20 } client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.user3) response = client.post(url, data=json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 201) cd_set = CreditDistribution.objects.filter( year=SettingsService.get('year'), semester=SettingsService.get('semester') ) self.assertEqual(cd_set[0].society, self.society1) self.assertEqual(cd_set[1].society, self.society2) self.assertEqual(cd_set[0].credit, 20) # test create again # the response status code should be 400 # because of the unique together validator response = client.post(url, data=data, decode=True) self.assertEqual(response.status_code, 400)
def test_update(self): society1_cd = CreditDistribution.objects.create( society=self.society1, year=SettingsService.get('year'), semester=SettingsService.get('semester')) self.society1.members.add(self.student1) client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.society_user1) url = '/api/society_manage/credit/{}/'.format(society1_cd.id) data = {'receivers': [self.student1.id]} res = client.patch(url, data=data, encode=True) self.assertEqual(res.status_code, 200) society1_cd.refresh_from_db() self.assertEqual(society1_cd.receivers_count, 1) self.assertEqual(society1_cd.receivers.first(), self.student1) data = {'receiver': []} res = client.patch(url, data=data, encode=True) self.assertEqual(res.status_code, 400) society1_cd.open = False society1_cd.save() data = {'receivers': [self.student1.id]} res = client.patch(url, data=data, encode=True) self.assertEqual(res.status_code, 406)
def test_retrieve(self): url = '/api/manage/settings/' res = self.client.get(url, encode=True) self.assertEqual(res.status_code, 200) self.assertEqual(res.data['year'], SettingsService.get('year')) self.assertEqual(res.data['semester'], SettingsService.get('semester'))
def test_archive_society(self): credit = CreditDistribution.objects.create( society=self.society3, year=SettingsService.get('year'), semester=SettingsService.get('semester'), credit=10, open=True ) student_user = self.createUser('student') student = self.createStudent(user=student_user) credit.receivers.add(student) self.society3.members.add(student) url = '/api/manage/society/{}/archive/'.format(self.society1.pk) client = APIClient(enforce_csrf_checks=True) response = client.post(url, decode=True) self.assertEqual(response.status_code, 403) # test with a waiting society url = '/api/manage/society/{}/archive/'.format(self.society1.pk) client.force_authenticate(self.user4) response = client.post(url, decode=True) self.assertEqual(response.status_code, 403) url = '/api/manage/society/{}/archive/'.format(self.society3.pk) response = client.post(url, decode=True) self.society3.refresh_from_db() self.assertEqual(response.status_code, 202) self.assertIsNone(self.society3.society_id) self.assertEqual(self.society3.status, SocietyStatus.ARCHIVED) self.assertEqual(self.society3.user.is_active, False) self.assertNotIn(student, self.society3.members.all()) self.assertNotIn(student, credit.receivers.all())
def update(self, request, *args, **kwargs): serializer = self.get_serializer_class()(data=request.data) if serializer.is_valid(): SettingsService.set('year', serializer.validated_data['year']) SettingsService.set('semester', serializer.validated_data['semester']) return Response(status=status.HTTP_202_ACCEPTED) return Response(status=status.HTTP_400_BAD_REQUEST)
def get_available_receivers(self): available_receivers = [] members = self.society.members.all() year = SettingsService.get('year') semester = SettingsService.get('semester') for member in members: has_receiver_credit = member.has_receive_credit(year, semester) if has_receiver_credit == self.society.id or has_receiver_credit is None: available_receivers.append(member) return available_receivers
def list(self, request, *args, **kwargs): year = request.query_params.get('year', SettingsService.get('year')) semester = request.query_params.get('semester', SettingsService.get('semester')) self.queryset = self.get_queryset().filter(year=year).filter( semester=semester) instance = self.queryset.first() if instance: serializer = self.get_serializer(instance) return response.Response(serializer.data) return response.Response(status=status.HTTP_404_NOT_FOUND)
def setUp(self): user1 = User.objects.create_user(username='******') self.student = Student.objects.create(user=user1, name='sms', grade=1, class_num=1, qq='123456789') user2 = User.objects.create_user(username='******') self.society1 = Society.objects.create(society_id=101, user=user2, name='Jeek', status=SocietyStatus.ACTIVE, recruit=True, president_grade=1, president_class=1, type=1) user3 = User.objects.create_user(username='******') self.society2 = Society.objects.create(society_id=102, user=user3, name='Jeek2', status=SocietyStatus.ACTIVE, recruit=True, president_grade=1, president_class=1, type=1) self.site_settings = SettingsService.get_dict() self.credit1 = CreditDistribution.objects.create( society=self.society1, year=self.site_settings['year'], semester=self.site_settings['semester'], open=True, ) self.credit1.receivers.set([self.student])
def test_bulk_create(self): url = '/api/manage/credit/bulk_create/' data = { 'year': SettingsService.get('year'), 'semester': 2 } client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.user3) res = client.post(url, data=data, decode=True) self.assertEqual(res.status_code, 201) CreditDistribution.objects.first().delete() res = client.post(url, data=data, decode=True) self.assertEqual(res.status_code, 201) self.assertEqual(CreditDistribution.objects.count(), 2) data = { 'yea': 1110, 'semester': 2 } client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.user3) res = client.post(url, data=data, decode=True) self.assertEqual(res.status_code, 400)
def quit(self, request, pk=None): society = self.get_object() member = request.user.student year = SettingsService.get('year') semester = SettingsService.get('semester') credit = CreditDistribution.objects.filter(society=society, year=year, semester=semester, open=True) society.members.remove(member) if credit.exists() and society.status == SocietyStatus.ACTIVE: credit.first().receivers.remove(member) return Response(data={'detail': '退出成功!'}, status=status.HTTP_200_OK)
def archive(self, request, pk=None): society = self.get_object() society.society_id = None society.status = SocietyStatus.ARCHIVED society.members.clear() society.user.is_active = False society.user.save() society.save() credit_distribution = CreditDistribution.objects.filter( society=society, year=SettingsService.get('year'), semester=SettingsService.get('semester'), open=True) if credit_distribution.exists(): credit_distribution.first().receivers.clear() return Response(status=status.HTTP_202_ACCEPTED)
def test_list_credit_distributions(self): url = '/api/manage/credit/?year={year}&semester={semester}'.format( year=SettingsService.get('year'), semester=SettingsService.get('semester') ) # test permissions client = APIClient(enforce_csrf_checks=True) response = client.get(url, decode=True) self.assertEqual(response.status_code, 403) client.force_authenticate(self.user3) res = client.get(url, decode=True) self.assertEqual(res.status_code, 200) self.assertEqual(res.data['count'], 0) CreditDistribution.objects.create( society=self.society1, year=SettingsService.get('year'), semester=SettingsService.get('semester') ) CreditDistribution.objects.create( society=self.society2, year=SettingsService.get('year'), semester=SettingsService.get('semester') ) res = client.get(url, decode=True) self.assertEqual(res.status_code, 200) self.assertEqual(res.data['count'], 2)
def test_bulk_close(self): CreditDistribution.objects.create( society=self.society1, year=SettingsService.get('year'), semester=SettingsService.get('semester') ) CreditDistribution.objects.create( society=self.society2, year=SettingsService.get('year'), semester=SettingsService.get('semester') ) url = '/api/manage/credit/bulk_close/' data = { 'year': SettingsService.get('year'), 'semester': SettingsService.get('semester') } client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.user3) res = client.post(url, data=data, decode=True) self.assertEqual(res.status_code, 200) self.assertEqual(CreditDistribution.objects.filter( year=SettingsService.get('year'), semester=SettingsService.get('semester') ).first().open, False) data = { 'year': 1110, 'semester': 2 } client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.user3) res = client.post(url, data=data, decode=True) self.assertEqual(res.status_code, 404) data = { 'yea': 1110, 'semestr': 2 } client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.user3) res = client.post(url, data=data, decode=True) self.assertEqual(res.status_code, 400)
def test_retrieve(self): url = '/api/society_manage/credit/' society1_cd = CreditDistribution.objects.create( society=self.society1, year=SettingsService.get('year'), semester=SettingsService.get('semester')) self.society1.members.set([self.student1, self.student2]) client = APIClient(enforce_csrf_checks=True) client.force_authenticate(self.society_user1) params = { 'year': SettingsService.get('year'), 'semester': SettingsService.get('semester') } res = client.get(url, data=params, encode=True) self.assertEqual(res.status_code, 200) self.assertEqual(res.data['year'], timezone.datetime.now().year) self.assertEqual(res.data['semester'], 1) self.assertEqual(len(res.data['available_receivers']), 2) self.assertEqual(res.data['available_receivers'][0]['name'], self.student1.name) self.assertEqual(res.data['open'], True) society1_cd.receivers.add(self.student1) society1_cd.refresh_from_db() res = client.get(url, data=params, encode=True) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.data['available_receivers']), 2) self.assertEqual(len(res.data['receivers']), 1) society2_cd = CreditDistribution.objects.create( society=self.society2, year=SettingsService.get('year'), semester=SettingsService.get('semester')) self.society2.members.set([self.student1, self.student2]) client.force_authenticate(self.society_user2) res = client.get(url, data=params, encode=True) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.data['available_receivers']), 1) # test 404 params = {'year': 1111, 'semester': 11} res = client.get(url, data=params, encode=True) self.assertEqual(res.status_code, 404)
def get_semester(self, obj): return SettingsService.get('semester')
def get_year(self, obj): return SettingsService.get('year')