From 0a81a88e939fd5a44501a6e4b1817af441c4f8c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Tue, 20 Nov 2018 00:22:43 +0100 Subject: [PATCH] Introduce wrapt and monkeypatch django.views.View so we have a setup method to override. (#279) --- src/bornhack/settings.py | 15 +++++++++++++++ src/requirements/production.txt | 2 ++ 2 files changed, 17 insertions(+) diff --git a/src/bornhack/settings.py b/src/bornhack/settings.py index 27d6caf2..cf3ca436 100644 --- a/src/bornhack/settings.py +++ b/src/bornhack/settings.py @@ -1,12 +1,27 @@ import os +import wrapt +import django.views + from .environment_settings import * + def local_dir(entry): return os.path.join( os.path.dirname(os.path.dirname(__file__)), entry ) + +# We do this hacky monkeypatching to enable us to define a setup method +# on class based views for setting up variables without touching the dispatch +# method. +@wrapt.patch_function_wrapper(django.views.View, 'dispatch') +def monkey_patched_dispatch(wrapped, instance, args, kwargs): + if hasattr(instance, 'setup'): + instance.setup(*args, **kwargs) + return wrapped(*args, **kwargs) + + DJANGO_BASE_PATH = os.path.dirname(os.path.dirname(__file__)) WSGI_APPLICATION = 'bornhack.wsgi.application' diff --git a/src/requirements/production.txt b/src/requirements/production.txt index b44a6ece..f7c8dd10 100644 --- a/src/requirements/production.txt +++ b/src/requirements/production.txt @@ -37,3 +37,5 @@ six==1.10.0 sqlparse==0.2.2 venusian==1.0 webencodings==0.5 + +wrapt==1.10.11