Daniel Greenfeld avatar Daniel Greenfeld committed 24d852e

tests and better docs

Comments (0)

Files changed (6)

 
     python manage.py syncdb
 
-Custom Settings
----------------
+Required Settings
+-----------------
 
 Add the following two properties to your settings file::
 
         Default = 31536000 # or 1 year.
         # This is how long the cookie will live in your visitors browser before it expires on it's own
         COOKIE_MAX_AGE = 31536000 # 1 year
+
+Custom settings
+---------------
+
+These settings can be overridden::
+
+    VISITOR_KEY
+        Default = 'visitor_key'
+
+    COOKIE_SAVED
+        Default = 'cookie_saved'
+
+    COOKIE_VISITOR_KEY
+        Default = 'vk'
+
+    REQUEST_LOG_ID
+        Default = 'request_log_id'
         
 References
 ----------

visitor/cache_utils.py

+from django.core.cache import cache
+
+CACHE_TIMEOUT = 60 * 60 * 1 # one hour
+
+def make_cache_key(visitor_key):
+    """ make the cache key for visitor """
+    return 'visitor_%s' % (visitor_key)
+
+def check_cache(visitor_key):
+    """ check the cache for this visitor """
+    cached = cache.get(make_cache_key(visitor_key))
+    if cached:
+       return cached
+       
+    return None
+
+def set_cache(visitor):
+    """ set the visitor object in the cache """
+    if visitor.visitor_key:
+        cache_key = make_cache_key(visitor.visitor_key) 
+        cache.set(cache_key, visitor, CACHE_TIMEOUT)

visitor/managers.py

             
     def create_from_ip(self, ip):
         from visitor.visitor_utils import create_uuid
-        return self.get_or_create(visitor_key=create_uuid(ip))[0]
-        
+        return self.get_or_create(visitor_key=create_uuid(ip))[0]

visitor/tests/__init__.py

+from visitor.tests.test_models import *

visitor/tests/test_models.py

+from datetime import datetime
+
+from django.contrib.sites.models import Site
+from django.core.cache import cache
+from django.http import HttpResponse
+from django.test import TestCase
+
+from visitor.models import Visitor
+
+class BaseTestCase(TestCase):
+        
+    def tearDown(self):
+        cache.clear()
+
+class VisitorModelTest(BaseTestCase):
+
+    def test_create_visitor(self):
+
+        count = Visitor.objects.count()
+        visitor = Visitor.objects.create(
+            visitor_key = 'abcdefg',
+            last_session_key = 'hijklmnop'
+        )
+        visitor.save()
+        self.assertTrue(count < Visitor.objects.count())
+

visitor/visitor_constants.py

-VISITOR_KEY = 'visitor_key'
-COOKIE_SAVED = 'cookie_saved'
-COOKIE_VISITOR_KEY = 'vk'
+from django.conf import settings
+
+VISITOR_KEY = getattr(settings, VISITOR_KEY, 'visitor_key')
+COOKIE_SAVED = getattr(settings, COOKIE_SAVED, 'cookie_saved')
+COOKIE_VISITOR_KEY = getattr(settings, COOKIE_VISITOR_KEY, 'vk')
+REQUEST_LOG_ID = getattr(settings, REQUEST_LOG_ID, 'request_log_id')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.