Skip to content

Commit

Permalink
All tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
marazmiki committed Apr 5, 2015
1 parent 8f09228 commit 517666d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 81 deletions.
2 changes: 0 additions & 2 deletions domains/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
from __future__ import division
from domains.utils import get_hooks, setup_hook


for hook in get_hooks():
print(hook)
setup_hook(hook)
7 changes: 2 additions & 5 deletions domains/hooks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def coerce(self, v):
return v

def get(self):
try:
v = getattr(_thread_locals, self.attribute)
except AttributeError:
v = self.default_value
v = getattr(_thread_locals, self.attribute, self.default_value)
return self.coerce(v)

def set(self, value):
Expand All @@ -58,7 +55,7 @@ def __int__(self):
try:
return self.get()
except AttributeError:
self.set(1)
self.set(self.default_value)
return self.get()


Expand Down
1 change: 1 addition & 0 deletions domains/hooks/site_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SiteIDHook(IntHookBase):
Dynamic SITE_ID attribute class
"""
attribute = 'SITE_ID'
default_value = 1

def __init__(self, *args, **kwargs):
super(IntHookBase, self).__init__(*args, **kwargs)
Expand Down
89 changes: 27 additions & 62 deletions domains/loaders/app_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,37 @@


try:
# Django 1.7x or older
from django.template.loaders.app_directories import app_template_dirs

class Loader(BaseLoader):
is_usable = True

def get_template_sources(self, template_name, template_dirs=None):
"""
Looks in the saved request object from the middleware for
directories and passes back the path. Doesn't verify that the
path is valid, though.
"""
if not template_dirs:
template_dirs = app_template_dirs

for template_dir in template_dirs:
try:
template_parts = get_template_name(template_dir, template_name)
if not template_parts:
continue
yield safe_join(*template_parts)
except UnicodeDecodeError:
# The template dir name was a bytestring
# that wasn't valid UTF-8.
raise
except ValueError:
# The joined path was located outside of template_dir.
pass

except ImportError:
# The new Django 1.8 template API
# Django 1.8
from django.template.utils import get_app_template_dirs
from django.core.exceptions import SuspiciousFileOperation
from django.template.base import TemplateDoesNotExist
import io

app_template_dirs = get_app_template_dirs('templates')

class Loader(BaseLoader):
is_usable = True

def get_template_sources(self, template_name, template_dirs=None):
"""
Returns the absolute paths to "template_name", when appended to each
directory in "template_dirs". Any paths that don't lie inside one of the
template dirs are excluded from the result set, for security reasons.
"""
if not template_dirs:
template_dirs = get_app_template_dirs('templates')

for template_dir in template_dirs:
try:
template_parts = get_template_name(template_dir, template_name)

if not template_parts:
continue
yield safe_join(*template_parts)
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
pass

def load_template_source(self, template_name, template_dirs=None):
for filepath in self.get_template_sources(template_name, template_dirs):
try:
with io.open(filepath, encoding=self.engine.file_charset) as fp:
return fp.read(), filepath
except IOError:
pass
raise TemplateDoesNotExist(template_name)
class Loader(BaseLoader):
is_usable = True

def get_template_sources(self, template_name, template_dirs=None):
"""
Looks in the saved request object from the middleware for
directories and passes back the path. Doesn't verify that the
path is valid, though.
"""
if not template_dirs:
template_dirs = app_template_dirs

for template_dir in template_dirs:
try:
template_parts = get_template_name(template_dir, template_name)
if not template_parts:
continue
yield safe_join(*template_parts)
except UnicodeDecodeError:
# The template dir name was a bytestring
# that wasn't valid UTF-8.
raise
except ValueError:
# The joined path was located outside of template_dir.
pass
11 changes: 8 additions & 3 deletions domains/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from django import test
from django import test, get_version
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
from django.conf.urls import url
Expand Down Expand Up @@ -210,8 +210,13 @@ def setUp(self):
def test_1(self):
for _ in range(0, 3):
for pk, site in self.sites.items():
self.client.get('/', HTTP_HOST=site.domain)
curr = Site.objects.get_current()
resp = self.client.get('/', HTTP_HOST=site.domain)

if get_version() >= '1.8':
curr = Site.objects.get_current(resp.context['request'])
else:
curr = Site.objects.get_current()

self.assertEquals(curr.domain, site.domain)
self.assertEquals(curr.pk, site.pk)

Expand Down
2 changes: 1 addition & 1 deletion domains/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setup_hook(hook):

installed_hooks[hook_instance.attribute] = hook_instance
setattr(settings, hook_instance.attribute, hook_instance)
print("installed", installed_hooks)


def get_installed_hooks():
return installed_hooks
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def version():

if __name__ == '__main__':
setup(name=ROOT_PACKAGE,
description='An application that allows you to run many different sites on one Django instance',
description=('An application that allows you to run many '
'different sites on one Django instance'),
author='Mikhail Porokhovnichenko',
author_email='marazmiki@gmail.com',
version=version(),
Expand All @@ -44,5 +45,4 @@ def version():
'Environment :: Web Environment',
'Programming Language :: Python',
'Framework :: Django'
],
)
])
16 changes: 11 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'

if get_version() >= '1.6':
TEST_RUNNER='django.test.runner.DiscoverRunner'
TEST_RUNNER = 'django.test.runner.DiscoverRunner'


SETTINGS = dict(
Expand Down Expand Up @@ -61,18 +61,24 @@
'django.template.loaders.app_directories.Loader',
)


TEMPLATE_CONTEXT_PROCESSORS = (
'django.template.context_processors.request',
)

if get_version() >= '1.8':
SETTINGS['TEMPLATES'] = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': {
'loaders': TEMPLATE_LOADERS
'loaders': TEMPLATE_LOADERS,
'context_processors': TEMPLATE_CONTEXT_PROCESSORS,
},
},
]
}]

else:
SETTINGS['TEMPLATE_LOADERS'] = TEMPLATE_LOADERS
SETTINGS.update(TEMPLATE_LOADERS=TEMPLATE_LOADERS)


settings.configure(**SETTINGS)

Expand Down

0 comments on commit 517666d

Please sign in to comment.