Commit 5db4c8b1 authored by luxcem's avatar luxcem

api urls for upvote/downvote/publish/reject

parent e5551869
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from rp.models import Article
from .serializers import ArticleSerializer
......@@ -7,3 +9,32 @@ from .serializers import ArticleSerializer
class ArticleViewSet(viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
def response_serialized_object(self, object):
return Response(self.serializer_class(object).data)
@detail_route(methods=["post"], url_path="publish")
def publish(self, request, pk=None):
article = self.get_object()
article.status = "PUBLISHED"
article.save()
return self.response_serialized_object(article)
@detail_route(methods=["post"], url_path="reject")
def reject(self, request, pk=None):
article = self.get_object()
article.status = "REJECTED"
article.save()
return self.response_serialized_object(article)
@detail_route(methods=["post"], url_path="upvote")
def upvote(self, request, pk=None):
article = self.get_object()
article.upvote(username=request.user.username)
return self.response_serialized_object(article)
@detail_route(methods=["post"], url_path="downvote")
def downvote(self, request, pk=None):
article = self.get_object()
article.downvote(username=request.user.username)
return self.response_serialized_object(article)
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-04-23 15:57
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rp', '0007_article_website'),
]
operations = [
migrations.AlterModelOptions(
name='article',
options={'permissions': (('can_change_status', 'Can change article status'), ('can_change_priority', 'Can change article priority'), ('can_vote', 'Can vote articles')), 'verbose_name': 'Article', 'verbose_name_plural': 'Articles'},
),
migrations.AddField(
model_name='article',
name='priority',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='article',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('PUBLISHED', 'Published'), ('REJECTED', 'Rejected')], default='PENDING', max_length=20, verbose_name='Status'),
),
]
......@@ -8,6 +8,11 @@ urlpatterns = [
ArticleListFlux.as_view(),
name="article-list"
),
url(
r"^article/list",
ArticleListFlux.as_view(),
name="article-list"
),
url(
r"^votes/upvote/(?P<content_type>\d+)/(?P<object_id>\d+)$",
upvote,
......
......@@ -21,5 +21,6 @@ class ArticleListFlux(UDList):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["filter_view"] = self.kwargs.get("filter_view", "draft")
context["nb_draft"] = Article.objects.filter(und_score__gte=3, status="PENDING").count()
context["nb_draft"] = Article.objects.filter(
und_score__gte=3, status="PENDING").count()
return context
from django.views.generic import ListView
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse
def upvote(request, content_type, object_id):
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)
obj.upvote(username=request.user.username)
return HttpResponse({"success"})
def downvote(request, content_type, object_id):
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)
obj.downvote(username=request.user.username)
return HttpResponse({"success"})
class UDList(ListView):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment