Skip to content

Commit 42d50e6

Browse files
committed
Move get user preprocessor function
1 parent a1eb33b commit 42d50e6

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

cached_auth/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818

1919
CACHE_KEY = 'cached_auth_middleware:%s'
2020

21+
user_preprocessor = None
22+
if hasattr(settings, 'CACHED_AUTH_PREPROCESSOR'):
23+
tmp = settings.CACHED_AUTH_PREPROCESSOR.split(".")
24+
module_name, function_name = ".".join(tmp[0:-1]), tmp[-1]
25+
func = getattr(__import__(module_name, fromlist=['']), function_name)
26+
if callable(func):
27+
user_preprocessor = func
28+
else:
29+
raise Exception("CACHED_AUTH_PREPROCESSOR_FUNCTION should be callable function with arguments user, request")
30+
2131
try:
2232
app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
2333
profile_model = models.get_model(app_label, model_name)
@@ -42,14 +52,8 @@ def get_cached_user(request):
4252
user = AnonymousUser()
4353
if user is None:
4454
user = get_user(request)
45-
if hasattr(settings, 'CACHED_AUTH_PREPROCESSOR_FUNCTION'):
46-
tmp = settings.CACHED_AUTH_PREPROCESSOR_FUNCTION.split(".")
47-
module_name, function_name = ".".join(tmp[0:-1]), tmp[-1]
48-
func = getattr(__import__(module_name, fromlist=['']), function_name)
49-
if callable(func):
50-
user = func(user, request)
51-
else:
52-
raise Exception("CACHED_AUTH_PREPROCESSOR_FUNCTION should be callable function with arguments user, request")
55+
if user_preprocessor:
56+
user_preprocessor(user, request)
5357
# Try to populate profile cache if profile is installed
5458
if profile_model:
5559
try:

cached_auth/tests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from django.test.client import Client
55
from django.test.utils import override_settings
66
from cached_auth import CACHE_KEY
7+
from django.conf import settings
8+
9+
import cached_auth
710

811
try:
912
from django.contrib.auth import get_user_model
@@ -53,8 +56,9 @@ def test_cached_middleware(self):
5356
self.user.delete()
5457
self.assertEqual(cache.get(key), None)
5558

56-
@override_settings(CACHED_AUTH_PREPROCESSOR_FUNCTION='cached_auth.tests.auth_preprocessor')
59+
@override_settings(CACHED_AUTH_PREPROCESSOR='cached_auth.tests.auth_preprocessor')
5760
def test_cached_auth_preprocessor_function(self):
61+
reload(cached_auth)
5862
client = Client()
5963
key = CACHE_KEY % self.user.id
6064
self.assertEqual(cache.get(key), None)

0 commit comments

Comments
 (0)