We all learn very early in playing with django to set a site and a 
SITE_ID.  Once we operate as multiple sites, we either use the multiple 
processes, each configured with a separate SITE_ID or we can now pass a 
request in.  However,  I assume for backward compatibility, SITE_ID must be 
removed.  This allows all sites which specify SITE_ID to operate as if the 
addition of passing request was not added.  However, removing SITE_ID to 
allow get_site_by_request breaks all applications which do NOT pass 
request, because there is no SITE_ID.

I propose a setting, which, when set to True in addition to setting 
SITE_ID, causes request to take precedence over SITE_ID.  This will allow 
applications using both paradigms to coexist without modification - sites 
which pass request use get_site_by_request as if SITE_ID had not been 
defined and calls passing no request get SITE_ID.  When the new setting is 
not present, all behavior is backward compatible.

Essentially, I am proposing something like the 'change set' I typed in 
below - I considered that a complex and/or in the fist if could cover all 
of the conditions, but I think qualifying the first line with the new 
setting and adding the elif to cover the cleanup case of not set to True is 
easier to understand - especially in light of understanding the the 
backward compatibility aspect of the change.  The version of this change I 
am currently running simply defines a new SITE_DEFAULT_ID to catch the 
third case, but it occurred to me that some modules may expect SITE_ID 
directly for some reason, and MIXED_MODE has them covered too.  

I expect the documentation change to accompany the new setting would go 
with the Sites documentation next to SITE_ID an explanation of removing 
SITE_ID to use request.

This would be my first contribution, so I am unsure that it would welcome.  
If this ticket is accepted, I will quite gladly prepare a pull request with 
an agreed upon setting name (in case mine doesn't grab ya) or other any 
other suggested style improvements etc.

 def get_current(self, request=None):
        """
        Return the current Site based on the SITE_ID in the project's settings.
        If SITE_ID isn't defined, return the site with domain matching
        request.get_host(). The ``Site`` object is cached the first time it's
        retrieved from the database.
        """
        from django.conf import settings
        site_id = getattr(settings, 'SITE_ID', '')
        mixed_mode = getattr(settings, 'SITE_MIXED_MODE', '')
        if site_id and not mixed_mode:
            return self._get_site_by_id(site_id)
        elif request:
            return self._get_site_by_request(request)
        elif site_id and mixed_mode == True:
            return self._get_site_by_id(site_id)

        raise ImproperlyConfigured(
            "You're using the Django \"sites framework\" without having "
            "set the SITE_ID setting. Create a site in your database and "
            "set the SITE_ID setting or pass a request to "
            "Site.objects.get_current() to fix this error."
        )

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/2af3d7a7-98c7-4a38-966d-b2c57da3329a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to