Opened 6 years ago
Last modified 6 years ago
#30021 new New feature
Feature request to allow "mixed mode" Sites operation. — at Initial Version
Reported by: | Ira Abbott | Owned by: | nobody |
---|---|---|---|
Component: | contrib.sites | Version: | 2.1 |
Severity: | Normal | Keywords: | |
Cc: | Ira Abbott | Triage Stage: | Someday/Maybe |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Thank you for taking the time to review my request.
All the best, and thank you for your work with django.
Manually edited 'change set' describing the proposed change (three lines) follow:
django/contrib/sites/models.py
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(). TheSite
object is cached the first time it's
retrieved from the database.
"""
from django.conf import settings
-- if getattr(settings, 'SITE_ID', ):
++ if getattr(settings, 'SITE_ID', ) and not getattr(settings, 'SITE_MIXED_MODE'):
site_id = settings.SITE_ID
return self._get_site_by_id(site_id)
elif request:
return self._get_site_by_request(request)
++ elif getattr(settings, 'SITE_MIXED_MODE') and settings.SITE_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."
)