Commit 4f61f1ad352f42d8e6f7743d87d70dbf217596e4
1 parent
cfde3435
Exists in
master
and in
39 other branches
Moving pagehit counter to decorator
Showing
8 changed files
with
27 additions
and
53 deletions
Show diff stats
src/api/handlers.py
1 | 1 | ||
2 | -from django.core.cache import cache | ||
3 | - | ||
4 | from piston.utils import rc | 2 | from piston.utils import rc |
5 | from piston.handler import BaseHandler | 3 | from piston.handler import BaseHandler |
6 | 4 | ||
7 | from colab.deprecated import solrutils | 5 | from colab.deprecated import solrutils |
8 | -from super_archives.models import PageHit | ||
9 | - | ||
10 | - | ||
11 | -class CountHandler(BaseHandler): | ||
12 | - allowed_methods = ('POST') | ||
13 | - | ||
14 | - def create(self, request): | ||
15 | - """Add one page view for the given url""" | ||
16 | - | ||
17 | - # If missing the path_info argument we can't do anything | ||
18 | - path_info = request.POST.get('path_info') | ||
19 | - if not path_info: | ||
20 | - return rc.BAD_REQUEST | ||
21 | 6 | ||
22 | - # Here we cache the user's IP to ensure that the same | ||
23 | - # IP won't hit the same page again for while | ||
24 | - ip_addr = request.META.get('REMOTE_ADDR') | ||
25 | - page_hits_cache = cache.get('page_hits', {}) | ||
26 | - duplicate = page_hits_cache.get(path_info, {}).get(ip_addr) | ||
27 | - | ||
28 | - if duplicate: | ||
29 | - return rc.DUPLICATE_ENTRY | ||
30 | - else: | ||
31 | - page_hits_cache.update({path_info: {ip_addr: True }}) | ||
32 | - cache.set('page_hits', page_hits_cache) | ||
33 | - | ||
34 | - # Everything ok, so just increment the page count | ||
35 | - page_hit = PageHit.objects.get_or_create(url_path=path_info)[0] | ||
36 | - page_hit.hit_count += 1 | ||
37 | - page_hit.save() | ||
38 | - | ||
39 | - return rc.CREATED | ||
40 | 7 | ||
41 | class SearchHandler(BaseHandler): | 8 | class SearchHandler(BaseHandler): |
42 | allowed_methods = ('GET', ) | 9 | allowed_methods = ('GET', ) |
src/api/urls.py
@@ -2,15 +2,12 @@ from django.conf.urls import patterns, include, url | @@ -2,15 +2,12 @@ from django.conf.urls import patterns, include, url | ||
2 | 2 | ||
3 | from piston.resource import Resource | 3 | from piston.resource import Resource |
4 | 4 | ||
5 | -from .handlers import CountHandler, SearchHandler | 5 | +from .handlers import SearchHandler |
6 | from .views import VoteView | 6 | from .views import VoteView |
7 | 7 | ||
8 | - | ||
9 | -count_handler = Resource(CountHandler) | ||
10 | search_handler = Resource(SearchHandler) | 8 | search_handler = Resource(SearchHandler) |
11 | 9 | ||
12 | urlpatterns = patterns('', | 10 | urlpatterns = patterns('', |
13 | url(r'message/(?P<msg_id>\d+)/vote$', VoteView.as_view()), | 11 | url(r'message/(?P<msg_id>\d+)/vote$', VoteView.as_view()), |
14 | - url(r'hit/$', count_handler), | ||
15 | url(r'search/$', search_handler), | 12 | url(r'search/$', search_handler), |
16 | ) | 13 | ) |
src/static/js/base.js
@@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
1 | + | ||
2 | +from django.core.cache import cache | ||
3 | + | ||
4 | +from super_archives.models import PageHit | ||
5 | + | ||
6 | + | ||
7 | +def count_hit(view): | ||
8 | + def wrapper(request, *args, **kwargs): | ||
9 | + # Here we cache the user's IP to ensure that the same | ||
10 | + # IP won't hit the same page again for while | ||
11 | + ip_addr = request.META.get('REMOTE_ADDR') | ||
12 | + cache_key = u'page_hits-{}-{}'.format(request.path_info, ip_addr) | ||
13 | + duplicate = cache.get(cache_key) | ||
14 | + if duplicate: | ||
15 | + return view(request, *args, **kwargs) | ||
16 | + cache.set(cache_key, True) | ||
17 | + | ||
18 | + # Everything ok, so just increment the page count | ||
19 | + page_hit = PageHit.objects.get_or_create(url_path=request.path_info)[0] | ||
20 | + page_hit.hit_count += 1 | ||
21 | + page_hit.save() | ||
22 | + | ||
23 | + return view(request, *args, **kwargs) | ||
24 | + return wrapper |
src/super_archives/templates/message-preview.html
@@ -7,8 +7,6 @@ | @@ -7,8 +7,6 @@ | ||
7 | src="{{ STATIC_URL }}img/{{ doc.Type }}.png" /> | 7 | src="{{ STATIC_URL }}img/{{ doc.Type }}.png" /> |
8 | {% else %} | 8 | {% else %} |
9 | <span class="glyphicon glyphicon-envelope"></span> | 9 | <span class="glyphicon glyphicon-envelope"></span> |
10 | -<!-- <img alt="thread" title="thread" | ||
11 | - src="{{ STATIC_URL }}img/thread.png" /> --> | ||
12 | {% endif %} | 10 | {% endif %} |
13 | 11 | ||
14 | {% if doc.mailinglist %} | 12 | {% if doc.mailinglist %} |
src/super_archives/templates/message-thread.html
src/super_archives/views.py
@@ -9,8 +9,10 @@ from django.core.exceptions import ObjectDoesNotExist | @@ -9,8 +9,10 @@ from django.core.exceptions import ObjectDoesNotExist | ||
9 | from django.shortcuts import render, get_list_or_404 | 9 | from django.shortcuts import render, get_list_or_404 |
10 | 10 | ||
11 | from .models import MailingList, Thread | 11 | from .models import MailingList, Thread |
12 | +from .decorators import count_hit | ||
12 | 13 | ||
13 | 14 | ||
15 | +@count_hit | ||
14 | def thread(request, mailinglist, thread_token): | 16 | def thread(request, mailinglist, thread_token): |
15 | 17 | ||
16 | try: | 18 | try: |
src/templates/base.html
@@ -22,9 +22,6 @@ | @@ -22,9 +22,6 @@ | ||
22 | <script type="text/javascript" src="{{ STATIC_URL }}third-party/jquery.cookie.js"></script> | 22 | <script type="text/javascript" src="{{ STATIC_URL }}third-party/jquery.cookie.js"></script> |
23 | <script src="{{ STATIC_URL }}third-party/bootstrap/js/bootstrap.js"></script> | 23 | <script src="{{ STATIC_URL }}third-party/bootstrap/js/bootstrap.js"></script> |
24 | 24 | ||
25 | - <script type="text/javascript" src="{{ STATIC_URL }}js/base.js"></script> | ||
26 | - | ||
27 | - | ||
28 | {% block head_js %}{% endblock %} | 25 | {% block head_js %}{% endblock %} |
29 | {% block head_css %}{% endblock %} | 26 | {% block head_css %}{% endblock %} |
30 | 27 |