-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alberto Paro
committed
Jun 24, 2010
1 parent
dd78bd8
commit 92d73a9
Showing
3 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django import forms | ||
from django.contrib import admin | ||
from django.contrib.auth.admin import UserAdmin | ||
from django.contrib.auth.models import User, Group | ||
|
||
class UserForm(forms.ModelForm): | ||
class Meta: | ||
model = User | ||
fields = ('username', 'email', 'first_name', 'last_name', 'is_active', | ||
'is_staff', 'is_superuser') | ||
|
||
class CustomUserAdmin(UserAdmin): | ||
fieldsets = None | ||
form = UserForm | ||
|
||
admin.site.unregister(User) | ||
admin.site.unregister(Group) | ||
admin.site.register(User, CustomUserAdmin) |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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.SIDE_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: | ||
site = None | ||
|
||
if not site: | ||
# Fall back to with/without 'www.' | ||
if domain.startswith('www.'): | ||
fallback_domain = domain[4:] | ||
else: | ||
fallback_domain = 'www.' + domain | ||
|
||
try: | ||
site = Site.objects.get(domain=fallback_domain) | ||
except Site.DoesNotExist: | ||
site = None | ||
|
||
# Add site if it doesn't exist | ||
if not site and getattr(settings, 'CREATE_SITES_AUTOMATICALLY', | ||
True): | ||
site = Site(domain=domain, name=domain) | ||
site.save() | ||
|
||
# Set SITE_ID for this thread/request | ||
if site: | ||
SITE_ID.value = site.pk | ||
else: | ||
SITE_ID.value = _default_site_id | ||
|
||
cache.set(cache_key, SITE_ID.value, 5*60) |