Re: Fwd: Different subdomain for each application (again)

2007-03-25 Thread ScottB

Hi Tom.

On Mar 24, 2:42 pm, Tom Smith <[EMAIL PROTECTED]> wrote:
> A while ago Doug and Eivind asked about using subdomains for each app.
>
> I'd like to do this, but in the example they had a stab at they were
> using Apache whereas I'm using Lighttpd...

One thing you might like to consider is having Lighty map the
subdomain in to the url where it can be matched against your
urlpatterns.

In the Lighty config you can use a regex to match the host.  If the
regex has captures, these are available to be passed to FastCGI using
%1, %2, etc (captures in the rewrite rule are $1, $2, etc).  So in the
Lighty config you can use something like:

$HTTP["host"] =~ "([\w\d-]+)?\.?example\.com" {
... snip ...
url.rewrite-once = (
... snip ...
"^(/.*)$" => "/extraset.fcgi/%1$1",
)
}

Now if you go to blog.example.com, the URL Django gets is /blog.
If you go to blog.example.com/2006/, Django gets /blog/2006/.

This makes it easy to match the subdomain in urlpatterns:

urlpatterns = patterns('',
(r'^blog/$', include('project.blog.urls'),
... etc

The blog app doesn't know anything about the prefix in the url and
doesn't need to know it is running on a subdomain.  The user never
sees the /blog prefix because this happens within the webserver.  I've
had this working, so let me know if you need any more details.

Scott


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Fwd: Different subdomain for each application (again)

2007-03-24 Thread Tom Smith

A while ago Doug and Eivind asked about using subdomains for each app.

I'd like to do this, but in the example they had a stab at they were  
using Apache whereas I'm using Lighttpd...


ideally I'd like subdomainone.wherever.com and  
subdomaintwo.wherever.com to pass through without anything AFTER the /.

I tried creating a root urls.py like this...

urlpatterns = patterns('',
(r'^/?$', 'app.views.main'),
(r'^admin/', include('django.contrib.admin.urls')),)

and a kinda main views.py like this...

def main(request):
#handle any calls
from django.conf.urls.defaults import *

server_name = request.META['SERVER_NAME'].split(".")
domain = server_name[0]

if domain == 'subdomainone': urlpatterns = patterns('',  (r'^/?$',  
include('seo.one.urls')),   )
if domain == 'subdomaintwo': urlpatterns = patterns('', (r'^/?$',  
include('seo.two.urls')),   )

. of course I can get the domain out of the SERVER_NAME, but then  
setting urlpatterns doesn't actually DO anything does it?

regards

tom









 

Tom Smith
http://www.theotherblog.com 
yahoo, aim, skype: everythingability
mob: +44 (0) 7720 288 285   
tel: +44 (0) 1904 870 565
fax: +44 (0) 8716 613 413
--- usability, findability, profitability, remarkability  
---



 

Tom Smith
http://www.everythingability.com
yahoo, aim, skype: everythingability
mob: +44 (0) 7720 288 285   
tel: +44 (0) 1904 870 565
fax: +44 (0) 8716 613 413
--- usability, findability, profitability, remarkability  
---



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Different subdomain for each application (again)

2007-03-23 Thread Michael Cuddy


I have exactly the same issue.  Here's how I dealt with it ...

First, the threadlocals module, this is used to keep track of the
site the user is browsing, once it's determined ...

 mware/threadlocals.py 
try:
from threading import local
except ImportError:
from django.utils._threading_local import local

__thread_locals = local()

class __NoValueGiven:
""" sentinel value class """
pass

def set(what,value):
setattr(__thread_locals,what,value)

def get(what, defval = __NoValueGiven):
if defval is __NoValueGiven:
# will raise AttributeError if local isn't set.
return getattr(__thread_locals,what)
else:
return getattr(__thread_locals,what,defval)

cut

The threadlocals recipe has been floating around for a while; the only thing
I added is to make it a dict, and then call out threadlocals by 'name'.

Next, I have an app with model which represents each of the valid companies;
mapping a URL/hostname to an object in the DB.

 company/models.py 

#
# $Id$
#
# File: company/models.py - define model for company objects.
#
from django.db import models
from django.db.models import Q
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.utils.translation import gettext_lazy as _
from django.conf import settings
import mware.threadlocals as locals

def get_current():
""" return Company object for current company. """
return locals.get('company')

def set_current(cur):
""" set current Company object. """
return locals.set('company', cur)

def set_current_by_domain(domain):
""" set current company object from domain. """
return set_current( Company.objects.get( domain = domain ) )

class Company(models.Model):
""" Object representing a theatre company and links the url 
.stagemgr.com to a company.
"""
# should be lower case.
domain = models.CharField(_("domain name"), unique = True, maxlength = 100 )

# name of theatre company
name = models.CharField(_("display name"), maxlength=50)

def save(self):
self.domain = self.domain.lower()
super(Company,self).save()

class Meta:
verbose_name = _('company')
verbose_name_plural = _('companies')
ordering = ('domain',)
class Admin:
list_display = ('domain', 'name')
search_fields = ('domain', 'name')

def __str__(self):
return self.domain

# set default company; default ID is set from settings (usually 1)
set_current(Company.objects.get(id = settings.SITE_ID))

 snip 

When the company.models module is imported, the last line, sets up the 
default -- this is good so that when you're using the shell, things work as
expected.

>From the shell you can use set_current_by_domain('foo.bar.com') to switch
companies.

Next, we have a managers.py which defines a CurrentCompanyManager class
which should be used on any models which should be access-controlled by URL:

 company/managers.py 
from django.db import models
from django.db.models.fields import FieldDoesNotExist
from company.models import get_current
import sys

class CurrentCompanyManager(models.Manager):
"Use this to limit objects to those associated with the current site."
def __init__(self, field_name='company'):
super(CurrentCompanyManager, self).__init__()
self.__field_name = field_name
self.__is_validated = False

def get_query_set(self):
if not self.__is_validated:
try:
self.model._meta.get_field(self.__field_name)
except FieldDoesNotExist:
raise ValueError, "%s couldn't find a field named %s in %s." % \
(self.__class__.__name__, self.__field_name, 
self.model._meta.object_name)
self.__is_validated = True
company = get_current().id
qset = super(CurrentCompanyManager, self).get_query_set()
# magic!
if company == 1:
return qset
return qset.filter(**{self.__field_name + '__id__exact': 
get_current().id })


-- snip 

By default, this manager uses the managed object's 'company' field as
the index field, but that can be changed when constructing the 
CurrentCompanyManager object.

This has a bit of magic in it -- if you're viewing objects from the 
URL assocaited with site-id == 1, you get all objects, otherwise, you
get objects filtered by the current company.
(now that I think about it, it should match against settings.SITE_ID, as
a magic #)

Here's an example object which uses the CurrentCompanyManager:

 
from django.db import models
from company.models import Company
from company.managers import CurrentCompanyManager

class Patron(models.Model):

objects = CurrentCompanyManager()
all = models.Manager()

company = models.ForeignKey(Company)

first_name = models.CharField(maxlength=80)
last_name = models.CharField(maxlength=80)

class Admin:
 

Different subdomain for each application (again)

2007-03-23 Thread Tom Smith
A while ago Doug and Eivind asked about using subdomains for each app.

I'd like to do this, but in the example they had a stab at they were  
using Apache whereas I'm using Lighttpd...


ideally I'd like subdomainone.wherever.com and  
subdomaintwo.wherever.com to pass through without anything AFTER the /.

I tried creating a root urls.py like this...

urlpatterns = patterns('',
(r'^/?$', 'app.views.main'),
(r'^admin/', include('django.contrib.admin.urls')),)

and a kinda main views.py like this...

def main(request):
#handle any calls
from django.conf.urls.defaults import *

server_name = request.META['SERVER_NAME'].split(".")
domain = server_name[0]

if domain == 'subdomainone': urlpatterns = patterns('',  (r'^/?$',  
include('seo.one.urls')),   )
if domain == 'subdomaintwo': urlpatterns = patterns('', (r'^/?$',  
include('seo.two.urls')),   )

. of course I can get the domain out of the SERVER_NAME, but then  
setting urlpatterns doesn't actually DO anything does it?

regards

tom









 

Tom Smith
http://www.theotherblog.com 
yahoo, aim, skype: everythingability
mob: +44 (0) 7720 288 285   
tel: +44 (0) 1904 870 565
fax: +44 (0) 8716 613 413
--- usability, findability, profitability, remarkability  
---



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Different subdomain for each application

2007-01-31 Thread Doug Van Horn

On Jan 31, 4:51 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> How would you go about using seperate subdomains for certain apps in a
> project? What I would like to do is something like this:
>
> urlpatterns = patterns('',
> (r'^weblog\.[a-z0-9-]+\.[a-z0-9-]{3}',
> include('mysite.blog.urls')),
> (r'^wiki\.[a-z0-9-]+\.[a-z0-9-]{3}', include('mysite.wiki.urls')),
> (r'^code\.[a-z0-9-]+\.[a-z0-9-]{3}', include('mysite.code.urls')),
> (r'^admin\.[a-z0-9-]+\.[a-z0-9-]{3}',
> include('django.contrib.admin.urls')),
> )
>
> This will of course not work as the matching is done against the file
> path and not the domainname.
>
> Cheers,
> Eivind Uggedal


I've thought about this too.  I haven't done anything but I think this
approach might work:

First, create the server aliases in the apache configuration for the
server, like:

ServerAlias code.example.com wiki.example.com admin.example.com

Then, use some middleware to 'inject' that sub-domain into the
request.path (is request.path writable?).  You might end up with:

__www__/
__wiki__/some/request/path/
__code__/another/path/

and url patterns like:

(r'^__www__/', include('apps.www.urls')),
(r'^__wiki__/', include('apps.wiki.urls')),
(r'^__code__/', include('apps.code.urls')),

That /seems/ like it should work.

I suppose you could also dig into the BaseHandler and the way it calls
the RegexURLResolver.resolve method, and pass it the full request
rather than just the path.  You'd probably need to change the
urlpatterns data structure, too.

Anyway, it's all just theory as I've never attempted any of this.  If
you give it a go, let us know as I like the idea of using sub-domains
rather than separate apache virtual servers.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Different subdomain for each application

2007-01-31 Thread [EMAIL PROTECTED]

How would you go about using seperate subdomains for certain apps in a
project? What I would like to do is something like this:

urlpatterns = patterns('',
(r'^weblog\.[a-z0-9-]+\.[a-z0-9-]{3}',
include('mysite.blog.urls')),
(r'^wiki\.[a-z0-9-]+\.[a-z0-9-]{3}', include('mysite.wiki.urls')),
(r'^code\.[a-z0-9-]+\.[a-z0-9-]{3}', include('mysite.code.urls')),
(r'^admin\.[a-z0-9-]+\.[a-z0-9-]{3}',
include('django.contrib.admin.urls')),
)

This will of course not work as the matching is done against the file
path and not the domainname.

Cheers,
Eivind Uggedal


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---