forked from shestera/django-multisite
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from jbazik/fix-loader
Avoid template loader details for better django version compatibility.
- Loading branch information
Showing
2 changed files
with
26 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.core.exceptions import SuspiciousFileOperation | ||
import os | ||
from django.conf import settings | ||
from django.contrib.sites.models import Site | ||
try: | ||
from django.template import Origin | ||
except ImportError: | ||
# Django < 1.9 only expects the name string, not an Origin object | ||
def Origin(name="", *args, **kwargs): | ||
return name | ||
from django.template.loaders.filesystem import Loader as FilesystemLoader | ||
from django.utils._os import safe_join | ||
|
||
|
||
class Loader(FilesystemLoader): | ||
def get_template_sources(self, template_name, template_dirs=None): | ||
if not template_dirs: | ||
template_dirs = settings.TEMPLATE_DIRS | ||
|
||
domain = Site.objects.get_current().domain | ||
default_dir = getattr(settings, 'MULTISITE_DEFAULT_TEMPLATE_DIR', 'default') | ||
|
||
new_template_dirs = [] | ||
for template_dir in template_dirs: | ||
new_template_dirs.append(safe_join(template_dir, domain)) | ||
if default_dir: | ||
new_template_dirs.append(safe_join(template_dir, default_dir)) | ||
|
||
for template_dir in new_template_dirs: | ||
try: | ||
name = safe_join(template_dir, template_name) | ||
except UnicodeDecodeError: | ||
# The template dir name was a bytestring that wasn't valid UTF-8. | ||
raise | ||
except (ValueError, SuspiciousFileOperation): | ||
# The joined path was located outside of this particular | ||
# template_dir (it might be inside another one, so this isn't | ||
# fatal). | ||
continue | ||
|
||
yield Origin( | ||
name=name, | ||
template_name=template_name, | ||
loader=self | ||
) | ||
default_dir = getattr(settings, 'MULTISITE_DEFAULT_TEMPLATE_DIR', | ||
'default') | ||
for tname in (os.path.join(domain, template_name), | ||
os.path.join(default_dir, template_name)): | ||
for item in super(Loader, self).get_template_sources(tname, | ||
template_dirs): | ||
yield item |