Skip to content

Commit

Permalink
Merge branch 'release/2.0.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Apr 5, 2024
2 parents 1271b3e + 8c1a5ad commit a68b1c9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ jobs:
env:
DJANGO: ${{ matrix.django-version }}

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.0.1
with:
name: Python ${{ matrix.python-version }}
token: ${{ secrets.CODECOV_TOKEN }}
slug: erikvw/django-multisite2
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Release Notes
=============

unreleased
----------
- change from VlaueError to multisite specific exceptions
when getting default SITE_ID in development mode.

2.0.0
-----
- major refactor, drop support for lower than py 3.11 and DJ4.2
Expand Down
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
|actions| |codecov| |downloads| |maintainability| |black|
|pypi| |actions| |codecov| |downloads| |maintainability| |black|



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

.. code-block::
pip install django-multisite2 (no release yet)
pip install django-multisite2
Replace your ``SITE_ID`` in ``settings.py`` to:
Expand Down Expand Up @@ -102,6 +102,9 @@ settings.py::

Templates
---------

This feature has been removed in version 2.0.0.

If required, create template subdirectories for domain level templates (in a
location specified in settings.TEMPLATES['DIRS'].

Expand Down
4 changes: 4 additions & 0 deletions multisite/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class MultisiteSiteDoesNotExist(Exception):
pass


class MultisiteAliasDoesNotExist(Exception):
pass


class MultisiteCacheError(Exception):
pass

Expand Down
12 changes: 9 additions & 3 deletions multisite/middleware/dynamic_site_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from django.contrib.sites.models import SITE_CACHE, Site
from django.core import mail
from django.core.cache import caches
from django.core.exceptions import DisallowedHost
from django.core.exceptions import DisallowedHost, ObjectDoesNotExist
from django.db.models.signals import post_delete, post_init, pre_save
from django.http import HttpResponse, HttpResponsePermanentRedirect

from ..exceptions import (
MultisiteAliasDoesNotExist,
MultisiteError,
MultisiteInvalidHostError,
debug_check_status_code,
Expand Down Expand Up @@ -170,11 +171,16 @@ def get_development_alias(netloc) -> Alias:
try:
# Prefer the default SITE_ID
site_id = settings.SITE_ID.get_default()
except ValueError:
except MultisiteError:
# Fallback to the first Site object
alias = Alias.canonical.order_by("site")[0]
else:
alias = Alias.canonical.get(site=site_id)
try:
alias = Alias.canonical.get(site=site_id)
except ObjectDoesNotExist as e:
raise MultisiteAliasDoesNotExist(
f"Invalid default SITE_ID. See {settings}. Got `{e}` for SITE_ID=`{site_id}`."
)
return alias

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions multisite/tests/tests/test_site_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.test import TestCase

from multisite import SiteID
from multisite.exceptions import MultisiteError


class TestSiteID(TestCase):
Expand All @@ -11,8 +12,8 @@ def setUp(self):
self.site_id = SiteID()

def test_invalid_default(self):
self.assertRaises(ValueError, SiteID, default="a")
self.assertRaises(ValueError, SiteID, default=self.site_id)
self.assertRaises(MultisiteError, SiteID, default="a")
self.assertRaises(MultisiteError, SiteID, default=self.site_id)

def test_compare_default_site_id(self):
self.site_id = SiteID(default=self.site.id)
Expand Down
10 changes: 7 additions & 3 deletions multisite/threadlocals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from multisite.exceptions import MultisiteError


class SiteID(local):
"""
Expand All @@ -17,13 +19,15 @@ class SiteID(local):
when combined with the appropriate middleware.
"""

def __init__(self, default=None, *args, **kwargs):
def __init__(self, default: int | None = None, *args, **kwargs):
"""
``default``, if specified, determines the default SITE_ID,
if that is unset.
"""
if default is not None and not isinstance(default, int):
raise ValueError("%r is not a valid default." % default)
raise MultisiteError(
f"Invalid default value for SITE_ID. See settings.SITE_ID. Got `{default}`."
)
self.default = default
self.reset()

Expand Down Expand Up @@ -100,7 +104,7 @@ def reset(self):
def get_default(self):
"""Returns the default SITE_ID."""
if self.default is None:
raise ValueError("SITE_ID has not been set.")
raise MultisiteError("SITE_ID default has not been set. See settings.SITE_ID.")
return self.default


Expand Down

0 comments on commit a68b1c9

Please sign in to comment.