def post(self, request, pk, format=None): try: igroup = InterestGroup.objects.get(pk=pk) serializer = ContentSerializer(igroup.contents, many=False, data=request.data, context={"request": request}) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) content = serializer.create(serializer.validated_data) igroup.contents.add(content) for tag in content.tags.all(): igroup.content_tags.add(tag) igroup.save() return Response( ContentSerializer(content, context={ 'request': request }).data) except Exception as e: return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
def list(self, request, *args, **kwargs): context = {'request': request} queryset = self.get_queryset() queryset = self.filter_queryset(queryset) page = self.paginate_queryset(queryset) if page is not None: serializer = ContentSerializer(page, many=True, context=context) return self.get_paginated_response(serializer.data) else: serializer = ContentSerializer(queryset, many=True, context=context) return Response(serializer.data)
def get(request): #posts = Post.objects.prefetch_related(Prefetch('post_content_reverse')).post_content_reverse.all() posts = Post.objects.prefetch_related( Prefetch('post_content_reverse')).all() print(posts) return Response( ContentSerializer(posts, many=True).data, status.HTTP_201_CREATED)
class TVSerializer(serializers.ModelSerializer): group = serializers.HyperlinkedRelatedField(view_name='group-detail', read_only=True) preview = ContentSerializer(read_only=True) class Meta: model = TV fields = ['id', 'name', 'group', 'preview']
def retrieve(self, request, pk=None, **kwargs): context = {'request': request} queryset = self.get_queryset() content = get_object_or_404(queryset, pk=pk) serializer = ContentSerializer(content, context=context) return Response(serializer.data)
def get(self, request, pk, format=None): try: igroup = InterestGroup.objects.get(pk=pk) print(igroup) serializer = ContentSerializer(igroup.contents.all(), many=True) return Response(serializer.data) except Exception as e: return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request): data = request.data serializers = PostSerializers(data=data, partial=True) if serializers.is_valid(): obj = serializers.save() data1 = data["content"] for d in data1: d["post"] = obj.id print(d) ser_content = ContentSerializer(d, partial=True) if ser_content.is_valid(): ser_content.save() else: return Response({"response": "Save Error"}, status=status.HTTP_400_BAD_REQUEST) return Response({"response": "Success"}, status=status.HTTP_201_CREATED) else: return Response({"response": "Serialize Error"}, status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): context = {'request': request} serializer = ContentSerializer(data=request.data, context=context) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def update(self, request, *args, **kwargs): context = {'request': request} instance = self.get_object() serializer = ContentSerializer(instance, data=request.data, partial=False, context=context) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
class LessonDetailSerializer(ModelSerializer): description = ContentSerializer() owner = UserDetailSerializer() course = SerializerMethodField() contents = SerializerMethodField() quiz = SerializerMethodField() class Meta: model = Lesson fields = "__all__" def get_course(self, obj): return obj.course.title def get_contents(self, obj): e = Contents.objects.filter(lesson=obj.pk, is_active=True).count() return e def get_quiz(self, obj): q = Quiz.objects.filter(lesson=obj.pk, is_active=True).first() if q: return q.pk return False
def post(self, request, *args, **kwargs): user = request.user data = request.data question = get_object_or_404(Question, pk=kwargs.get("pk")) content = {'text': data['text'], "sub_title": data.get("title")} if "file" in request.FILES: file = request.FILES['file'] content['file'] = file serializer = ContentSerializer(data=content) serializer.is_valid(raise_exception=True) serializer.save() choice = { "owner": user.pk, "description": serializer.instance.pk, "question": kwargs.get("pk"), "is_answer": data.get("is_answer") } serializer = ChoiceSerializer(data=choice) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK)
class CustomUserDetailSerializer(CustomUserSerializer): owner_projects = ContentSerializer(source="profile.owner_projects", many=True, read_only=True)
def post(self, request, *args, **kwargs): user = request.user data = request.data course = get_object_or_404(Course, pk=kwargs.get("pk")) content = {'text': data['text']} if "file" in request.FILES: file = request.FILES['file'] content['file'] = file serializer = ContentSerializer(data=content) serializer.is_valid(raise_exception=True) serializer.save() place = Lesson.objects.filter(course=course.id).count() + 1 lesson = { "title": data['title'], "owner": user.pk, "description": serializer.instance.pk, "place": place, "course": kwargs.get("pk"), } serializer = LessonSerializer(data=lesson) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK)
class CourseDetailSerializer(ModelSerializer): description = ContentSerializer() owner = UserDetailSerializer() enroll = SerializerMethodField() enrolled = SerializerMethodField() prerequisite = SerializerMethodField() prerequisite_fullfilled = SerializerMethodField() lessons = SerializerMethodField() quizzes = SerializerMethodField() number_of_visits = SerializerMethodField() tags = SerializerMethodField() class Meta: model = Course fields = "__all__" def get_enroll(self, obj): user = self.context.get('user') if user: if not user.is_anonymous: e = Enrollment.objects.filter(course=obj.pk, user=user.pk).first() if e: return e.pk return False def get_enrolled(self, obj): e = Enrollment.objects.filter(course=obj.pk).count() return e def get_prerequisite(self, obj): model = ContentType.objects.get(model="course") pre = Prerequisite.objects.filter(content_type=model.pk, object_id=obj.pk).first() if pre: model = pre.prerequisite_content_type Model = apps.get_model(app_label=model.app_label, model_name=model.model) Instance = Model.objects.filter( pk=pre.prerequisite_object_id).first() return PrerequisiteSerializer(pre).data return None def get_prerequisite_fullfilled(self, obj): user = self.context.get('user') if user: if not user.is_anonymous: model = ContentType.objects.get(model="course") pre = Prerequisite.objects.filter(content_type=model.pk, object_id=obj.pk).first() if pre: model = pre.prerequisite_content_type Model = apps.get_model(app_label=model.app_label, model_name=model.model) Instance = Model.objects.filter( pk=pre.prerequisite_object_id).first() ff = Fullfilled.objects.filter( content_type=model.pk, object_id=pre.prerequisite_object_id, user=user.pk) if not ff: return False return True def get_lessons(self, obj): e = Lesson.objects.filter(course=obj.pk, is_active=True).count() return e def get_quizzes(self, obj): e = Quiz.objects.filter(lesson__course=obj.pk, is_active=True).count() return e def get_number_of_visits(self, obj): obj.number_of_visits += 1 obj.save() return obj.number_of_visits def get_tags(self, obj): model = ContentType.objects.get(model="course") tags = EntityTag.objects.filter(content_type=model.pk, object_id=obj.pk) return EntityTagDetailSerializer(tags, many=True).data
def post(self, request, *args, **kwargs): user = request.user data = request.data content = {'text': data['text']} if "file" in request.FILES: file = request.FILES['file'] content['file'] = file serializer = ContentSerializer(data=content) serializer.is_valid(raise_exception=True) serializer.save() course = { "title": data['title'], "owner": user.pk, "description": serializer.instance.pk } serializer = CourseSerializer(data=course) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK)
def put(self, request, *args, **kwargs): user = request.user data = request.data pk = kwargs['pk'] course = get_object_or_404(Course, pk=pk) if not course.is_authorized(request.user): return Response(status=401) content = course.description content_data = { 'text': data['text'], "last_edited_at": datetime.datetime.now() } if "file" in request.FILES: file = request.FILES['file'] content_data['file'] = file serializer = ContentSerializer(content, data=content_data) serializer.is_valid(raise_exception=True) serializer.save() course_data = { "title": data['title'], "owner": course.owner.pk, "description": course.description.pk, "last_edited_at": datetime.datetime.now() } serializer = CourseSerializer(course, data=course_data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK)
def get(self, request, pk, format=None): user = User.objects.get(pk=pk) serializer = ContentSerializer(user.content_owner.all(), many=True) return Response(serializer.data)