Skip to content

Commit 1ac5db7

Browse files
virogenesisKátia Nakamura
authored andcommitted
Adds a DjangoLoader for templates so they can be overriden with prefix {year}_template.html (#145)
1 parent 153f459 commit 1ac5db7

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

pyconbalkan/conference/middleware.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import threading
12
from urllib.parse import urljoin
23

34
from django.conf import settings
45
from django.http import HttpResponseRedirect
56

67
from pyconbalkan.conference.models import Conference
78

9+
context = threading.local()
10+
811

912
class ConferenceSelectionMiddleware:
1013
def __init__(self, get_response):
@@ -53,4 +56,12 @@ def __call__(self, request):
5356
), "/")
5457
)
5558

59+
# Please forgive me everyone, I couldn't find a better way of accessing the conference object globally.
60+
# And I really didn't want to use django.contrib.sites.models.Site
61+
# https://docs.djangoproject.com/en/2.1/ref/contrib/sites/#module-django.contrib.sites
62+
# If anyone has any better idea how to accomplish this please make a pull request.
63+
#
64+
# Idea taken from: https://stackoverflow.com/a/27694861/548059
65+
66+
context.conference = request.conference
5667
return self.get_response(request)

pyconbalkan/core/loaders.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from os import path
2+
3+
from django.template import TemplateDoesNotExist
4+
from django.template.loaders.app_directories import Loader
5+
6+
from pyconbalkan.conference.middleware import context
7+
8+
9+
class PyconLoader(Loader):
10+
def get_template(self, template_name, skip=None):
11+
if not hasattr(context, "conference"):
12+
raise TemplateDoesNotExist("Conference object not be found in context, skipping.")
13+
template_basename = "{:d}_{}".format(context.conference.year, path.basename(template_name))
14+
template_name = path.join(path.dirname(template_name), template_basename)
15+
16+
return super(PyconLoader, self).get_template(template_name, skip=None)

pyconbalkan/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@
8787
{
8888
'BACKEND': 'django.template.backends.django.DjangoTemplates',
8989
'DIRS': [],
90-
'APP_DIRS': True,
9190
'OPTIONS': {
91+
'loaders': [
92+
'pyconbalkan.core.loaders.PyconLoader',
93+
'django.template.loaders.app_directories.Loader',
94+
],
9295
'context_processors': [
9396
'django.template.context_processors.debug',
9497
'django.template.context_processors.request',

0 commit comments

Comments
 (0)