Opened 20 months ago

Last modified 20 months ago

#30021 new New feature

Allow contrib.sites to use the request host and fallback to a SITE_ID

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 (last modified by Tim Graham)

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(). 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 = getattr(settings, 'SITE_MIXED_MODE', '')
--        if getattr(settings, 'SITE_ID', ''):
++     if site_id and not mixed:
--          site_id = settings.SITE_ID
            return self._get_site_by_id(site_id)
            elif request:
                 return self._get_site_by_request(request)
++       elif site_id and mixed == 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."
)

Change History (16)

comment:1 Changed 20 months ago by Ira Abbott

Needs documentation: set

comment:2 Changed 20 months ago by Ira Abbott

Cc: Ira Abbott added

comment:3 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:4 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:5 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:6 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:7 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:8 Changed 20 months ago by Ira Abbott

Sorry for the churn. I proposed something I am hoping is more elegant and in line than my current version. After I typed it, I saw that it needed a little splatter-proofing.

comment:9 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:10 Changed 20 months ago by Ira Abbott

Description: modified (diff)

comment:11 Changed 20 months ago by Ira Abbott

After more tweaking and plugging it in for test:

    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."
        )
Last edited 20 months ago by Tim Graham (previous) (diff)

comment:12 Changed 20 months ago by Ira Abbott

I have tested the changed file with both with and without the new setting and verified that site by request happened with the setting, but that I got the base site without it. I also removed the app requiring site id, but left the one with request and verified that with no SITE_ID (simple switcheroo in the settings).

comment:13 Changed 20 months ago by Tim Graham

Description: modified (diff)

comment:14 Changed 20 months ago by Ira Abbott

Description: modified (diff)
Has patch: unset

comment:15 Changed 20 months ago by Tim Graham

Description: modified (diff)
Easy pickings: unset
Needs documentation: unset
Summary: Feature request to allow "mixed mode" Sites operation.Allow contrib.sites to use the request host and fallback to a SITE_ID
Triage Stage: UnreviewedSomeday/Maybe

Proposals for new settings must be made on the DevelopersMailingList.

comment:16 Changed 20 months ago by Ira Abbott

Thanks Tim. Given that I did not follow process, I will take "Someday/Maybe" as positive feedback.
I have copied the result here to DevelopersMailingList, waiting for the topic's approval.
Thank you for your consideration and patience.

Note: See TracTickets for help on using tickets.
Back to Top