From 695023a09f7ed62d3a414dfc7c7b909fd0d1171b Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Fri, 20 May 2016 17:19:20 +0200 Subject: [PATCH 1/1] sanitize settings --- edumed/settings.d/10-paths.py | 3 --- edumed/settings.d/40-context.py | 14 ---------- edumed/settings.d/40-middleware.py | 27 ------------------- edumed/settings.py | 15 ----------- edumed/settings/__init__.py | 21 +++++++++++++++ .../30-apps.py => settings/apps.py} | 4 ++- .../40-auth.py => settings/auth.py} | 3 +++ .../20-basic.py => settings/basic.py} | 11 +++++--- .../40-cache.py => settings/cache.py} | 2 ++ edumed/settings/context.py | 15 +++++++++++ .../50-contrib.py => settings/contrib.py} | 2 ++ .../60-custom.py => settings/custom.py} | 2 ++ .../30-locale.py => settings/locale.py} | 5 +++- .../40-logging.py => settings/logging.py} | 1 + edumed/settings/middleware.py | 25 +++++++++++++++++ edumed/settings/paths.py | 4 +++ .../35-search.py => settings/search.py} | 2 ++ .../50-static.py => settings/static.py} | 11 +++++--- edumed/utils.py | 12 +++++++-- 19 files changed, 110 insertions(+), 69 deletions(-) delete mode 100644 edumed/settings.d/10-paths.py delete mode 100644 edumed/settings.d/40-context.py delete mode 100644 edumed/settings.d/40-middleware.py delete mode 100644 edumed/settings.py create mode 100644 edumed/settings/__init__.py rename edumed/{settings.d/30-apps.py => settings/apps.py} (95%) rename edumed/{settings.d/40-auth.py => settings/auth.py} (75%) rename edumed/{settings.d/20-basic.py => settings/basic.py} (75%) rename edumed/{settings.d/40-cache.py => settings/cache.py} (89%) create mode 100644 edumed/settings/context.py rename edumed/{settings.d/50-contrib.py => settings/contrib.py} (95%) rename edumed/{settings.d/60-custom.py => settings/custom.py} (64%) rename edumed/{settings.d/30-locale.py => settings/locale.py} (91%) rename edumed/{settings.d/40-logging.py => settings/logging.py} (97%) create mode 100644 edumed/settings/middleware.py create mode 100644 edumed/settings/paths.py rename edumed/{settings.d/35-search.py => settings/search.py} (86%) rename edumed/{settings.d/50-static.py => settings/static.py} (89%) diff --git a/edumed/settings.d/10-paths.py b/edumed/settings.d/10-paths.py deleted file mode 100644 index b3ef304..0000000 --- a/edumed/settings.d/10-paths.py +++ /dev/null @@ -1,3 +0,0 @@ -from os import path - -PROJECT_DIR = path.dirname(path.abspath(path.dirname(__file__))) diff --git a/edumed/settings.d/40-context.py b/edumed/settings.d/40-context.py deleted file mode 100644 index 6e43646..0000000 --- a/edumed/settings.d/40-context.py +++ /dev/null @@ -1,14 +0,0 @@ -TEMPLATE_CONTEXT_PROCESSORS = tuple(x for x in ( - "django.contrib.auth.context_processors.auth" - if "django.contrib.auth" in INSTALLED_APPS else None, - "django.core.context_processors.debug", - "django.core.context_processors.i18n", - "django.core.context_processors.media", - "django.core.context_processors.static", - "django.core.context_processors.tz", - "django.contrib.messages.context_processors.messages" - if 'django.contrib.messages' in INSTALLED_APPS else None, - "django.core.context_processors.request", - 'pybb.context_processors.processor', - 'edumed.context_processors.base_template' -) if x is not None) diff --git a/edumed/settings.d/40-middleware.py b/edumed/settings.d/40-middleware.py deleted file mode 100644 index 6175197..0000000 --- a/edumed/settings.d/40-middleware.py +++ /dev/null @@ -1,27 +0,0 @@ -MIDDLEWARE_CLASSES = tuple(x for x in ( - 'django.middleware.cache.UpdateCacheMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware' - if "django.contrib.sessions" in INSTALLED_APPS else None, - #'django.middleware.locale.LocaleMiddleware', - 'subdomains.middleware.SubdomainURLRoutingMiddleware', - 'fnpdjango.middleware.URLLocaleMiddleware', - - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'honeypot.middleware.HoneypotMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware' - if "django.contrib.auth" in INSTALLED_APPS else None, - 'django_cas.middleware.CASMiddleware' - if "django_cas" in INSTALLED_APPS else None, - 'django.contrib.messages.middleware.MessageMiddleware' - if "django.contrib.messages" in INSTALLED_APPS else None, - # Uncomment the next line for simple clickjacking protection: - # 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'pagination.middleware.PaginationMiddleware' - if "pagination" in INSTALLED_APPS else None, - 'django.middleware.cache.FetchFromCacheMiddleware', - 'fnpdjango.middleware.SetRemoteAddrFromXRealIP', - 'pybb.middleware.PybbMiddleware', - 'forum.middleware.ForumMiddleware', - 'wtem.middleware.ThreadLocalMiddleware' -) if x is not None) diff --git a/edumed/settings.py b/edumed/settings.py deleted file mode 100644 index c5df1a7..0000000 --- a/edumed/settings.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- -import os.path -import glob - -conffiles = glob.glob(os.path.join( - os.path.dirname(__file__), 'settings.d', '*.py')) -conffiles.sort() -for f in conffiles: - execfile(os.path.abspath(f)) - -try: - execfile(os.path.abspath(os.path.join( - os.path.dirname(__file__), 'localsettings.py'))) -except IOError: - pass diff --git a/edumed/settings/__init__.py b/edumed/settings/__init__.py new file mode 100644 index 0000000..4a75df0 --- /dev/null +++ b/edumed/settings/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +from .paths import * +from .basic import * +from .apps import * +from .locale import * +from .search import * +from .auth import * +from .cache import * +from .context import * +from .logging import * +from .middleware import * +from .contrib import * +from .static import * +from .custom import * + +# Load localsettings, if they exist +try: + from edumed.localsettings import * +except ImportError: + pass \ No newline at end of file diff --git a/edumed/settings.d/30-apps.py b/edumed/settings/apps.py similarity index 95% rename from edumed/settings.d/30-apps.py rename to edumed/settings/apps.py index a5b9c97..ca243c9 100644 --- a/edumed/settings.d/30-apps.py +++ b/edumed/settings/apps.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + INSTALLED_APPS = ( 'edumed', 'curriculum', @@ -5,7 +7,7 @@ INSTALLED_APPS = ( 'comment', 'wtem', 'publishers', - 'api' + 'api', 'fnpdjango', 'south', diff --git a/edumed/settings.d/40-auth.py b/edumed/settings/auth.py similarity index 75% rename from edumed/settings.d/40-auth.py rename to edumed/settings/auth.py index 14847ac..35c74c4 100644 --- a/edumed/settings.d/40-auth.py +++ b/edumed/settings/auth.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +from .apps import INSTALLED_APPS + if 'django_cas' in INSTALLED_APPS: AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', diff --git a/edumed/settings.d/20-basic.py b/edumed/settings/basic.py similarity index 75% rename from edumed/settings.d/20-basic.py rename to edumed/settings/basic.py index 9d0a384..3e553b3 100644 --- a/edumed/settings.d/20-basic.py +++ b/edumed/settings/basic.py @@ -1,10 +1,15 @@ +# -*- coding: utf-8 -*- +import os.path + +from .paths import PROJECT_DIR + DEBUG = False TEMPLATE_DEBUG = DEBUG DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. - 'NAME': path.join(PROJECT_DIR, 'dev.db'), # Or path to database file if using sqlite3. + 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. + 'NAME': os.path.join(PROJECT_DIR, 'dev.db'), # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. @@ -18,7 +23,7 @@ SITE_ID = 1 TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', -# 'django.template.loaders.eggs.Loader', + # 'django.template.loaders.eggs.Loader', ) ROOT_URLCONF = 'edumed.urls' diff --git a/edumed/settings.d/40-cache.py b/edumed/settings/cache.py similarity index 89% rename from edumed/settings.d/40-cache.py rename to edumed/settings/cache.py index d6d903e..3079b6c 100644 --- a/edumed/settings.d/40-cache.py +++ b/edumed/settings/cache.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', diff --git a/edumed/settings/context.py b/edumed/settings/context.py new file mode 100644 index 0000000..bff5838 --- /dev/null +++ b/edumed/settings/context.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from edumed.utils import process_app_deps + +TEMPLATE_CONTEXT_PROCESSORS = process_app_deps(( + ("django.contrib.auth.context_processors.auth", "django.contrib.auth"), + "django.core.context_processors.debug", + "django.core.context_processors.i18n", + "django.core.context_processors.media", + "django.core.context_processors.static", + "django.core.context_processors.tz", + ("django.contrib.messages.context_processors.messages", 'django.contrib.messages'), + "django.core.context_processors.request", + 'pybb.context_processors.processor', + 'edumed.context_processors.base_template', +)) diff --git a/edumed/settings.d/50-contrib.py b/edumed/settings/contrib.py similarity index 95% rename from edumed/settings.d/50-contrib.py rename to edumed/settings/contrib.py index 9306de1..d7ea957 100644 --- a/edumed/settings.d/50-contrib.py +++ b/edumed/settings/contrib.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + CAS_SERVER_URL = 'http://logowanie.nowoczesnapolska.org.pl/cas/' CAS_VERSION = '3' diff --git a/edumed/settings.d/60-custom.py b/edumed/settings/custom.py similarity index 64% rename from edumed/settings.d/60-custom.py rename to edumed/settings/custom.py index 869e6f4..a2fc281 100644 --- a/edumed/settings.d/60-custom.py +++ b/edumed/settings/custom.py @@ -1 +1,3 @@ +# -*- coding: utf-8 -*- + CONTACT_FORMS_MODULE = 'edumed.contact_forms' diff --git a/edumed/settings.d/30-locale.py b/edumed/settings/locale.py similarity index 91% rename from edumed/settings.d/30-locale.py rename to edumed/settings/locale.py index ed2173a..cfe4cc1 100644 --- a/edumed/settings.d/30-locale.py +++ b/edumed/settings/locale.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- +import os.path + +from .paths import PROJECT_DIR LANGUAGES = ( ('pl', u'polski'), @@ -33,5 +36,5 @@ CONTRIB_LOCALE_APPS = ( 'pybb', ) LOCALE_PATHS = ( - path.join(PROJECT_DIR, 'edumed/locale-contrib'), + os.path.join(PROJECT_DIR, 'edumed/locale-contrib'), ) diff --git a/edumed/settings.d/40-logging.py b/edumed/settings/logging.py similarity index 97% rename from edumed/settings.d/40-logging.py rename to edumed/settings/logging.py index 3d91c9c..ea3ad61 100644 --- a/edumed/settings.d/40-logging.py +++ b/edumed/settings/logging.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error when DEBUG=False. diff --git a/edumed/settings/middleware.py b/edumed/settings/middleware.py new file mode 100644 index 0000000..15ab189 --- /dev/null +++ b/edumed/settings/middleware.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from edumed.utils import process_app_deps + +MIDDLEWARE_CLASSES = process_app_deps(( + 'django.middleware.cache.UpdateCacheMiddleware', + ('django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions'), + # 'django.middleware.locale.LocaleMiddleware', + 'subdomains.middleware.SubdomainURLRoutingMiddleware', + 'fnpdjango.middleware.URLLocaleMiddleware', + + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'honeypot.middleware.HoneypotMiddleware', + ('django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth'), + ('django_cas.middleware.CASMiddleware', 'django_cas'), + ('django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages'), + # Uncomment the next line for simple clickjacking protection: + # 'django.middleware.clickjacking.XFrameOptionsMiddleware', + ('pagination.middleware.PaginationMiddleware', 'pagination'), + 'django.middleware.cache.FetchFromCacheMiddleware', + 'fnpdjango.middleware.SetRemoteAddrFromXRealIP', + 'pybb.middleware.PybbMiddleware', + 'forum.middleware.ForumMiddleware', + 'wtem.middleware.ThreadLocalMiddleware' +)) diff --git a/edumed/settings/paths.py b/edumed/settings/paths.py new file mode 100644 index 0000000..55d7b9e --- /dev/null +++ b/edumed/settings/paths.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +import os.path + +PROJECT_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) diff --git a/edumed/settings.d/35-search.py b/edumed/settings/search.py similarity index 86% rename from edumed/settings.d/35-search.py rename to edumed/settings/search.py index bfc6f28..f54983b 100644 --- a/edumed/settings.d/35-search.py +++ b/edumed/settings/search.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.solr_backend.SolrEngine', diff --git a/edumed/settings.d/50-static.py b/edumed/settings/static.py similarity index 89% rename from edumed/settings.d/50-static.py rename to edumed/settings/static.py index c667c32..a2f2c05 100644 --- a/edumed/settings.d/50-static.py +++ b/edumed/settings/static.py @@ -1,12 +1,17 @@ -MEDIA_ROOT = path.join(PROJECT_DIR, 'media/') +# -*- coding: utf-8 -*- +import os.path + +from .paths import PROJECT_DIR + +MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media/') MEDIA_URL = '/media/' -STATIC_ROOT = path.join(PROJECT_DIR, 'static/') +STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/') STATIC_URL = '/static/' STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -# 'django.contrib.staticfiles.finders.DefaultStorageFinder', + # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) STATICFILES_STORAGE = 'fnpdjango.utils.pipeline_storage.GzipPipelineCachedStorage' diff --git a/edumed/utils.py b/edumed/utils.py index 756ffa3..2dce875 100644 --- a/edumed/utils.py +++ b/edumed/utils.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- import codecs import csv - import cStringIO +from settings.apps import INSTALLED_APPS + # source: https://docs.python.org/2/library/csv.html#examples class UnicodeCSVWriter(object): @@ -33,4 +34,11 @@ class UnicodeCSVWriter(object): def writerows(self, rows): for row in rows: - self.writerow(row) \ No newline at end of file + self.writerow(row) + + +def process_app_deps(list_with_deps): + return tuple( + (x[0] if type(x) == tuple else x) + for x in list_with_deps + if type(x) != tuple or x[1] in INSTALLED_APPS) \ No newline at end of file -- 2.20.1