Standardize Site/RequestSite access; looser coupling of sites framework in contrib
|Reported by:||Gabriel Hurley||Owned by:||Gabriel Hurley|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
A few days ago I started this thread on the Django Developers list in order to solve a number of tickets at the same time. The gist:
All of these tickets relate to needing access to the current domain, whether that's through a Site instance, or through a RequestSite instance. To facilitate this, the provided patch adds a utility function,
django.contrib.sites.models.get_current_site which takes a request as an argument and returns either a Site or RequestSite instance.
Using that utility function, a lot of repetitious code could be removed and streamlined, and several tickets were trivial to resolve.
Included in this patch:
- Adds the
- Provides a note in the contrib.sites docs regarding the function.
- Converts to contrib.sites tests from doctests to unit tests and adds a test for
- Resolves #8960 by allowing
django.contrib.contenttypes.views.shortcutto use either a Site or RequesSite object.
- Converts contrib.contenttypes tests from doctests to unit tests and adds a test for #8960.
- Resolves #10235 and #10909 by using
get_current_sitein the Sitemaps framework.
- Adds test case for #10235/#10909.
django.contrib.syndication.viewsto use `get_current_site.
get_current_siteas per #10608.
The patch passes the full test suite under WinXP/Py2.6/SQLite.
This patch would completely fix #8960, #10235, and #10909. The remainder of #10608 is actually invalid, because the remaining places where contrib apps rely on contrib.sites the model has a direct relation to Site, or there is no way to access a request object to use RequestSite. Any further decoupling in contrib would require backwards-incompatible rewrites.