Skip to content

Commit

Permalink
Merge branch 'release/2.0.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Mar 31, 2024
2 parents 3640423 + 8fd473c commit 1271b3e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.rst → CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
Release Notes
=============

2.0.1
-----
- change rel.to to related_model (Django 4.2)
2.0.0
-----
- major refactor, drop support for lower than py 3.11 and DJ4.2
- drop pytest, use unitests / runtests.py
- replace rel.to with related_model (Django 4.2)
- remove deprecated `process_requests`
- add more specific exception handling
- add MULTISITE_REGISTER_POST_MIGRATE_SYNC_ALIAS settings attr
to allow user to connect signal outside of multisite
- drop template loader code. Maybe add back later
- remove unused ThreadLocalsMiddleware

1.9.0
-----
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
|pypi| |actions| |codecov| |downloads| |maintainability| |black|
|actions| |codecov| |downloads| |maintainability| |black|



Expand All @@ -19,7 +19,7 @@ Install with pip:

.. code-block::
pip install django-multisite2
pip install django-multisite2 (no release yet)
Replace your ``SITE_ID`` in ``settings.py`` to:
Expand Down Expand Up @@ -198,7 +198,7 @@ To run the tests::


.. |pypi| image:: https://img.shields.io/pypi/v/django-multisite2.svg
:target: https://pypi.python.org/pypi/django-multisite2
:target: https://pypi.python.org/pypi/django-multisite2

.. |actions| image:: https://github.com/erikvw/django-multisite2/actions/workflows/build.yml/badge.svg
:target: https://github.com/erikvw/django-multisite2/actions/workflows/build.yml
Expand Down
25 changes: 14 additions & 11 deletions multisite/middleware/dynamic_site_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def __init__(self, get_response):
post_delete.connect(self.site_deleted_hook, sender=Site)

def __call__(self, request):
response = self.get_response(request)
# if 500 here -> Site ObjectDoesNotExist was raised by get_current_site()
debug_check_status_code(response, request=request)
# debug_check_status_code(response, request=request)
try:
netloc = request.get_host().lower()
except DisallowedHost as e:
Expand All @@ -66,7 +65,7 @@ def __call__(self, request):
# found Alias
self.cache.set(cache_key, alias)
settings.SITE_ID.set(alias.site_id)
response = self.redirect_to_canonical(request, response, alias)
response = self.redirect_to_canonical(request, alias)
elif (alias := self.get_alias(netloc)) is None:
# Cache missed, fallback using settings.MULTISITE_FALLBACK
debug_raise_cache_missed_exception(netloc, alias)
Expand All @@ -77,17 +76,21 @@ def __call__(self, request):
self.cache.set(cache_key, alias)
settings.SITE_ID.set(alias.site_id)
SITE_CACHE[settings.SITE_ID] = alias.site # Pre-populate SITE_CACHE
response = self.redirect_to_canonical(request, response, alias)
debug_check_status_code(response, netloc=netloc, alias=alias, site=alias.site)
return response
response = self.redirect_to_canonical(request, alias)
# debug_check_status_code(response, netloc=netloc, alias=alias, site=alias.site)
return response or self.get_response(request)

@staticmethod
def redirect_to_canonical(request, response, alias: Alias) -> HttpResponse:
def redirect_to_canonical(request, alias: Alias) -> HttpResponse | None:
if not alias.redirect_to_canonical or alias.is_canonical:
return response
url = urlsplit(request.build_absolute_uri(request.get_full_path()))
url = urlunsplit((url.scheme, alias.site.domain, url.path, url.query, url.fragment))
return HttpResponsePermanentRedirect(url)
response = None
else:
url = urlsplit(request.build_absolute_uri(request.get_full_path()))
url = urlunsplit(
(url.scheme, alias.site.domain, url.path, url.query, url.fragment)
)
response = HttpResponsePermanentRedirect(url)
return response

def get_cache_key(self, netloc):
"""Returns a cache key based on ``netloc``."""
Expand Down
9 changes: 8 additions & 1 deletion multisite/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Callable

from django.apps import apps as django_apps
Expand Down Expand Up @@ -89,12 +90,18 @@ def create_or_sync_missing_canonical_from_site_domain(apps: AppConfig | None = N
create_or_sync_alias_from_site(site=site, apps=apps)


def create_or_sync_canonical_from_all_sites(apps: AppConfig | None = None) -> None:
def create_or_sync_canonical_from_all_sites(
apps: AppConfig | None = None, verbose: bool | None = None
) -> None:
"""Create or sync canonical Alias objects from all Site objects.
Renamed canonical manager method ``sync_all``.
"""
if verbose:
sys.stdout.write(" * Syncing canonical from site domain ... \n")
sync_canonical_from_site_domain(apps=apps)
if verbose:
sys.stdout.write(" * Syncing missing canonical from site domain ... \n")
create_or_sync_missing_canonical_from_site_domain(apps=apps)


Expand Down

0 comments on commit 1271b3e

Please sign in to comment.