Skip to content

Commit

Permalink
updated to use middleware for users
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Kozaczko committed Jan 29, 2013
1 parent a87f970 commit ca9aa8d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Download the tar.gz, extract it and run the following inside the directory:
== Basic usage ==
Using this package is _really_ simple; you just have to import HistoricalRecords and create an instance of it on every model you want to historically track.

Append the following line you your MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = (
'simple_history.middleware.CurrentUserMiddleware',
)


On your models you need to include the following line at the top:
from simple_history.models import HistoricalRecords

Expand Down
39 changes: 39 additions & 0 deletions simple_history/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""models.py: Simple History Middleware - sued for capturing the user"""

__author__ = 'Marty Alchin'
__date__ = '2011/08/29 20:43:34'
__credits__ = ['Marty Alchin', 'Corey Bertram', 'Steven Klass']

from django.db.models import signals
from django.utils.functional import curry
from django.utils.decorators import decorator_from_middleware

from registration import FieldRegistry

class CurrentUserMiddleware(object):

def process_request(self, request):
if request.method in ['GET', 'HEAD', 'OPTIONS', 'TRACE']:
# We aren't doing anything return..
return

if hasattr(request, 'user') and request.user.is_authenticated:
user = request.user
else:
user = None

update_users = curry(self.update_users, user)
signals.pre_save.connect(update_users, dispatch_uid=request, weak=False)

def update_users(self, user, sender, instance, **kwargs):
registry = FieldRegistry()
if sender in registry:
for field in registry.get_fields(sender):
setattr(instance, field.name, user)

def process_response(self, request, response):
signals.pre_save.disconnect(dispatch_uid=request)
return response

record_current_user = decorator_from_middleware(CurrentUserMiddleware)
15 changes: 15 additions & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
from django.utils import importlib
from manager import HistoryDescriptor

from registration import FieldRegistry
from django.contrib.auth.models import User

# This is used to store the user id - else just None.
class CurrentUserField(models.ForeignKey):
def __init__(self, **kwargs):
super(CurrentUserField, self).__init__(User, null=True, **kwargs)

def contribute_to_class(self, cls, name):
super(CurrentUserField, self).contribute_to_class(cls, name)
registry = FieldRegistry()
registry.add_field(cls, self)



class HistoricalRecords(object):
def contribute_to_class(self, cls, name):
Expand Down Expand Up @@ -125,6 +139,7 @@ def get_instance(self):
'history_id': models.AutoField(primary_key=True),
'history_date': models.DateTimeField(auto_now_add=True),
'history_user': models.ForeignKey(User, null=True),
'changed_by': CurrentUserField(related_name=rel_nm),
'history_type': models.CharField(max_length=1, choices=(
('+', 'Created'),
('~', 'Changed'),
Expand Down
20 changes: 20 additions & 0 deletions simple_history/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
"""registration.py: Simple History Registration for users"""

__author__ = 'Marty Alchin'
__date__ = '2011/08/29 20:43:34'
__credits__ = ['Marty Alchin', 'Corey Bertram', 'Steven Klass']

class FieldRegistry(object):
_registry = {}

def add_field(self, model, field):
reg = self.__class__._registry.setdefault(model, [])
reg.append(field)

def get_fields(self, model):
return self.__class__._registry.get(model, [])

def __contains__(self, model):
return model in self.__class__._registry

0 comments on commit ca9aa8d

Please sign in to comment.