Browse files

SITES_FILTER feature for restricting sites accessible from the project

  • Loading branch information...
1 parent 62c2b47 commit 1f2a5211198731227986b0cc4a81f61fe67c0fb6 @wardi committed Mar 28, 2012
Showing with 21 additions and 3 deletions.
  1. +3 −0 README.rst
  2. +8 −2 dynamicsiteslite/middleware.py
  3. +9 −1 example/fixtures/initial_data.json
  4. +1 −0 example/settings.py
View
3 README.rst
@@ -59,6 +59,9 @@ Configuration
8. make a site dir for each site you're hosting (eg. ``mkdir sites/www_mysitesdomain_com``) <-- put underscores instead of dots in the domain name, these need to be imported as python packages. Make sure to put an ``__init__.py`` file in each site dir as well.
+ 9. add a SITES_FILTER setting if you want to restrict the sites served by this project. SITES_FILTER is a dict used as follows when dynamicsiteslite looks up sites in the database::
+
+ Site.objects.filter(**SITES_FILTER)
Debugging
---------
View
10 dynamicsiteslite/middleware.py
@@ -176,7 +176,7 @@ def lookup(self):
site_id,
cache_key)
try:
- self.site = Site.objects.get(id=site_id)
+ self.site = all_sites().get(id=site_id)
SITE_ID.value = site_id
except Site.DoesNotExist:
# This might happen if the Site object was deleted from the
@@ -191,7 +191,7 @@ def lookup(self):
self.logger.debug(
'Checking database for domain=%s',
self.domain)
- self.site = Site.objects.get(domain=self.domain)
+ self.site = all_sites().get(domain=self.domain)
except Site.DoesNotExist:
return False
if not self.site:
@@ -281,3 +281,9 @@ def site_folder_name(site):
Just a simple implementation to start.
"""
return site.domain.replace('.', '_')
+
+def all_sites():
+ """
+ Apply optional SITES_FILTER setting to Site.objects
+ """
+ return Site.objects.filter(**getattr(settings, 'SITES_FILTER', {}))
View
10 example/fixtures/initial_data.json
@@ -22,5 +22,13 @@
"domain": "host3.example.com",
"name": "Host 3"
}
+ },
+ {
+ "pk": 4,
+ "model": "sites.site",
+ "fields": {
+ "domain": "www.anotherhost.tld",
+ "name": "This one is filtered out by SITES_FILTER"
+ }
}
-]
+]
View
1 example/settings.py
@@ -162,3 +162,4 @@
HOSTNAME_REDIRECTS = {
'redirect1.example.com': 'host3.example.com',
}
+SITES_FILTER = {'domain__endswith':'.example.com'}

0 comments on commit 1f2a521

Please sign in to comment.