diff --git a/src/api/handlers.py b/src/api/handlers.py index 0e4e0b7..093253b 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -1,42 +1,9 @@ -from django.core.cache import cache - from piston.utils import rc from piston.handler import BaseHandler from colab.deprecated import solrutils -from super_archives.models import PageHit - - -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', ) diff --git a/src/api/urls.py b/src/api/urls.py index 45ac54d..ff4ea6b 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -2,15 +2,12 @@ from django.conf.urls import patterns, include, url from piston.resource import Resource -from .handlers import CountHandler, SearchHandler +from .handlers import SearchHandler from .views import VoteView - -count_handler = Resource(CountHandler) search_handler = Resource(SearchHandler) urlpatterns = patterns('', url(r'message/(?P\d+)/vote$', VoteView.as_view()), - url(r'hit/$', count_handler), url(r'search/$', search_handler), ) diff --git a/src/static/js/base.js b/src/static/js/base.js deleted file mode 100644 index 0deb2b3..0000000 --- a/src/static/js/base.js +++ /dev/null @@ -1,7 +0,0 @@ -function pagehit(path_info) { - jQuery.ajax({ - url: '/api/hit/', - type: 'POST', - data: {'path_info': path_info}, - }); -} diff --git a/src/super_archives/decorators.py b/src/super_archives/decorators.py new file mode 100644 index 0000000..dd556f0 --- /dev/null +++ b/src/super_archives/decorators.py @@ -0,0 +1,24 @@ + +from django.core.cache import cache + +from super_archives.models import PageHit + + +def count_hit(view): + def wrapper(request, *args, **kwargs): + # 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') + cache_key = u'page_hits-{}-{}'.format(request.path_info, ip_addr) + duplicate = cache.get(cache_key) + if duplicate: + return view(request, *args, **kwargs) + cache.set(cache_key, True) + + # Everything ok, so just increment the page count + page_hit = PageHit.objects.get_or_create(url_path=request.path_info)[0] + page_hit.hit_count += 1 + page_hit.save() + + return view(request, *args, **kwargs) + return wrapper diff --git a/src/super_archives/templates/message-preview.html b/src/super_archives/templates/message-preview.html index d336140..746d5a3 100644 --- a/src/super_archives/templates/message-preview.html +++ b/src/super_archives/templates/message-preview.html @@ -7,8 +7,6 @@ src="{{ STATIC_URL }}img/{{ doc.Type }}.png" /> {% else %} - {% endif %} {% if doc.mailinglist %} diff --git a/src/super_archives/templates/message-thread.html b/src/super_archives/templates/message-thread.html index 99e86c8..22f375f 100644 --- a/src/super_archives/templates/message-thread.html +++ b/src/super_archives/templates/message-thread.html @@ -200,9 +200,5 @@ - - {% endblock %} diff --git a/src/super_archives/views.py b/src/super_archives/views.py index 31bd999..311bc46 100644 --- a/src/super_archives/views.py +++ b/src/super_archives/views.py @@ -9,8 +9,10 @@ from django.core.exceptions import ObjectDoesNotExist from django.shortcuts import render, get_list_or_404 from .models import MailingList, Thread +from .decorators import count_hit +@count_hit def thread(request, mailinglist, thread_token): try: diff --git a/src/templates/base.html b/src/templates/base.html index 6573a46..a755328 100644 --- a/src/templates/base.html +++ b/src/templates/base.html @@ -22,9 +22,6 @@ - - - {% block head_js %}{% endblock %} {% block head_css %}{% endblock %} -- libgit2 0.21.2