django-visitor / visitor /

from django.db import models
from django.contrib.sessions.models import Session
from django.conf import settings
from django.contrib.auth import SESSION_KEY, BACKEND_SESSION_KEY, load_backend
from django.contrib.auth.models import AnonymousUser

from visitor import managers

from datetime import datetime

class Visitor(models.Model):
    visitor_key = models.CharField(max_length='50', db_index=True)
    created = models.DateTimeField(
    last_update = models.DateTimeField()
    num_visits = models.SmallIntegerField(default=0)
    last_session_key = models.CharField(max_length=40)
    objects = managers.VisitorManager()
    def __unicode__(self):
        return '#%d/%s' % (, self.last_session_key)
    def generate_key(self, ip_address):
        if not self.visitor_key:
            from visitor.visitor_utils import create_uuid
            self.visitor_key = create_uuid(ip_address)
        return self.visitor_key

    def mark_visit(self):
        self.num_visits += 1

    def save(self, *args, **kws):
        self.last_update =
        super(Visitor, self).save(*args, **kws)
    def session(self):
            return Session.objects.get(session_key=self.last_session_key)
        except Session.DoesNotExist:
            return None
    def user():
            a backend-agnostic way to get the user from the session.
            Originally taken from and
            changed a little. Thanks jdunck
        session_engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
        session_wrapper = session_engine.SessionStore(self.last_session_key)
        user_id = session_wrapper.get(SESSION_KEY)
        auth_backend = load_backend(session_wrapper.get(BACKEND_SESSION_KEY))

        if user_id and auth_backend:
          return auth_backend.get_user(user_id)
          return AnonymousUser()