Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31802 closed Cleanup/optimization (fixed)

Add system check for SITE_ID.

Reported by: Gagan Deep Owned by: Parth Verma
Component: contrib.sites Version: dev
Severity: Normal Keywords: sites, cache
Cc: me@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The "sites" framework requires to set a SITE_ID to specify the database ID of the Site object associated with that particular settings file. From the documentation, it can be interpreted that it should be an integer. But, it can also be set to a string literal('1'). I know this is wrong, but the Django accepted it and worked with it anyway without showing any warning or error.

The problem arose with SITE_CACHE. It was not getting invalidated when the Site object is updated. The reason is the code which handles invalidation uses primary key of instance object which will be an integer. Therefore, the cache is never invalidated.

try:
    del SITE_CACHE[instance.pk]
except KeyError:
    pass

Please see complete code on GitHub.

Since working of an element of Django is dependent on this setting, I suggest that there be a validation test for SITE_ID.

Change History (7)

comment:1 by Adam Johnson, 4 years ago

Part of the problem here is that Site.objects.get(pk=some_string) works because the database uses [SQL implicit type conversion](https://adamj.eu/tech/2020/03/06/sql-implicit-type-conversion/) to cast the string, but we need a consistent value within Python.

If it's only the site cache that's affected we could make SITE_CACHE a subclass of dict that casts keys but that seems like overengineering when a system check would provide enough validation at the boundary.

Version 0, edited 4 years ago by Adam Johnson (next)

comment:2 by Mariusz Felisiak, 4 years ago

Summary: Validation for "SITE_ID" settingAdd system check for SITE_ID.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization
Version: 3.0master

... it can be interpreted that it should be an integer.

It's clearly stated in docs: "The ID, as an integer, ..." but I agree that we can add a system check, e.g.

sites.E003: ``SITE_ID`` must be an integer.

comment:3 by Parth Verma, 4 years ago

Owner: changed from nobody to Parth Verma
Status: newassigned

comment:4 by Mariusz Felisiak, 4 years ago

Has patch: set
Needs documentation: set
Needs tests: set

comment:5 by Mariusz Felisiak, 4 years ago

Needs documentation: unset
Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 41065cf:

Fixed #31802 -- Added system check for non-integer SITE_ID.

comment:7 by Gagan Deep, 4 years ago

It's clearly stated in ​docs: "The ID, as an integer, ..."

I must have overlooked that. Thanks for bringing it. :)

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