Skip to content

Instantly share code, notes, and snippets.

@lorinkoz
lorinkoz / tenant_prefixed_dynamic_urlconf.py
Created April 21, 2020 06:28
Variants for constructing a tenant prefixed dynamic URLConf module
#
# Executes the code in the string into the module
#
def get_from_code(urlconf, dynamic_path):
from types import ModuleType
DYNAMIC_MODULE_CODE = """
from {urlconf} import *
from {urlconf} import urlpatterns as original_urlpatterns
from django_pgschemas.urlresolvers import tenant_patterns

Keybase proof

I hereby claim:

  • I am lorinkoz on github.
  • I am lorinkoz (https://keybase.io/lorinkoz) on keybase.
  • I have a public key ASCJFKWZvfxQ7xxA-b3aVJs6Pp7-2zX8T3mbsqnzAufggwo

To claim this, I am signing this object:

@lorinkoz
lorinkoz / inject_request.py
Last active January 21, 2019 20:59
Decorator to inject Django request into a function
def inject_request(func):
def wrapper(*args, **kwargs):
request = None
frame = inspect.currentframe().f_back
while frame and not request:
for v_name, v_object in frame.f_locals.items():
if isinstance(v_object, HttpRequest):
request = v_object
break
frame = frame.f_back