Exemplo n.º 1
0
    def get(self, request, id):
        try:
            article = Article.objects.filter(id=id)
            if id == "":
                raise FieldError("id input not valid")
            if not article:
                raise EmptyResultSet("no result for article id = " + str(id))
            else:
                article = Article.objects.get(id=id)

            return Response(
                {
                    "status": status.HTTP_200_OK,
                    "message": "success",
                    "data": {
                        "id": article.id,
                        "title": article.title,
                        "content": article.content
                    }
                },
                status=status.HTTP_200_OK)

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)
Exemplo n.º 2
0
    def get(self, request, id):
        try:
            forum = Forum.objects.filter(id=id)
            if not id:
                raise FieldError("id input not valid")
            if not forum:
                raise EmptyResultSet("no result for forum id = " + str(id))
            else:
                forum = Forum.objects.get(id=id)

            answer = []
            for i in forum.answer.all():
                isUpvote = False
                if Employee.objects.get(user=request.user) in i.upvote.all():
                    isUpvote = True
                data = {
                    "id": i.id,
                    "username": i.owner.user.username,
                    "answer": i.answer,
                    "upvote": i.upvote.count(),
                    "is_upvote": isUpvote,
                    "created_at": i.createdAt
                }
                answer.append(data)
            isUpvote = False
            if Employee.objects.get(user=request.user) in forum.upvote.all():
                isUpvote = True

            return Response({
                "status": status.HTTP_200_OK,
                "message": "success",
                "data": {
                    "id": forum.id,
                    "username": forum.owner.user.username,
                    "title": forum.title,
                    "question": forum.question,
                    "upvote": forum.upvote.count(),
                    "answer": answer,
                    "is_upvote": isUpvote,
                    "created_at": forum.createdAt
                }
            })

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)
Exemplo n.º 3
0
    def post(self, request, id):
        try:
            forum = Forum.objects.get(id=id)
            if not id:
                raise FieldError("id input not valid")
            if forum == []:
                raise EmptyResultSet("no result for forum id = ", id)
            if request.data['is_increase'] == "":
                raise FieldError("answer is not valid")

            employee = Employee.objects.get(user=request.user)
            if request.data['is_increase'] == "true":
                if employee in forum.upvote.all():
                    raise Exception("Already upvote")
                else:
                    forum.upvote.add(employee)
            else:
                forum.upvote.remove(employee)

            isUpvote = False
            if Employee.objects.get(user=request.user) in forum.upvote.all():
                isUpvote = True

            return Response({
                "status": status.HTTP_200_OK,
                "message": "success",
                "data": {
                    "id": forum.id,
                    "username": forum.owner.user.username,
                    "title": forum.title,
                    "question": forum.question,
                    "upvote": forum.upvote.count(),
                    "is_upvote": isUpvote,
                    "created_at": forum.createdAt
                }
            })

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)
Exemplo n.º 4
0
    def post(self, request, id):
        try:
            forum = Forum.objects.get(id=id)
            if not id:
                raise FieldError("id input not valid")
            if forum == []:
                raise EmptyResultSet("no result for forum id = ", id)
            if request.data['answer'] == "":
                raise FieldError("answer is not valid")

            owner = Employee.objects.get(user=request.user)
            answer = Answer.objects.create(answer=request.data['answer'],
                                           owner=owner)
            forum.answer.add(answer)

            isUpvote = False
            if Employee.objects.get(user=request.user) in answer.upvote.all():
                isUpvote = True

            return Response({
                "status": status.HTTP_201_CREATED,
                "message": "objects created",
                "data": {
                    "id": answer.id,
                    "username": answer.owner.user.username,
                    "upvote": answer.upvote.count(),
                    "is_upvote": isUpvote,
                    "created_at": answer.createdAt
                }
            })

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)
Exemplo n.º 5
0
    def get(self, request):
        try:
            employee = Employee.objects.filter(user=request.user)
            search = request.GET.get("search")
            if not employee:
                raise EmptyResultSet("Employee not found")
            else:
                employee = employee[0]

            myCourse = CourseOwned.objects.all()
            myCourseResponse = []

            if search:
                topicID = []
                for i in Topic.objects.filter(topic__icontains=search):
                    topicID.append(i.id)

                courseID = []
                for i in Course.objects.filter(
                        Q(name__icontains=search) | Q(topic__in=topicID)):
                    courseID.append(i.id)

                myCourse = CourseOwned.objects.filter(
                    Q(owner=employee) & Q(course__in=courseID))

            else:
                myCourse = CourseOwned.objects.filter(owner=employee)
            lst = []
            for i in myCourse:
                data = {}
                if i.lastLesson:
                    data = {
                        "id":
                        i.course.id,
                        "name":
                        i.course.name,
                        "topic": [j.topic for j in i.course.topic.all()],
                        "last_progress": {
                            "id": i.lastLesson.id,
                            "title": i.lastLesson.title,
                            "type": "lesson",
                            "step": {
                                "id": i.lastStep.id,
                                "title": i.lastStep.title
                            }
                        },
                        "progress":
                        i.totalComplete / i.course.totalStepAndQuiz
                        if i.course.totalStepAndQuiz != 0 else 0.0,
                        "img":
                        i.course.img.url
                    }
                elif i.lastQuiz:
                    data = {
                        "id":
                        i.course.id,
                        "name":
                        i.course.name,
                        "topic": [j.topic for j in i.course.topic.all()],
                        "last_progress": {
                            "id": i.lastQuiz.id,
                            "title": i.lastQuiz.title,
                            "type": "quiz"
                        },
                        "progress":
                        i.totalComplete / i.course.totalStepAndQuiz
                        if i.course.totalStepAndQuiz != 0 else 0.0,
                        "img":
                        i.course.img.url
                    }
                else:
                    data = {
                        "id":
                        i.course.id,
                        "name":
                        i.course.name,
                        "topic": [j.topic for j in i.course.topic.all()],
                        "last_progress": {},
                        "progress":
                        i.totalComplete / i.course.totalStepAndQuiz
                        if i.course.totalStepAndQuiz != 0 else 0.0,
                        "img":
                        i.course.img.url
                    }
                lst.append(data)
            myCourseResponse = {"list": lst, "count": len(lst)}

            courseResponse = {}
            course = []
            userCourse = CourseOwned.objects.filter(owner=employee)
            userCourseList = []
            for i in userCourse:
                userCourseList.append(i.course)
            if not search:
                if not userCourse:
                    allCourse = Course.objects.all()
                    for i in allCourse:
                        course.append({
                            "id": i.id,
                            "name": i.name,
                            "description": i.description,
                            "topic": [j.topic for j in i.topic.all()],
                            "img": i.img.url,
                            "price": i.price,
                            "reward": i.reward
                        })

                else:
                    topicID = []
                    for i in userCourse:
                        for j in i.course.topic.all():
                            topicID.append(j.id)

                    filteredCourse = Course.objects.filter(topic__in=topicID, )
                    for i in filteredCourse:
                        if i not in userCourseList:
                            course.append({
                                "id":
                                i.id,
                                "name":
                                i.name,
                                "description":
                                i.description,
                                "topic": [j.topic for j in i.topic.all()],
                                "img":
                                i.img.url,
                                "price":
                                i.price,
                                "reward":
                                i.reward
                            })

            else:
                topicID = []
                for i in Topic.objects.filter(topic__icontains=search):
                    topicID.append(i.id)
                filteredCourse = Course.objects.filter(
                    Q(name__icontains=search)
                    | Q(topic__in=topicID)).distinct()
                for i in filteredCourse:
                    if i not in userCourseList:
                        course.append({
                            "id": i.id,
                            "name": i.name,
                            "description": i.description,
                            "topic": [j.topic for j in i.topic.all()],
                            "img": i.img.url,
                            "price": i.price,
                            "reward": i.reward
                        })

            courseResponse = {"list": course, "count": len(course)}
            response = {
                "status": status.HTTP_200_OK,
                "message": "success",
                "data": {
                    "my_course": myCourseResponse,
                    "course": courseResponse
                }
            }

            return Response(response, status=status.HTTP_200_OK)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)
Exemplo n.º 6
0
    def get(self, request):
        try:

            search = request.GET.get("search")

            article = ArticleSection.objects.all()
            if not search:
                category = request.GET.get("category")
                if not category:
                    raise FieldError("category or search input not valid")
                categoryObject = Topic.objects.get(topic=category)
                if not categoryObject:
                    raise EmptyResultSet

                article = ArticleSection.objects.filter(topic=categoryObject)

                articleSection = []
                for i in article:
                    article = []
                    for j in i.section.all():
                        article.append({
                            "id": j.id,
                            "title": j.title,
                            "content": j.content
                        })
                    articleSection.append({
                        "id": i.id,
                        "title": i.title,
                        "article": article
                    })

                return Response(
                    {
                        "status": status.HTTP_200_OK,
                        "message": "success",
                        "data": {
                            'section': articleSection,
                            'count': len(articleSection),
                        }
                    },
                    status=status.HTTP_200_OK)

            else:
                article = Article.objects.filter(title__icontains=search)
                articles = []
                for i in article:
                    articles.append({
                        'id': i.id,
                        'title': i.title,
                        'content': i.content
                    })

                return Response(
                    {
                        "status": status.HTTP_200_OK,
                        "message": "success",
                        "data": {
                            "articles": articles,
                            'count': len(article)
                        }
                    },
                    status=status.HTTP_200_OK)

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)
Exemplo n.º 7
0
    def post(self, request):
        try:
            if request.data['title'] == "":
                raise FieldError("title is not valid")
            if request.data['question'] == "":
                raise FieldError("question is not valid")
            if request.data['topic'] == "":
                raise FieldError("topic is not valid")

            owner = Employee.objects.get(user=request.user)
            forum = Forum.objects.create(title=request.data['title'],
                                         question=request.data['question'],
                                         owner=owner)

            for i in request.data['topic']:
                topic = Topic.objects.filter(topic=i)
                if topic.count() == 0:
                    topic = Topic.objects.create(topic=i)
                else:
                    topic = topic[0]

                forum.topic.add(topic)

            answer = []
            for i in forum.answer.all():
                isUpvote = False
                if Employee.objects.get(user=request.user) in i.upvote.all():
                    isUpvote = True
                data = {
                    "id": i.id,
                    "username": i.owner.user.username,
                    "answer": i.answer,
                    "upvote": i.upvote.count(),
                    "is_upvote": isUpvote
                }
                answer.append(data)

            isUpvote = False
            if Employee.objects.get(user=request.user) in forum.upvote.all():
                isUpvote = True

            return Response({
                "status": status.HTTP_201_CREATED,
                "message": "object created",
                "data": {
                    "id": forum.id,
                    "username": forum.owner.user.username,
                    "title": forum.title,
                    "question": forum.question,
                    "upvote": forum.upvote.count(),
                    "answer": answer,
                    "is_upvote": isUpvote,
                    "created_at": forum.createdAt
                }
            })

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)
Exemplo n.º 8
0
    def get(self, request):
        try:
            category = request.GET.get("category")
            sortInput = request.GET.get("sort")
            search = request.GET.get("search")

            forum = Forum.objects.all()

            # Validate search
            if not search:
                # Validate category and sort
                if not category:
                    raise FieldError("category or search input not valid")
                if not sortInput:
                    raise FieldError("sort or search input not valid")

                # Get topic object based on category
                categoryObject = Topic.objects.get(topic=category)
                if not categoryObject:
                    raise EmptyResultSet
                # Get forum based on category and sort input
                if sortInput == "date":
                    forum = Forum.objects.filter(
                        topic__in=[categoryObject.id]).order_by("-createdAt")
                elif sortInput == "vote":
                    forum = Forum.objects.filter(
                        topic__in=[categoryObject.id]).annotate(
                            num_vote=Count('upvote')).order_by('-num_vote')
                elif sortInput == "answer":
                    forum = Forum.objects.filter(
                        topic__in=[categoryObject.id]).annotate(
                            num_answer=Count('answer')).order_by('-num_answer')
                else:
                    raise FieldError("sort input not valid")

            else:  # if sort exist
                forum = Forum.objects.filter(title__icontains=search)

            # Create list of forum
            forumList = []
            for i in forum:

                # Check is upvote
                isUpvote = False
                if Employee.objects.get(user=request.user) in i.upvote.all():
                    isUpvote = True

                # Append
                forumList.append({
                    "id": i.id,
                    "username": i.owner.user.username,
                    "title": i.title,
                    "question": i.question,
                    "upvote": i.upvote.count(),
                    "total_answer": i.answer.count(),
                    "created_at": i.createdAt,
                    "is_upvote": isUpvote
                })

            # Response
            resp = {
                "status": status.HTTP_200_OK,
                "message": "success",
                "data": {
                    "forum": forumList,
                    "count": forum.count()
                }
            }

            return Response(resp, status=status.HTTP_200_OK)

        except FieldError as e:
            return FieldErrorHandler(e)

        except EmptyResultSet as e:
            return EmptyResultSetHandler(e)

        except Exception as e:
            return ExceptionHandler(e)