forked from jazzband/django-simple-history
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dominik Kozaczko
committed
Jan 29, 2013
1 parent
a87f970
commit ca9aa8d
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|