Commits

Jesús Espino  committed 2cb5815

Fixing some errors and adding a variable to count all visits in indpendent objects (for counting by date, for example)

  • Participants
  • Parent commits 55337a1

Comments (0)

Files changed (3)

File visits/models.py

 from django.db.models.aggregates import Max, Min, Avg
 from django.utils.translation import ugettext_lazy as _
 from visits.utils import is_ignored, gen_hash
+from visits import settings
 
 try:
     from django.utils.timezone import now
         else:
             raise ValueError('You must pass "app_model" or "uri" parameter.')
 
-    def get_object_visits_for(self, request, app_model, obj=None):
+    def get_object_visits_for(self, app_model=None, obj=None):
         if obj:
-            visitor_hash = gen_hash(request, obj._meta.app_label, obj.__class__.__name__, obj.id)
             return self.filter(
-                visitor_hash=visitor_hash,
                 object_app=obj._meta.app_label,
                 object_model=obj.__class__.__name__,
                 object_id=obj.id
  
     def add_uri_visit(self, request, uri, app_label):
         visitor_hash = gen_hash(request, uri)
-        visit = self.get_or_create(
-            ip_address=request.META.get('REMOTE_ADDR',''),
-            visitor_hash=visitor_hash,
-            uri=uri,
-            object_app=app_label
-        )
+        if settings.VISITS_OBJECTS_AS_COUNTERS:
+            visit = self.get_or_create(
+                ip_address=request.META.get('REMOTE_ADDR',''),
+                visitor_hash=visitor_hash,
+                uri=uri,
+                object_app=app_label
+            )
+        else:
+            visit = (Visit(
+                ip_address=request.META.get('REMOTE_ADDR',''),
+                visitor_hash=visitor_hash,
+                uri=uri,
+                object_app=app_label
+            ), True)
 
-        if len(visit) and not is_ignored(request, visit[0]):
-            visit[0].last_visit = now()
-            visit[0].visits += 1
-            visit[0].save()
+        if len(visit):
+            visit[0].last_visit = Visit.objects.filter(visitor_hash=visitor_hash).aggregate(last_visit=models.Max('last_visit'))['last_visit']
+            if not is_ignored(request, visit[0]):
+                visit[0].last_visit = now()
+                visit[0].visits += 1
+                visit[0].save()
 
     def add_object_visit(self, request, obj):
         visitor_hash = gen_hash(request, obj._meta.app_label, obj.__class__.__name__, obj.id)
-        visit = self.get_or_create(
-            visitor_hash=visitor_hash,
-            object_app=obj._meta.app_label,
-            object_model=obj.__class__.__name__,
-            object_id=obj.id,
-            ip_address=request.META.get('REMOTE_ADDR', '')
-        )
+        if settings.VISITS_OBJECTS_AS_COUNTERS:
+            visit = self.get_or_create(
+                visitor_hash=visitor_hash,
+                object_app=obj._meta.app_label,
+                object_model=obj.__class__.__name__,
+                object_id=obj.id,
+                ip_address=request.META.get('REMOTE_ADDR', '')
+            )
+        else:
+            visit = (Visit(
+                visitor_hash=visitor_hash,
+                object_app=obj._meta.app_label,
+                object_model=obj.__class__.__name__,
+                object_id=obj.id,
+                ip_address=request.META.get('REMOTE_ADDR', '')
+            ), True)
 
-        if len(visit) and not is_ignored(request, visit[0]):
-            visit[0].last_visit = now()
-            visit[0].visits += 1
-            visit[0].save()
+        if len(visit):
+            visit[0].last_visit = Visit.objects.filter(visitor_hash=visitor_hash).aggregate(last_visit=models.Max('last_visit'))['last_visit']
+            if not is_ignored(request, visit[0]):
+                visit[0].last_visit = now()
+                visit[0].visits += 1
+                visit[0].save()
 
     def calculate(self, obj, uri, what=MAX):
         if obj:

File visits/settings.py

 
 REQUEST_FIELDS_FOR_HASH = getattr(settings, 'REQUEST_FIELDS_FOR_HASH', ['REMOTE_ADDR', 'HTTP_USER_AGENT'])
 URI_WITH_GET_PARAMS = getattr(settings, 'URI_WITH_GET_PARAMS', False)
+VISITS_OBJECTS_AS_COUNTERS = getattr(settings, 'VISITS_OBJECTS_AS_COUNTERS', True)

File visits/templatetags/visits_tags.py

 # -*- coding: utf-8 -*-
 from django.template import Library, Node, TemplateSyntaxError, Variable
 from django.utils.translation import ugettext as _
-from django.db.models import Sum
+from django.db.models import Sum, Model
 
 from visits.models import Visit
 
 
     def render(self, context):
         obj = self.obj.resolve(context)
-        if type(obj) is dict:
+        
+        if isinstance(obj, dict):
             context[self.context_var] = Visit.objects.filter(
+                uri=obj["request_path"],
+            ).aggregate(visits_sum=Sum("visits"))["visits_sum"]
+        elif isinstance(obj, Model):
+            context[self.context_var] = Visit.objects.filter(
+                object_app=obj._meta.app_label,
                 object_model=obj.__class__.__name__,
                 object_id=obj.id
             ).aggregate(visits_sum=Sum('visits'))['visits_sum']
-        elif "instance" in str(type(obj)):
-            context[self.context_var] = Visit.objects.filter(
-                uri=obj["request_path"]
-            ).aggregate(visits_sum=Sum("visits"))["visits_sum"]
-        return ""
+        return ''
 
 def do_get_visits(parser, token):
     """