Skip to content

Commit

Permalink
[#630] Add _patch_setattr function to partner sites middleware
Browse files Browse the repository at this point in the history
...to get thread local settings to actually work

Without it the assignment to a thread local setting wont happen.

See https://gist.github.com/802020/b638e41fa8852d308468c02d07dd3e1365c33def
  • Loading branch information
zzgvh committed Aug 28, 2014
1 parent edc11db commit fec618c
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions akvo/rsr/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,33 @@ def _set_value(self, value):
return TLSProperty()


DEFAULT_SITE_ID = getattr(settings, "SITE_ID", None)
settings.__class__.SITE_ID = make_tls_property(DEFAULT_SITE_ID)
def _patch_setattr(obj):
"""Purpose of this is to allow changes to settings object again after it is
changed to tls property.
Without this patch the following is not possible::
settings.SITE_ID = 1
settings.SITE_ID = 42
assert settings.SITE_ID == 42 # this fails without this patch
"""
old_setattr = obj.__setattr__
def wrap_setattr(self, name, value):
try:
getattr(self.__class__, name).value = value
except AttributeError:
old_setattr(name, value)
obj.__class__.__setattr__ = wrap_setattr


_patch_setattr(settings)

DEFAULT_SITE_ID = getattr(settings, "SITE_ID", None)
DEFAULT_PARTNER_SITE = getattr(settings, "PARTNER_SITE", None)
settings.__class__.PARTNER_SITE = make_tls_property(DEFAULT_PARTNER_SITE)

settings.__class__.SITE_ID = make_tls_property(DEFAULT_SITE_ID)
settings.__class__.PARTNER_SITE = make_tls_property(DEFAULT_PARTNER_SITE)

PARTNER_SITES_MARKETING_SITE = getattr(
settings,
Expand Down

0 comments on commit fec618c

Please sign in to comment.