handlers.py
2.9 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from django.core.cache import cache
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required
from piston.utils import rc
from piston.handler import BaseHandler
from colab import solrutils
from colab.super_archives.models import Message, PageHit
class VoteHandler(BaseHandler):
allowed_methods = ('GET', 'POST', 'DELETE')
def create(self, request, message_id):
if not request.user.is_authenticated():
return rc.FORBIDDEN
try:
Message.objects.get(id=message_id).vote(request.user)
except IntegrityError:
return rc.DUPLICATE_ENTRY
return rc.CREATED
def read(self, request, message_id):
return Message.objects.get(id=message_id).votes_count()
def delete(self, request, message_id):
if not request.user.is_authenticated():
return rc.FORBIDDEN
try:
Message.objects.get(id=message_id).unvote(request.user)
except ObjectDoesNotExist:
return rc.NOT_HERE
return rc.DELETED
class CountHandler(BaseHandler):
allowed_methods = ('POST')
def create(self, request):
"""Add one page view for the given url"""
# If missing the path_info argument we can't do anything
path_info = request.POST.get('path_info')
if not path_info:
return rc.BAD_REQUEST
# Here we cache the user's IP to ensure that the same
# IP won't hit the same page again for while
ip_addr = request.META.get('REMOTE_ADDR')
page_hits_cache = cache.get('page_hits', {})
duplicate = page_hits_cache.get(path_info, {}).get(ip_addr)
if duplicate:
return rc.DUPLICATE_ENTRY
else:
page_hits_cache.update({path_info: {ip_addr: True }})
cache.set('page_hits', page_hits_cache)
# Everything ok, so just increment the page count
page_hit = PageHit.objects.get_or_create(url_path=path_info)[0]
page_hit.hit_count += 1
page_hit.save()
return rc.CREATED
class SearchHandler(BaseHandler):
allowed_methods = ('GET', )
def read(self, request):
query = request.GET.get('q')
page = request.GET.get('p', 1)
results_per_page = request.GET.get('n', 50)
order = request.GET.get('o')
if not query:
return 'Query cannot be empty.'
else:
query = query.encode('utf-8')
try:
n = int(results_per_page)
except ValueError:
n = 10
if 1 > n > 500:
n = 1
try:
page = int(page)
except ValueError:
page = 1
if page < 1:
page = 1
return solrutils.select(query, results_per_page, page, order)