From 8f82a084e959b9f7349ca9570ab01382f8423aaf Mon Sep 17 00:00:00 2001 From: Andres Vargas Date: Fri, 27 Mar 2015 14:46:52 -0600 Subject: [PATCH 1/3] make admin works --- importd/__init__.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/importd/__init__.py b/importd/__init__.py index 8980e51..f61233a 100644 --- a/importd/__init__.py +++ b/importd/__init__.py @@ -415,24 +415,26 @@ def _configure_django(self, **kw): admin_url = kw.pop("admin", "^admin/") if admin_url: + middlewares_list = [] + if "django.contrib.sessions" not in installed: + installed.append("django.contrib.sessions") + middlewares_list.append("django.contrib.sessions" + ".middleware." + "SessionMiddleware") if "django.contrib.auth" not in installed: installed.append("django.contrib.auth") + middlewares_list.append("django.contrib.auth.middleware" + ".AuthenticationMiddleware") if "django.contrib.contenttypes" not in installed: installed.append("django.contrib.contenttypes") if "django.contrib.auth" not in installed: installed.append("django.contrib.auth") + middlewares_list.append("django.contrib.auth.middleware" + ".AuthenticationMiddleware") if "django.contrib.messages" not in installed: installed.append("django.contrib.messages") - if "django.contrib.sessions" not in installed: - installed.append("django.contrib.sessions") - # check session middleware installed - # https://docs.djangoproject.com/en/1.7/topics/http/sessions/#enabling-sessions - last_position = len(kw["MIDDLEWARE_CLASSES"]) - kw["MIDDLEWARE_CLASSES"] = list(kw["MIDDLEWARE_CLASSES"]) - kw["MIDDLEWARE_CLASSES"].insert( - last_position, - "django.contrib.sessions.middleware.SessionMiddleware" - ) + middlewares_list.append("django.contrib.messages." + "middleware.MessageMiddleware") if "django.contrib.admin" not in installed: installed.append("django.contrib.admin") if "django.contrib.humanize" not in installed: @@ -462,6 +464,12 @@ def _configure_django(self, **kw): ) # This one gives 500 if its Enabled without previous syncdb # 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', + for middleware_str in middlewares_list: + if not middleware_str in kw["MIDDLEWARE_CLASSES"]: + last_pos = len(kw["MIDDLEWARE_CLASSES"]) + kw["MIDDLEWARE_CLASSES"].insert(last_pos, + middleware_str) + if django_extensions and werkzeug: installed.append('django_extensions') From b239b952e8ec9bb8c0ec8e136d59b0acd19da6ec Mon Sep 17 00:00:00 2001 From: Andres Vargas Date: Fri, 27 Mar 2015 15:00:56 -0600 Subject: [PATCH 2/3] fix contrib middlewares and context for getting django admin works --- importd/__init__.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/importd/__init__.py b/importd/__init__.py index f61233a..20b9257 100644 --- a/importd/__init__.py +++ b/importd/__init__.py @@ -409,6 +409,9 @@ def _configure_django(self, **kw): kw["MIDDLEWARE_CLASSES"].insert( 0, "importd.SmartReturnMiddleware" ) + if not "TEMPLATE_CONTEXT_PROCESSORS" in kw: + kw["TEMPLATE_CONTEXT_PROCESSORS"] = list( + global_settings.TEMPLATE_CONTEXT_PROCESSORS) installed = list(kw.setdefault("INSTALLED_APPS", [])) @@ -416,6 +419,7 @@ def _configure_django(self, **kw): if admin_url: middlewares_list = [] + context_processor_list = [] if "django.contrib.sessions" not in installed: installed.append("django.contrib.sessions") middlewares_list.append("django.contrib.sessions" @@ -435,6 +439,8 @@ def _configure_django(self, **kw): installed.append("django.contrib.messages") middlewares_list.append("django.contrib.messages." "middleware.MessageMiddleware") + context_processor_list.append("django.contrib.messages." + "context_processors.messages") if "django.contrib.admin" not in installed: installed.append("django.contrib.admin") if "django.contrib.humanize" not in installed: @@ -464,12 +470,16 @@ def _configure_django(self, **kw): ) # This one gives 500 if its Enabled without previous syncdb # 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', - for middleware_str in middlewares_list: - if not middleware_str in kw["MIDDLEWARE_CLASSES"]: - last_pos = len(kw["MIDDLEWARE_CLASSES"]) - kw["MIDDLEWARE_CLASSES"].insert(last_pos, - middleware_str) + def _insert_setting(list_strings, django_settings_key): + for _str in list_strings: + if not _str in kw[django_settings_key]: + last_pos = len(kw[django_settings_key]) + kw[django_settings_key].insert(last_pos, _str) + + _insert_setting(middlewares_list, "MIDDLEWARE_CLASSES") + _insert_setting(context_processor_list, + "TEMPLATE_CONTEXT_PROCESSORS") if django_extensions and werkzeug: installed.append('django_extensions') From 1b31007cc75a24e78bdfac976b6e9c005bb570f9 Mon Sep 17 00:00:00 2001 From: Andres Vargas Date: Mon, 30 Mar 2015 09:20:33 -0600 Subject: [PATCH 3/3] better if sentences --- importd/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/importd/__init__.py b/importd/__init__.py index 20b9257..63a103a 100644 --- a/importd/__init__.py +++ b/importd/__init__.py @@ -409,7 +409,7 @@ def _configure_django(self, **kw): kw["MIDDLEWARE_CLASSES"].insert( 0, "importd.SmartReturnMiddleware" ) - if not "TEMPLATE_CONTEXT_PROCESSORS" in kw: + if "TEMPLATE_CONTEXT_PROCESSORS" not in kw.keys(): kw["TEMPLATE_CONTEXT_PROCESSORS"] = list( global_settings.TEMPLATE_CONTEXT_PROCESSORS) @@ -473,7 +473,7 @@ def _configure_django(self, **kw): def _insert_setting(list_strings, django_settings_key): for _str in list_strings: - if not _str in kw[django_settings_key]: + if _str not in kw[django_settings_key]: last_pos = len(kw[django_settings_key]) kw[django_settings_key].insert(last_pos, _str)