views.py
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from django import http
from django.db import IntegrityError
from django.views.generic import View
from django.core.exceptions import ObjectDoesNotExist
from super_archives.models import Message
class VoteView(View):
http_method_names = [u'get', u'put', u'delete', u'head']
def put(self, request, msg_id):
if not request.user.is_authenticated():
return http.HttpResponseForbidden()
try:
Message.objects.get(id=msg_id).vote(request.user)
except IntegrityError:
# 409 Conflict
# used for duplicated entries
return http.HttpResponse(status=409)
# 201 Created
return http.HttpResponse(status=201)
def get(self, request, msg_id):
votes = Message.objects.get(id=msg_id).votes_count()
return http.HttpResponse(votes, content_type='application/json')
def delete(self, request, msg_id):
if not request.user.is_authenticated():
return http.HttpResponseForbidden()
try:
Message.objects.get(id=msg_id).unvote(request.user)
except ObjectDoesNotExist:
return http.HttpResponseGone()
# 204 No Content
# empty body, as per RFC2616.
# object deleted
return http.HttpResponse(status=204)