class AnswersSerializer(serializers.ModelSerializer): # Serializer for Answers model grades = GradesSerializer(source='f_grade') interviews = InterviewsSerializer(source='f_interview') questions = QuestionsSerializer(source='f_question') class Meta: model = Answers fields = ('id', 'answer_like', 'grades', 'questions', 'interviews') def to_representation(self, instance): return {'answer_like': instance.answer_like, 'grades': instance.f_grade_id, 'questions': instance.f_question_id} def create(self, validated_data): validated_data['f_grade'] = dict(validated_data['f_grade']) grade = Grades.objects.get(name=validated_data.pop('f_grade').get('name')) validated_data['f_question'] = dict(validated_data['f_question']) question = Questions.objects.get(name=validated_data.pop('f_question').get('name')) validated_data['f_interview'] = dict(validated_data['f_interview']) interview = Interviews.objects.get(id=validated_data.get('f_interview').get('id')) instance = Answers.objects.create(answer_like=validated_data.get('answer_like'), f_grade=grade, f_question=question, f_interview=interview) return instance
class CommentsSerializer(serializers.ModelSerializer): # Serializer for Comments model # Get questions, grades, interviews data using QuestionsSerializer, GradesSerializer and InterviewsSerializer questions = QuestionsSerializer(source='f_question') grades = GradesSerializer(source='f_grade') interviews = InterviewsSerializer(source='f_interview') class Meta: model = Comments fields = ('id', 'questions', 'grades', 'comment', 'interviews') def to_representation(self, instance): return { 'validated_grade': instance.f_grade_id, 'comment': instance.comment, 'questions': instance.f_question_id } def create(self, validated_data): validated_data['f_question'] = dict(validated_data['f_question']) questions = Questions.objects.get( name=validated_data.pop('f_question').get('name')) validated_data['f_grade'] = dict(validated_data['f_grade']) grades = Grades.objects.get( name=validated_data.pop('f_grade').get('name')) validated_data['f_interview'] = dict(validated_data['f_interview']) interview = Interviews.objects.get( id=validated_data.pop('f_interview').get('id')) instance = Comments.objects.create( f_question=questions, f_grade=grades, comment=validated_data.get('comment'), f_interview=interview) return instance
def create(self, request, *args, **kwargs): try: grad = GradesSerializer( Grades.objects.get(id=request.data['grades'])) ques = QuestionsSerializer( Questions.objects.get(id=request.data['questions'])) inter = InterviewsSerializer( Interviews.objects.get(id=request.data['interviews'])) except Grades.DoesNotExist: return Response('Grades does not exist', status=status.HTTP_400_BAD_REQUEST) except Questions.DoesNotExist: return Response('Questions does not exist', status=status.HTTP_400_BAD_REQUEST) except Interviews.DoesNotExist: return Response('Interviews does not exist', status=status.HTTP_400_BAD_REQUEST) request.data['grades'] = grad.data request.data['questions'] = ques.data request.data['interviews'] = inter.data serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def get(self, request, *args, **kwargs): def data_generation(dict1, dict2, dict3): data = [{ 'label': 'somethingA', 'values': [{ 'x': key, 'y': dict1[key] } for key in dict1.keys()] }, { 'label': 'somethingB', 'values': [{ 'x': key, 'y': dict2[key] } for key in dict2.keys()] }, { 'label': 'somethingC', 'values': [{ 'x': key, 'y': dict3[key] } for key in dict3.keys()] }] return data queryset = self.get_queryset() serializer_interwiew = InterviewsSerializer(queryset, many=True) recruiters = UserData.objects.filter(f_role_id=2) data = {} for recruiter in recruiters: list_created_date = [] list_interview_date = [] list_completion_date = [] for item in serializer_interwiew.data: if item['recruiter']['auth']['id'] == recruiter.f_auth.id: if item['created_date'] != None: list_created_date.append(item['created_date']) if item['interview_date'] != None: list_interview_date.append(item['interview_date']) if item['completion_date'] != None: list_completion_date.append(item['completion_date']) now = datetime.datetime.now() list_created_date_7_days = {} list_interview_date_7_days = {} list_completion_date_7_days = {} for i in range(6, -1, -1): then = timedelta(days=i) list_created_date_7_days[(now - then).strftime("%d/%m/%Y")] = 0 list_interview_date_7_days[(now - then).strftime("%d/%m/%Y")] = 0 list_completion_date_7_days[(now - then).strftime("%d/%m/%Y")] = 0 for item in list_created_date: if (now - then).strftime("%Y-%m-%d") == item[0:10]: list_created_date_7_days[( now - then).strftime("%d/%m/%Y")] += 1 for item in list_interview_date: if (now - then).strftime("%Y-%m-%d") == item[0:10]: list_interview_date_7_days[( now - then).strftime("%d/%m/%Y")] += 1 for item in list_completion_date: if (now - then).strftime("%Y-%m-%d") == item[0:10]: list_completion_date_7_days[( now - then).strftime("%d/%m/%Y")] += 1 list_created_date_month = {} list_interview_date_month = {} list_completion_date_month = {} for i in range(4, -1, -1): then = timedelta(days=i * 7) list_then = [ (now - timedelta(days=i * 7 - j)).strftime("%Y-%m-%d") for j in range(7) ] str_date = ( now - timedelta(days=i * 7)).strftime("%d/%m/%Y") + "-" + ( now - timedelta(days=i * 7 - 6)).strftime("%d/%m/%Y") list_created_date_month[(now - then).strftime(str_date)] = 0 list_interview_date_month[(now - then).strftime(str_date)] = 0 list_completion_date_month[(now - then).strftime(str_date)] = 0 for item in list_created_date: if item[0:10] in list_then: list_created_date_month[(now - then).strftime(str_date)] += 1 for item in list_interview_date: if item[0:10] in list_then: list_interview_date_month[( now - then).strftime(str_date)] += 1 for item in list_completion_date: if item[0:10] in list_then: list_completion_date_month[( now - then).strftime(str_date)] += 1 list_created_date_year = {} list_interview_date_year = {} list_completion_date_year = {} for i in range(12): then = now for j in range(12 - i, 1, -1): then = (then.replace(day=1) - timedelta(days=1)) list_created_date_year[then.strftime("%m/%Y")] = 0 list_interview_date_year[then.strftime("%m/%Y")] = 0 list_completion_date_year[then.strftime("%m/%Y")] = 0 for item in list_created_date: if then.strftime("%Y-%m") == item[0:7]: list_created_date_year[then.strftime("%m/%Y")] += 1 for item in list_interview_date: if then.strftime("%Y-%m") == item[0:7]: list_interview_date_year[then.strftime("%m/%Y")] += 1 for item in list_completion_date: if then.strftime("%Y-%m") == item[0:7]: list_completion_date_year[then.strftime("%m/%Y")] += 1 data[recruiter.f_auth.username] = { 'day_data': data_generation(list_created_date_7_days, list_interview_date_7_days, list_completion_date_7_days), 'month_data': data_generation(list_created_date_month, list_interview_date_month, list_completion_date_month), 'year_data': data_generation(list_created_date_year, list_interview_date_year, list_completion_date_year) } return Response({ 'all_data': data, 'username': request.user.username }, status=200)