class CompanySerializer(serializers.ModelSerializer): departments = DepartmentsSerializer(source="department_set", many=True, read_only=True) class Meta: model = Company fields = ('__all__')
def update(self, request, *args, **kwargs): department_data = Departments.objects.get(id=int(request.data.get('id'))) department_data.name = request.data['name'] department_data.questions.set(request.data['questions']) department_data.save() serializer = DepartmentsSerializer(department_data) log.info('Department {} was updated by {}'.format(serializer.data['name'], request.user.username)) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_get_valid_department_no_login_user(self): """Test for GET Department with id '2' with unlogin user and get error """ serializer = DepartmentsSerializer( Departments.objects.get(name='depart1')) response = self.client.get('/api/departments/' + str(serializer.data['id'])) # Check status of response self.assertEqual(response.status_code, status.HTTP_302_FOUND)
def create(self, request, *args, **kwargs): try: recruiter = UserDataSerializer(UserData.objects.get(f_auth_id=self.request.user.id)) candidate = UserDataSerializer(UserData.objects.get(f_auth_id=request.data['candidate'])) expert = UserDataSerializer(UserData.objects.get(f_auth_id=request.data['expert'])) department = DepartmentsSerializer(Departments.objects .get(id=request.data['department'])) department.to_representation = department.original_representation if recruiter.data['auth']['id'] == candidate.data['auth']['id'] or \ recruiter.data['auth']['id'] == expert.data['auth']['id'] or \ candidate.data['auth']['id'] == expert.data['auth']['id']: return Response('Users can not be the same!', status=status.HTTP_400_BAD_REQUEST) if recruiter.data['role']['name'] != 'recruiter': log.error('User {} with role {} tries to create interview' .format(request.user.username, recruiter.data['role']['name'])) return Response('You aren\'t recruiter!', status=status.HTTP_400_BAD_REQUEST) except UserData.DoesNotExist as error: log.error(error) return Response('User does not exist!', status=status.HTTP_400_BAD_REQUEST) except Departments.DoesNotExist as error: log.error(error) return Response('Department does not exist!', status=status.HTTP_400_BAD_REQUEST) request.data['recruiter'] = recruiter.data request.data['candidate'] = candidate.data request.data['expert'] = expert.data request.data['department'] = department.data request.data['created_date'] = datetime.date.today() request.data['status'] = 'scheduled' serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) log.info(self.perform_create(serializer)) return Response(serializer.data, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): """Get name from 'departments', serialize it and get full JSON""" list_questions = [] for i in request.data['questions']: try: ser = QuestionsSerializer(Questions.objects.get(id=i)) list_questions.append(ser.data) except Questions.DoesNotExist: log.error('Question with id={} doesn\'t exist'.format(i)) return Response('question does not exist', status=status.HTTP_400_BAD_REQUEST) request.data['questions'] = list_questions serializer = DepartmentsSerializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) log.info('Department {} was created by {}'.format(serializer.data.get('name'), request.user.username)) log.debug('Department was created with {} questions'.format(len(list_questions))) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def test_get_valid_department(self): """Test for GET Department with id '2' """ # Note: PostGre server not working properly with setup # and give to new instance id=2 self.client.force_login( User.objects.get_or_create(username='******')[0]) serializer = DepartmentsSerializer( Departments.objects.get(name='depart1')) response = self.client.get('/api/departments/' + str(serializer.data['id'])) # Check status of response self.assertEqual(response.status_code, status.HTTP_200_OK) # Check body of response self.assertEqual(serializer.data, response.data)
class InterviewsSerializer(serializers.ModelSerializer): """InterviewsSerializer""" recruiter = UserDataSerializer(source='f_recruiter') candidate = UserDataSerializer(source='f_candidate') expert = UserDataSerializer(source='f_expert') department = DepartmentsSerializer(source='f_department') latitude = serializers.FloatField(allow_null=True) longitude = serializers.FloatField(allow_null=True) location = serializers.CharField(allow_null=True) class Meta: """Meta""" model = Interviews fields = ('id', 'recruiter', 'candidate', 'expert', 'department', 'created_date', 'status', 'interview_date', 'completion_date', 'latitude', 'longitude', 'location') extra_kwargs = { 'id': { 'read_only': False, 'required': False }, } def create(self, validated_data): validated_data['f_recruiter'] = dict(validated_data['f_recruiter']) validated_data['f_candidate'] = dict(validated_data['f_candidate']) validated_data['f_expert'] = dict(validated_data['f_expert']) validated_data['f_department'] = dict(validated_data['f_department']) recruiter = UserData.objects.get(f_auth=User.objects.get( username=validated_data.get('f_recruiter').get('f_auth').get( 'username'))) candidate = UserData.objects.get(f_auth=User.objects.get( username=validated_data.get('f_candidate').get('f_auth').get( 'username'))) expert = UserData.objects.get(f_auth=User.objects.get( username=validated_data.get('f_expert').get('f_auth').get( 'username'))) department = Departments.objects.get( name=validated_data.get('f_department').get('name')) instance = Interviews.objects.create( f_recruiter=recruiter, f_candidate=candidate, f_expert=expert, f_department=department, interview_date=validated_data.get('interview_date'), created_date=validated_data.get('created_date'), latitude=validated_data.get('latitude'), longitude=validated_data.get('longitude'), location=validated_data.get('location'), status=validated_data.get('status')) return instance def update(self, instance, validated_data): department = Departments.objects.get(name=validated_data.get('name')) instance.f_department = department candidate = validated_data['f_candidate']['f_auth']['username'] candidate = UserData.objects.get(f_auth=User.objects.get( username=validated_data.get('f_candidate').get('f_auth').get( 'username'))) instance.f_candidate = candidate expert = validated_data['f_expert']['f_auth']['username'] expert = UserData.objects.get(f_auth=User.objects.get( username=validated_data.get('f_expert').get('f_auth').get( 'username'))) instance.f_expert = expert instance.save() return instance def validate_unique_users(self): """validate_unique_users""" if self.f_recruiter.id is self.f_candidate.id or \ self.f_recruiter.id is self.f_expert.id or \ self.f_candidate.id is self.f_expert.id: raise serializers.ValidationError('Users can not be the same.')