Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions django/contrib/sites/checks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
from types import NoneType

from django.conf import settings
from django.core.checks import Error
from django.core.exceptions import ValidationError


def check_site_id(app_configs, **kwargs):
if hasattr(settings, "SITE_ID") and not isinstance(
settings.SITE_ID, (NoneType, int)
):
return [
Error("The SITE_ID setting must be an integer", id="sites.E101"),
]
# Inner import avoids AppRegistryNotReady
from django.contrib.sites.models import Site

if hasattr(settings, "SITE_ID"):
try:
site_id = Site._meta.pk.to_python(settings.SITE_ID)
except ValidationError as exc:
return [
Error(
f"The SITE_ID setting failed to validate: {exc}.", id="sites.E101"
),
]
else:
# to_python() might coerce a SITE_ID of the wrong type to the valid
# type, e.g. "1" to 1 for AutoField.
if site_id != settings.SITE_ID:
expected_type = type(site_id).__name__
return [
Error(
f"The SITE_ID setting must be of type {expected_type}.",
id="sites.E101",
),
]
return []
23 changes: 10 additions & 13 deletions django/core/serializers/pyyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,34 @@


class DjangoSafeDumper(SafeDumper):
# The "safe" serializer is used for better interoperability.

def represent_decimal(self, data):
return self.represent_scalar("tag:yaml.org,2002:str", str(data))

def represent_ordered_dict(self, data):
return self.represent_mapping("tag:yaml.org,2002:map", data.items())

def represent_time(self, data):
# Base YAML doesn't support serialization of time types (as opposed to
# dates or datetimes, which it does support). Converting them to
# strings isn't perfect, but it's better than a "!!python/time" type
# which would prevent deserialization under any other language.
return self.represent_scalar("tag:yaml.org,2002:str", str(data))


DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)
DjangoSafeDumper.add_representer(
collections.OrderedDict, DjangoSafeDumper.represent_ordered_dict
)
DjangoSafeDumper.add_representer(datetime.time, DjangoSafeDumper.represent_time)


class Serializer(PythonSerializer):
"""Convert a queryset to YAML."""

internal_use_only = False

def _value_from_field(self, obj, field):
# A nasty special case: base YAML doesn't support serialization of time
# types (as opposed to dates or datetimes, which it does support).
# Since we want to use the "safe" serializer for better
# interoperability, we need to do something with those pesky times.
# Converting 'em to strings isn't perfect, but it's better than a
# "!!python/time" type which would halt deserialization under any other
# language.
value = super()._value_from_field(obj, field)
if isinstance(value, datetime.time):
value = str(value)
return value

def end_serialization(self):
self.options.setdefault("allow_unicode", True)
yaml.dump(
Expand Down
3 changes: 2 additions & 1 deletion docs/ref/checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,8 @@ The following checks are performed on any model using a
The following checks verify that :mod:`django.contrib.sites` is correctly
configured:

* **sites.E101**: The :setting:`SITE_ID` setting must be an integer.
* **sites.E101**: The :setting:`SITE_ID` setting must be of type ``<type>``.
*or* The :setting:`SITE_ID` setting failed to validate: ``<error>``.

``staticfiles``
---------------
Expand Down
17 changes: 15 additions & 2 deletions tests/sites_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,25 @@ def test_site_natural_key(self):
self.assertEqual(self.site.natural_key(), (self.site.domain,))

@override_settings(SITE_ID="1")
def test_check_site_id(self):
def test_check_site_id_incorrect_type(self):
self.assertEqual(
check_site_id(None),
[
checks.Error(
msg="The SITE_ID setting must be an integer",
msg="The SITE_ID setting must be of type int.",
id="sites.E101",
),
],
)

@override_settings(SITE_ID="x")
def test_check_site_id_failed_to_validate(self):
self.assertEqual(
check_site_id(None),
[
checks.Error(
msg="The SITE_ID setting failed to validate: ['“x” value "
"must be an integer.'].",
id="sites.E101",
),
],
Expand Down
Loading