Skip to content

Instantly share code, notes, and snippets.

@bjinwright
Last active May 26, 2017 17:39
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bjinwright/e2c93bb7d4c85a7da3d5e3e3e45889ad to your computer and use it in GitHub Desktop.
Django Theme Switcher that does not use Django Sites and doesn't use Thread.local
from django.core.exceptions import SuspiciousFileOperation
from django.template import Origin
from django.template.loaders.filesystem import Loader
from django.conf import settings
from django.utils._os import safe_join
class ThemeLoader(Loader):
themes = [theme.get('theme') for theme in settings.SITE_CONFIGS.values()]
current_theme = ''
def get_template_sources(self, template_name, template_dirs=None):
template_split = template_name.split('/')
if template_split[0] in self.themes:
self.theme = template_split[0]
else:
template_name = ['{}/{}'.format(self.theme,template_name),template_name]
if not template_dirs:
template_dirs = self.get_dirs()
for template_dir in template_dirs:
if isinstance(template_name,list):
for i in template_name:
try:
name = safe_join(template_dir,i)
except SuspiciousFileOperation:
continue
else:
try:
name = safe_join(template_dir, template_name)
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
continue
yield Origin(
name=name,
template_name=template_name,
loader=self,
)
def get_contents(self, origin):
contents = super(ThemeLoader, self).get_contents(origin)
return contents
from django.conf import settings
class ThemeMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
def process_template_response(self,request, response):
host = request.get_host()
if host in settings.SITE_CONFIGS:
theme = settings.SITE_CONFIGS.get(host).get('theme')
response.template_name = [
'{}/{}'.format(theme,tn) for tn in response.template_name]
return response
"""
Django settings for themeswap project.
Generated by 'django-admin startproject' using Django 1.11.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xgg!g(_w_4rbdd7!ms+15z3w*+re2bb@xvm#2+4$@!%j=s2fg5'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'demo'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'demo.middleware.ThemeMiddleware'
]
SITE_CONFIGS = {
'127.0.0.1:8000':{
'theme':'blue'
},
'127.0.0.1:8001': {
'theme':'green'
},
}
ROOT_URLCONF = 'themeswap.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/home/brian/.virtualenvs/tmp-5d1d95d6be51b0b5/themeswap/demo/templates'
],
'APP_DIRS': False,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'loaders':[
'demo.loader.ThemeLoader'
]
},
},
]
WSGI_APPLICATION = 'themeswap.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment