Skip to content

Adds a DjangoLoader for templates so they can be overriden with prefi… #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pyconbalkan/conference/middleware.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import threading
from urllib.parse import urljoin

from django.conf import settings
from django.http import HttpResponseRedirect

from pyconbalkan.conference.models import Conference

context = threading.local()


class ConferenceSelectionMiddleware:
def __init__(self, get_response):
Expand Down Expand Up @@ -53,4 +56,12 @@ def __call__(self, request):
), "/")
)

# Please forgive me everyone, I couldn't find a better way of accessing the conference object globally.
# And I really didn't want to use django.contrib.sites.models.Site
# https://docs.djangoproject.com/en/2.1/ref/contrib/sites/#module-django.contrib.sites
# If anyone has any better idea how to accomplish this please make a pull request.
#
# Idea taken from: https://stackoverflow.com/a/27694861/548059

context.conference = request.conference
return self.get_response(request)
16 changes: 16 additions & 0 deletions pyconbalkan/core/loaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from os import path

from django.template import TemplateDoesNotExist
from django.template.loaders.app_directories import Loader

from pyconbalkan.conference.middleware import context


class PyconLoader(Loader):
def get_template(self, template_name, skip=None):
if not hasattr(context, "conference"):
raise TemplateDoesNotExist("Conference object not be found in context, skipping.")
template_basename = "{:d}_{}".format(context.conference.year, path.basename(template_name))
template_name = path.join(path.dirname(template_name), template_basename)

return super(PyconLoader, self).get_template(template_name, skip=None)
5 changes: 4 additions & 1 deletion pyconbalkan/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'loaders': [
'pyconbalkan.core.loaders.PyconLoader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
Expand Down