Skip to content

Commit

Permalink
adding threadlocals and middleware abstracts
Browse files Browse the repository at this point in the history
  • Loading branch information
wtayyeb committed Oct 31, 2015
1 parent e9b9758 commit 7554ed3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
32 changes: 32 additions & 0 deletions theming/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding:utf-8 -*-
'''
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
'''

from .models import ThemeManager
from .threadlocals import set_thread_variable


class ThemingMiddleware(object):
''' Middleware that puts the request object in thread local storage.
add this middleware to MIDDLEWARE_CLASSES to make theming work.
MIDDLEWARE_CLASSES = (
...
'theming.middleware.ThemingMiddleware',
)
'''

def process_request(self, request):
host = request.get_host()
mgr = ThemeManager()
mgr.set_host(host)

# why ?
set_thread_variable('theme_manager', mgr)

# why ?
set_thread_variable('request', request)
18 changes: 18 additions & 0 deletions theming/threadlocals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding:utf-8 -*-
'''
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
'''

from threading import local


_threadlocals = local()


def set_thread_variable(key, val):
setattr(_threadlocals, key, val)


def get_thread_variable(key, default=None):
return getattr(_threadlocals, key, default)

0 comments on commit 7554ed3

Please sign in to comment.