def upcoming_homework(self, request): try: with transaction.atomic(): def get_date(homework): return homework.get('hw').get('date_delivery') _student = request.user.profile.student _my_assignments = _student.assignstudent_student.filter(activo=True) _homework = [] for i in _my_assignments: _homework_temp = i.assignment.homework_assignment.filter(activo=True) for h in _homework_temp: homework_serializer = HomeworkSerializer(h) data = { 'assignmentId': i.assignment.id, 'hw': homework_serializer.data } _homework.append(data) _homework.sort(key=get_date) return Response({"homework": _homework[:7]}, status=status.HTTP_200_OK) except User.DoesNotExist: return Response({"detail": "User not found"}, status=status.HTTP_404_NOT_FOUND) except KeyError as e: return Response({"detail": "{} is a required field".format(str(e))}, status=status.HTTP_400_BAD_REQUEST)
def detail_homework(self, request): data = request.data user = request.user print('DATA detail_homework CREATE:', data) try: with transaction.atomic(): _assign_id = data.get("assignment") _id = data.get("id") if _id is not None and _assign_id is not None: _homework = Homework.objects.get(id=_id, assignment=_assign_id, activo=True) send_data = HomeworkSerializer(_homework) _profile = Profile.objects.get(user=user.id, activo=True) _student = Student.objects.get(profile=_profile.id, activo=True) count_deliver = len(HomeworkStudent.objects.filter(homework=_id, student=_student.id, activo=True)) res_homework = None if(count_deliver > 0): res_homework = HomeworkStudent.objects.get(homework=_id, student=_student.id, activo=True) send_res_homework = HomeworkStudentSerializer(res_homework) res_homework = send_res_homework.data return Response({"homework": send_data.data, "count": count_deliver, "res_homework":res_homework}, status=status.HTTP_200_OK) else: return Response({"detail": "You didnt send all the necessary data"}, status=status.HTTP_400_BAD_REQUEST) return Response({"detail": "Somenthing was wrong, its no possible to create professor"}, status=status.HTTP_400_BAD_REQUEST) except User.DoesNotExist: return Response({"detail": "User not found"}, status=status.HTTP_404_NOT_FOUND) except KeyError as e: return Response({"detail": "{} is a required field".format(str(e))}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): student_group_id = get_profile_from_user( request.user.id).study_group_id homework = Homeworks.objects.filter( target_group=student_group_id, deadline_time__gte=datetime.datetime.now()) if homework: json_data = HomeworkSerializer(homework, many=True) else: return Response(None) return Response(json_data.data)
def create_homework(self, request): data = request.data print('DATA create_homework CREATE:', data) try: with transaction.atomic(): homework_register = HomeworkRegisterSerializer(data) if homework_register.is_valid: _attached = data.get('attached') _date_delivery = data.get('date_delivery') _description = data.get('description') _myvalue = data.get('myvalue') _title = data.get('title') _assign_id = data.get("assignment") _assignment = Assignment.objects.get(id=_assign_id, activo=True) _homework = Homework.objects.create( attached=_attached, date_delivery=_date_delivery, description=_description, myvalue=_myvalue, title=_title, assignment=_assignment ) _homework.save() _homework_all = Homework.objects.filter(assignment=_assign_id, activo=True) send_data = HomeworkSerializer(_homework_all, many=True) return Response({"homework": send_data.data}, status=status.HTTP_200_OK) else: return Response({"detail": "You didnt send all the necessary data"}, status=status.HTTP_400_BAD_REQUEST) return Response({"detail": "Somenthing was wrong, its no possible to create professor"}, status=status.HTTP_400_BAD_REQUEST) except User.DoesNotExist: return Response({"detail": "User not found"}, status=status.HTTP_404_NOT_FOUND) except KeyError as e: return Response({"detail": "{} is a required field".format(str(e))}, status=status.HTTP_400_BAD_REQUEST)
def current_homework(self, request): data = request.data user = request.user print('DATA HOMEWORK CREATE:', data) try: with transaction.atomic(): _id = data.get("id") print("ID of current_homework", _id) _homework_all = Homework.objects.filter( assignment=_id, activo=True ) send_data = HomeworkSerializer(_homework_all, many=True) return Response({"homework": send_data.data}, status=status.HTTP_200_OK) except User.DoesNotExist: return Response({"detail": "User not found"}, status=status.HTTP_404_NOT_FOUND) except KeyError as e: return Response({"detail": "{} is a required field".format(str(e))}, status=status.HTTP_400_BAD_REQUEST)
def delete_homework(self, request): data = request.data try: with transaction.atomic(): _assign_id = data.get("assignment") _id = data.get("id") print("ID of delete_homework, homework, assign", _id, _assign_id) if _assign_id is not None and _id is not None: _homework = Homework.objects.get(id=_id, assignment=_assign_id, activo=True) _homework.delete() _homework_all = Homework.objects.filter(assignment=_assign_id, activo=True) send_data = HomeworkSerializer(_homework_all, many=True) return Response({"homework": send_data.data}, status=status.HTTP_200_OK) else: return Response({"detail": "You didnt send all the necessary data"}, status=status.HTTP_400_BAD_REQUEST) except User.DoesNotExist: return Response({"detail": "User not found"}, status=status.HTTP_404_NOT_FOUND) except KeyError as e: return Response({"detail": "{} is a required field".format(str(e))}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, **kwargs): homework = Homeworks.objects.get(id=kwargs.get('homework_id')) json_data = HomeworkSerializer(homework, many=False) return Response(json_data.data)