Code

Opened 4 years ago

Closed 4 years ago

#12063 closed (wontfix)

Ensure SITE_CACHE uses integers as key values

Reported by: patrys Owned by: nobody
Component: Contrib apps Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Currently django.contrib.sites.models.SiteManager.get_current just takes whatever value SITE_ID is set to and shoves it into the cache. This leads to strange results if you replace the integer value with a proxy object that implements __int__().

Whenever you want to return a different value at runtime (say you want to serve different content depending on the time of day or the hostname), you currently have to clear the cache as you end up with the following dict:

SITE_CACHE = {
    <Your proxy object>: <The first Site object ever used>,
}

The second time Django will just use the cache (as the proxy is equal to itself even if the value returned by __int__() changed).

Yes, I know altering settings at run time is not really encouraged but being able to proxy SITE_ID and alter the value using middleware is useful when dealing with dynamic subdomains (like customer1.example.com) and the change is both tiny and backwards-compatible.

Attachments (1)

django-site-cache.patch (678 bytes) - added by patrys 4 years ago.
Same patch in svn diff format

Download all attachments as: .zip

Change History (2)

Changed 4 years ago by patrys

Same patch in svn diff format

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

I'm not convinced that a dynamic SITE_ID is the right way to tackle this class of problem. I'm going to mark this wontfix; please raise this on django-dev if you want to argue your position.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.