Commit dd7a373f authored by luxcem's avatar luxcem

adds votes views and urls

parent 8f6731e3
from django.conf.urls import url
from rp.views import upvote, downvote
urlpatterns = [
url(
r"^votes/upvote/(?P<content_type>\d+)/(?P<object_id>\d+)$",
upvote,
name="und-upvote"
),
url(
r"^votes/downvote/(?P<content_type>\d+)/(?P<object_id>\d+)$",
downvote,
name="und-downvote"
),
]
from django.shortcuts import render
# Create your views here.
from .votes import upvote, downvote, UDList # noqa
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(user=request.user)
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(user=request.user)
return HttpResponse({"success"})
class UDList(ListView):
"""
Ads id upvoted by user
"""
def get_context_data(self, **kwargs):
content_type = ContentType.objects.get_for_model(self.model)
queryset = self.get_queryset()
context = super().get_context_data(**kwargs)
context["und_votes"] = {
"upvoted": queryset.filter(
und_votes__user=self.request.user,
und_votes__content_type=content_type,
und_votes__score=1
).values_list("id", flat=True),
"downvoted": queryset.filter(
und_votes__user=self.request.user,
und_votes__content_type=content_type,
und_votes__score=-1
).values_list("id", flat=True)
}
return context
"""
rp_new URL Configuration
"""
from django.conf.urls import url
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r"^admin/", admin.site.urls),
url(r"^rp/", include("rp.urls"))
]
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