Ejemplo n.º 1
0
 def put(self, request, pk, format=None):
     article = self.get_object(pk)
     serializer = ArticleSerializer(article, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=400)
Ejemplo n.º 2
0
	def create(self,request):
		user = request.user
		serializer = ArticleSerializer(data=request.DATA,context={'request':request})
		if serializer.is_valid():
			serializer.save(user=user)
			return Response(serializer.data, status=status.HTTP_201_CREATED)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
def article_detail(request, pk):
    """
    Retrieve, update or delete an article.
    """
    try:
        article = Article.objects.get(pk=pk)
    except Article.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = ArticleSerializer(article)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ArticleSerializer(article, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)

        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        article.delete()
        return HttpResponse(status=204)
Ejemplo n.º 4
0
 def post(self, request):
     request.data.update({'requester': request.user.profile.id})
     serializer = ArticleSerializer(data=request.data)
     if serializer.is_valid(raise_exception=ValueError):
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.error_messages,
                     status=status.HTTP_404_BAD_REQUEST)
Ejemplo n.º 5
0
 def post(self, request):
     article = ArticleSerializer(data=request.data)
     if article.is_valid():
         article.save()
         return Response(data=article.data, status=status.HTTP_200_OK)
     else:
         return Response(data=article.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 6
0
    def post(self, request: HttpRequest, board_id: int):
        data = JSONParser().parse(request)
        data[self.lookup_board_kwarg] = board_id

        serializer = ArticleSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 7
0
def article_list(request):
    """
    List all articles, or create a new article.
    """

    if request.method == 'GET':
        articles = Article.objects.all()
        serializer = ArticleSerializer(articles, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = ArticleSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        return Response(serializer.errors, status=400)
Ejemplo n.º 8
0
def article_detail(request, pk):
    try:
        article = Article.objects.get(pk=pk)
    except Article.DoesNotExist:
        return Response(status=404)

    if request.method == 'GET':
        serializer = ArticleSerializer(article)
        return Response(serializer.data)

    if request.method == 'PUT':
        serializer = ArticleSerializer(article, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=400)

    elif request.method == 'DELETE':
        article.delete()
        return Response(status=204)
Ejemplo n.º 9
0
 def post(self, request):  # 에러 테스트 필요함
     if request.user.is_authenticated:  # 사용자가 인증 되었을경우
         serializer = ArticleSerializer(data=request.data)
         try:
             profile = Profile.objects.get(user=request.user)
         except:
             Profile.objects.create(user=request.user)
             profile = Profile.objects.get(user=request.user)
         for temp in json.loads(
                 request.data.get('images_id')):  # 이미지의 id값들이 유효한지 체크
             if not self._is_image_available(temp):  # 잘못된 id값을 받았을 경우
                 return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
         if serializer.is_valid():
             import pdb
             pdb.set_trace()
             serializer.save(writer=request.user,
                             group=profile.group)  # 작성자 요청자로 설정
             return JsonResponse(
                 serializer.data, status=status.HTTP_201_CREATED)
         return Response(
             serializer.errors,
             status=status.HTTP_400_BAD_REQUEST)  # 폼에 오류가 있을 경우
     return Response(status=status.HTTP_401_UNAUTHORIZED)  # 인증되지 않았을 경우
Ejemplo n.º 10
0
 def post(self, request, format=None):
     serializer = ArticleSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=201)
     return Response(serializer.errors, status=400)
Ejemplo n.º 11
0
def article_filter(request):
    '''
    List articles according to a particular filter
    '''

    if request.method == 'GET':

        articles = Article.objects.all()
        categories = Category.objects.all()
        publishers = Publisher.objects.all()

        category = request.GET.get('category')
        publisher = request.GET.get('publisher')
        id_only = request.GET.get('id')
        sentiment_score_min = request.GET.get('sentiment_score_min')
        sentiment_score_max = request.GET.get('sentiment_score_max')

        articleLimit = request.GET.get('limit')
        articleOffset = request.GET.get('offset')

        # Only articles from last 24 hours
        # articles = articles.filter(publish_date__gte=(datetime.now() - timedelta(days=1)))

        # Order articles by publish date descending order
        articles = articles.order_by('-publish_date')

        # Only 1 Article of particular ID
        if valid_filter(id_only):
            articles = articles.filter(id=id_only)

        # Filter by Category, Publisher, Sentiment Score Range
        if valid_filter(category):
            if not category == "recommended":
                articles = articles.filter(categories__taxonomy_id=category)

        if valid_filter(publisher):
            articles = articles.filter(publisher__name=publisher)

        if valid_filter(sentiment_score_min):
            articles = articles.filter(
                sentiment_score__gte=sentiment_score_min)

        if valid_filter(sentiment_score_max):
            articles = articles.filter(sentiment_score__lt=sentiment_score_max)

        # Article Limit and Offset
        if valid_filter(category):
            if category == "recommended" and request.user.is_authenticated:
                user = request.user
                articles = sortByRecommended(user, articles)

        if valid_filter(articleOffset) and valid_filter(articleLimit):
            return articles[int(articleOffset):int(articleOffset) +
                            int(articleLimit)]

        elif valid_filter(articleLimit):
            return articles[0:int(articleLimit)]

        else:
            return articles

            # articles = serializers.serialize('json', articles)
            # serializer = ArticleSerializer(articles, many=True)
            # print(serializer)
            # return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ArticleSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)

        return JsonResponse(serializer.errors, status=400)