I wanted to know if your patch can help with sub-domains. It's hard for me
to tell with a diff file.


On Wed, Dec 24, 2008 at 1:25 AM, Spike^ekipS <spikeek...@gmail.com> wrote:

>
> I also tried to find the solution to handle multiple domain without
> using django site framework, but i did not find anything, so i made a
> small patches to implement it. here is the usage,
>
> urls.py:
> urlpatterns = patters("",
> ...........
>    url(r'^', include("example_com.urls"), site="^www.example.com$" ),
>    url(r'^', include("example_com1.urls"), site=lambda x : x ==
> "www.example1.com" ),
> ...........
> )
>
> you can use just regular expression or function in 'site' argument.
> here is the patch against django 1.0.2
>
> ====================================================
> diff -uNr django/conf/urls/defaults.py django-patched/conf/urls/
> defaults.py
> --- django/conf/urls/defaults.py        2008-11-19 14:44:19.000000000 +0900
> +++ django-patched/conf/urls/defaults.py        2008-12-22
> 17:56:53.223489610
> +0900
> @@ -18,10 +18,10 @@
>         pattern_list.append(t)
>     return pattern_list
>
> -def url(regex, view, kwargs=None, name=None, prefix=''):
> +def url(regex, view, kwargs=None, name=None, prefix='', site=None):
>     if type(view) == list:
>         # For include(...) processing.
> -        return RegexURLResolver(regex, view[0], kwargs)
> +        return RegexURLResolver(regex, view[0], kwargs, site=site)
>     else:
>         if isinstance(view, basestring):
>             if not view:
> diff -uNr django/core/handlers/base.py django-patched/core/handlers/
> base.py
> --- django/core/handlers/base.py        2008-11-19 14:44:20.000000000 +0900
> +++ django-patched/core/handlers/base.py        2008-12-22
> 17:56:54.310996664
> +0900
> @@ -74,7 +74,7 @@
>         resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
>         try:
>             callback, callback_args, callback_kwargs =
> resolver.resolve(
> -                    request.path_info)
> +                    request.path_info, site=request.environ.get
> ("SERVER_NAME"))
>
>             # Apply view middleware
>             for middleware_method in self._view_middleware:
> diff -uNr django/core/urlresolvers.py django-patched/core/
> urlresolvers.py
> --- django/core/urlresolvers.py 2008-11-19 14:44:21.000000000 +0900
> +++ django-patched/core/urlresolvers.py 2008-12-22 17:56:54.439792164
> +0900
> @@ -7,7 +7,7 @@
>     (view_function, function_args, function_kwargs)
>  """
>
> -import re
> +import re, types
>
>  from django.http import Http404
>  from django.core.exceptions import ImproperlyConfigured,
> ViewDoesNotExist
> @@ -138,13 +138,19 @@
>     callback = property(_get_callback)
>
>  class RegexURLResolver(object):
> -    def __init__(self, regex, urlconf_name, default_kwargs=None):
> +    def __init__(self, regex, urlconf_name, default_kwargs=None,
> site=None):
>         # regex is a string representing a regular expression.
>         # urlconf_name is a string representing the module containing
> urlconfs.
>         self.regex = re.compile(regex, re.UNICODE)
>         self.urlconf_name = urlconf_name
>         self.callback = None
>         self.default_kwargs = default_kwargs or {}
> +
> +        if type(site) is types.FunctionType :
> +            self.site_pattern = site
> +        elif type(site) is (str, unicode, ) :
> +            self.site_pattern = lambda x : bool(re.compile(site and
> site or ".*").search(x))
> +
>         self._reverse_dict = MultiValueDict()
>
>     def __repr__(self):
> @@ -171,12 +177,18 @@
>         return self._reverse_dict
>     reverse_dict = property(_get_reverse_dict)
>
> -    def resolve(self, path):
> +    def resolve(self, path, site=None):
>         tried = []
>         match = self.regex.search(path)
>         if match:
>             new_path = path[match.end():]
>             for pattern in self.urlconf_module.urlpatterns:
> +                _site_pattern = None
> +                if hasattr(pattern, "site_pattern") :
> +                    _site_pattern = pattern.site_pattern
> +                if site and hasattr(pattern, "site_pattern") and not
> pattern.site_pattern(site) :
> +                    continue
> +
>                 try:
>                     sub_match = pattern.resolve(new_path)
>                 except Resolver404, e:
> @@ -200,7 +212,10 @@
>     urlconf_module = property(_get_urlconf_module)
>
>     def _get_url_patterns(self):
> -        return self.urlconf_module.urlpatterns
> +        p = self.urlconf_module.urlpatterns
> +        p.site_pattern = self.site_pattern
> +        return p
> +
>     url_patterns = property(_get_url_patterns)
>
>     def _resolve_special(self, view_type):
> diff -uNr django/http/__init__.py django-patched/http/__init__.py
> --- django/http/__init__.py     2008-11-19 14:44:05.000000000 +0900
> +++ django-patched/http/__init__.py     2008-12-22 17:56:54.566997983
> +0900
> @@ -48,11 +48,21 @@
>         if 'HTTP_X_FORWARDED_HOST' in self.META:
>             host = self.META['HTTP_X_FORWARDED_HOST']
>         elif 'HTTP_HOST' in self.META:
> -            host = self.META['HTTP_HOST']
> +            _host = self.META['HTTP_HOST'].split(":")
> +            if hasattr(settings, "DEFAULT_PORT") :
> +                host = "%s:%d" % (_host[0], settings.DEFAULT_PORT, )
> +            else :
> +                host = self.META['HTTP_HOST'].split(":")
> +
> +            return host
>         else:
>             # Reconstruct the host using the algorithm from PEP 333.
>             host = self.META['SERVER_NAME']
> -            server_port = str(self.META['SERVER_PORT'])
> +            if hasattr(settings, "DEFAULT_PORT") :
> +                server_port = settings.DEFAULT_PORT
> +            else :
> +                server_port = str(self.META['SERVER_PORT'])
> +
>             if server_port != (self.is_secure() and '443' or '80'):
>                 host = '%s:%s' % (host, server_port)
>         return host
> diff -uNr django/utils/http.py django-patched/utils/http.py
> --- django/utils/http.py        2008-11-19 14:44:21.000000000 +0900
> +++ django-patched/utils/http.py        2008-12-22 17:56:54.694497454 +0900
> @@ -94,3 +94,20 @@
>         i = i % j
>         factor -= 1
>     return ''.join(base36)
> +
> +import re
> +RE_HOST_AND_PORT = re.compile(":(\d+)$")
> +def split_domain (s, with_port=False) :
> +    o = [i for i in RE_HOST_AND_PORT.split(s) if i.strip()]
> +    if len(o) < 2 :
> +        o = o + ([None, ] * (2 - len(o)))
> +
> +    if with_port and not o[1] :
> +        o[1] = "80"
> +
> +    return tuple(o)
> +
> +def normalize_domain (s) :
> +    return ":".join(split_domain(s, with_port=True, ))
> +
> +
> ====================================================
>
>
>
> On 12월23일, 오후12시30분, Dan Ger <kickme...@gmail.com> wrote:
> > Hi, I have searched to no avail and decided to post here.
> >
> > I have a django app that is going to have multiple domain names and
> > would very much like to use the domain name in the urls.py to help
> > with routing.  I have been unable to find a built in way of using this
> > and wanted to see if anyone else is doing this before i get into the
> > urls internals and hack it up.  I would like to do something like
> > this:
> >
> > (r'^something.com/foo', include('something.urls')),
> >
> > Can anyone point me in the right direction?  Thanks much!
> >
>

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

Reply via email to