[tor-commits] [pups/production] added a new db column (visits) which acts as a counter for visits per token by users

colin at torproject.org colin at torproject.org
Fri Oct 24 03:44:56 UTC 2014


commit 331658438dc31b8ae1b4e487a4aeccfd373d1419
Author: Sherief Alaa <sheriefalaa.w at gmail.com>
Date:   Mon Jun 2 06:40:54 2014 +0300

    added a new db column (visits) which acts as a counter for visits per token by users
---
 pups/settings.py.sample |    1 +
 webchat/models.py       |   12 +++++++++++-
 webchat/views.py        |   17 +++++++++++------
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/pups/settings.py.sample b/pups/settings.py.sample
index 97d4ab6..85607cf 100644
--- a/pups/settings.py.sample
+++ b/pups/settings.py.sample
@@ -98,6 +98,7 @@ INSTALLED_APPS = (
     'pups',
     'webchat',
     'stats',
+    'south'
 )
 
 TEMPLATE_CONTEXT_PROCESSORS = (
diff --git a/webchat/models.py b/webchat/models.py
index b475a6d..375ba04 100644
--- a/webchat/models.py
+++ b/webchat/models.py
@@ -36,9 +36,10 @@ class Token(models.Model):
     created_at = models.DateTimeField(auto_now_add=True)
     expires_at = models.DateTimeField()
     comment = models.CharField(max_length=128)
+    visits = models.IntegerField(default=0)
 
     def __unicode__(self):
-        return u'ID: %s Owner: %s' % (self.t_id, self.owner)
+        return u'ID: %s Owner: %s visits: %s' % (self.t_id, self.owner, self.visits)
 
     def create_token(self, owner_id, expiration_days, comment):
         q = Token(
@@ -73,3 +74,12 @@ class Token(models.Model):
         '''
         return Token.objects.filter(owner=assistant).order_by('-t_id')\
             .filter(expires_at__gt=timezone.now())
+
+    def increment_visits(self, id):
+        token = Token.objects.filter(t_id=id)
+
+        if not token:
+            return False
+
+        token.update(visits=F('visits')+1)
+        return True
diff --git a/webchat/views.py b/webchat/views.py
index aec8135..f5b9aad 100644
--- a/webchat/views.py
+++ b/webchat/views.py
@@ -86,20 +86,25 @@ def chat(request, token):
     and did not expire.
     '''
 
-    t = Token()
-    t_obj = get_object_or_404(Token, token=token)
+    requested_token = get_object_or_404(Token, token=token)
 
     # Make sure token didn't expire
-    if t_obj.expires_at < timezone.now():
+    if requested_token.expires_at < timezone.now():
         return render(request, "token_exp.html")
 
     params = {
         'server': settings.CONFIG['server'],
         'bosh': settings.CONFIG['bosh'],
-        'receiver': t_obj.owner.username + settings.CONFIG['receiver'],
-        'receiver_name': t_obj.owner.username,
-        'token': token
+        'receiver':
+        requested_token.owner.username + settings.CONFIG['receiver'],
+        'receiver_name': requested_token.owner.username,
+        'token': token,
+        'comment': requested_token.comment
     }
+
+    # Count visits for metrics
+    requested_token.increment_visits(requested_token.pk)
+
     return render(request, 'prodromus.html', params)
 
 





More information about the tor-commits mailing list