def test_shared_site_template(self): with patch('cms_templates.settings.shared_sites') as mock: mock.return_value = ['SHARED_SITE'] shared_site = Site.objects.create(domain="shared_site.org", name="SHARED_SITE") site1 = Site.objects.create(domain="site1.org", name="site1") settings.__class__.SITE_ID = make_tls_property() settings.__class__.SITE_ID.value = site1.id site2 = Site.objects.create(domain="site2.org", name="site2") t1_shared = Template.objects.create(name='shared.html', content='shared') t1_shared.sites.clear() t1_shared.sites.add(shared_site) t1_s1 = Template.objects.create(name='site1.html', content='site1') t1_s1.sites.clear() t1_s1.sites.add(site1) t2_s2 = Template.objects.create(name='site2.html', content='site2') t2_s2.sites.clear() t2_s2.sites.add(site2) tpl = loader.get_template('site1.html') self.assertEqual(tpl.render(Context({})), 'site1') # test that template assigned to the shared site (SHARED_SITE) # is available for site1 tpl = loader.get_template('shared.html') self.assertEqual(tpl.render(Context({})), 'shared')
def test_orphan(self): #test that the orphan site can be loaded site1 = Site.objects.create(domain="site1.org", name="site1") settings.__class__.SITE_ID = make_tls_property() settings.__class__.SITE_ID.value = site1.id t_orphan = Template.objects.create(name='orphan.html', content='orphan') template = loader.get_template('orphan.html') self.assertEqual(template.render(Context({})), 'orphan')
def setUp(self): self.site = Site.objects.create(domain="test.org", name="test") settings.__class__.SITE_ID = make_tls_property() settings.__class__.SITE_ID.value = self.site.id self.page_template = Template.objects.create( name="page_template", content="must not be empty") self.page = Page.objects.create(template="page_template", site=self.site) self.parent_template = Template.objects.create( name="parent", content="must not be empty") self.child_template = Template.objects.create( name="child", content="{% extends 'parent' %}") self.parent_template2 = Template.objects.create( name="parent2", content="must not be empty") self.child_template2 = Template.objects.create( name="child2", content="{% extends 'parent2' %}") self.child_template3 = Template.objects.create( name="child3", content="{% extends 'parent2' %}") self.page2 = Page.objects.create(template="parent2", site=self.site)
def test_shared_template_assigned_also_to_another_site(self): #test that no exception is raised because the shared template belongs #to both shared site and site1 with patch('cms_templates.settings.shared_sites') as mock: mock.return_value = ['SHARED_SITE'] shared_site = Site.objects.create(domain="shared_site.org", name="SHARED_SITE") site1 = Site.objects.create(domain="site1.org", name="site1") settings.__class__.SITE_ID = make_tls_property() settings.__class__.SITE_ID.value = site1.id t1_shared = Template.objects.create(name='shared.html', content='shared') #shared template belongs to both sites t1_shared.sites.clear() t1_shared.sites.add(shared_site, site1) t1_s1 = Template.objects.create(name='site1.html', content='site1') t1_s1.sites.clear() t1_s1.sites.add(site1) tpl = loader.get_template('shared.html') self.assertEqual(tpl.render(Context({})), 'shared')
from django.conf import settings from django.core.cache import cache from django.contrib.sites.models import Site from djangotoolbox.utils import make_tls_property _default_site_id = getattr(settings, 'SITE_ID', None) SITE_ID = settings.__class__.SITE_ID = make_tls_property() class DynamicSiteIDMiddleware(object): """Sets settings.SITE_ID based on request's domain""" def process_request(self, request): # Ignore port if it's 80 or 443 if ':' in request.get_host(): domain, port = request.get_host().split(':') if int(port) not in (80, 443): domain = request.get_host() else: domain = request.get_host().split(':')[0] # Domains are case insensitive domain = domain.lower() # We cache the SITE_ID cache_key = 'Site:domain:%s' % domain site = cache.get(cache_key) if site: SITE_ID.value = site else: try: site = Site.objects.get(domain=domain) except Site.DoesNotExist:
from django.http import Http404 from django.core.urlresolvers import resolve from django.db.models import Q from djangotoolbox.utils import make_tls_property from djangotoolbox.sites.dynamicsite import DynamicSiteIDMiddleware from django.contrib.sites.models import Site from django.utils.cache import patch_vary_headers from dbtemplates.models import Template from cms.models import Page from cms.utils.permissions import get_user_sites_queryset from settings import include_orphan logger = logging.getLogger(__name__) CMS_TEMPLATES = settings.__class__.CMS_TEMPLATES = make_tls_property() CMS_TEMPLATE_INHERITANCE_TITLE = 'Inherit the template of the nearest ancestor' class SiteIDPatchMiddleware(object): """ This middleware works together with DynamicSiteIDMiddleware from djangotoolbox and patches the site_id based on the django-cms cms_admin_site session variable if in admin or based on the domain by falling back on DynamicSiteIDMiddleware. """ fallback = DynamicSiteIDMiddleware() def process_request(self, request): # Use cms_admin_site session variable to guess on what site # the user is trying to edit stuff. session_site_id = request.session.get('cms_admin_site', None)
from django.conf import settings from django.core.cache import cache from django.contrib.sites.models import Site from djangotoolbox.utils import make_tls_property _default_site_id = getattr(settings, 'SITE_ID', None) SITE_ID = settings.__class__.SITE_ID = make_tls_property() class DynamicSiteIDMiddleware(object): """Sets settings.SITE_ID based on request's domain""" def process_request(self, request): # Ignore port if it's 80 or 443 if ':' in request.get_host(): domain, port = request.get_host().split(':') if int(port) not in (80, 443): domain = request.get_host() else: domain = request.get_host().split(':')[0] # Domains are case insensitive domain = domain.lower() # We cache the SITE_ID cache_key = 'Site:domain:%s' % domain site = cache.get(cache_key) if site: SITE_ID.value = site else: try: site = Site.objects.get(domain=domain)
from django.http import HttpResponsePermanentRedirect, Http404 from django.shortcuts import render_to_response from django.utils.cache import patch_vary_headers from django.utils.http import urlquote from django.utils.importlib import import_module from djangotoolbox.utils import make_tls_property import logging import os import sys from django.utils._os import safe_join from django.utils.importlib import import_module from django.utils import six SITE_ID = settings.__dict__['_wrapped'].__class__.SITE_ID = make_tls_property() TEMPLATE_DIRS = settings.__dict__['_wrapped'].__class__.TEMPLATE_DIRS = make_tls_property(settings.TEMPLATE_DIRS) #TEMPLATE_LOADERS = settings.__dict__['_wrapped'].__class__.TEMPLATE_LOADERS = make_tls_property(settings.TEMPLATE_LOADERS) #STATIC_ROOT = settings.__dict__['_wrapped'].__class__.STATIC_ROOT = make_tls_property() class DynamicSitesMiddleware(object): """ Sets settings.SITE_ID based on request's domain. Also handles hostname redirects, and ensures the proper subdomain is requested for the site """ def process_request(self, request): self.logger = logging.getLogger(__name__) #check in the cache first hostname_redirects = cache.get('hostname_redirects')
from django.http import HttpResponsePermanentRedirect, Http404 from django.shortcuts import render_to_response from django.utils.cache import patch_vary_headers from django.utils.http import urlquote from django.utils.importlib import import_module from djangotoolbox.utils import make_tls_property import logging import os import sys from django.utils._os import safe_join from django.utils.importlib import import_module from django.utils import six SITE_ID = settings.__dict__['_wrapped'].__class__.SITE_ID = make_tls_property() TEMPLATE_DIRS = settings.__dict__[ '_wrapped'].__class__.TEMPLATE_DIRS = make_tls_property( settings.TEMPLATE_DIRS) #TEMPLATE_LOADERS = settings.__dict__['_wrapped'].__class__.TEMPLATE_LOADERS = make_tls_property(settings.TEMPLATE_LOADERS) #STATIC_ROOT = settings.__dict__['_wrapped'].__class__.STATIC_ROOT = make_tls_property() class DynamicSitesMiddleware(object): """ Sets settings.SITE_ID based on request's domain. Also handles hostname redirects, and ensures the proper subdomain is requested for the site """ def process_request(self, request): self.logger = logging.getLogger(__name__)