From 5fcf242b8ec18c4e9c9a057753aa7f953ff472a0 Mon Sep 17 00:00:00 2001
From: Dennis Ahrens <dennis.ahrens@hs-hannover.de>
Date: Thu, 11 Jul 2019 11:51:41 +0200
Subject: [PATCH] Raise django version and adjust settings to hsh structure

---
 .gitignore                             |  5 +-
 helloworld/settings/__init__.py        | 20 ++++++--
 helloworld/settings/common.py          | 68 +++++++++++++++++++++-----
 helloworld/settings/dev_example.py     | 46 +++++++++++++++++
 helloworld/settings/private.example.py | 13 -----
 helloworld/settings/prod_example.py    | 12 +++++
 helloworld/urls.py                     | 28 ++---------
 requirements.txt                       |  3 +-
 8 files changed, 138 insertions(+), 57 deletions(-)
 create mode 100644 helloworld/settings/dev_example.py
 delete mode 100644 helloworld/settings/private.example.py
 create mode 100644 helloworld/settings/prod_example.py

diff --git a/.gitignore b/.gitignore
index 8b7ba6c..3954c81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,5 +9,6 @@
 build/
 dist/
 venv/
-helloworld.db
-helloworld/settings/private.py
+db.sqlite3
+helloworld/settings/prod.py
+helloworld/settings/dev.py
diff --git a/helloworld/settings/__init__.py b/helloworld/settings/__init__.py
index 523517d..f89d07a 100644
--- a/helloworld/settings/__init__.py
+++ b/helloworld/settings/__init__.py
@@ -1,5 +1,17 @@
-from helloworld.settings.common import *
-from helloworld.settings.logging import *
+from .common import *
 
-# this comes after all other settings (since private settings will override several values)
-from helloworld.settings.private import *
+try:
+    from .prod import *
+    _PRODUCTION = True
+    assert DEBUG is False, "DEBUG in development?!"
+except ImportError:
+    _PRODUCTION = False
+
+try:
+    from .dev import *
+    _DEVELOPMENT = True
+except ImportError:
+    _DEVELOPMENT = False
+
+
+assert _DEVELOPMENT ^ _PRODUCTION, "Bad settings: development {0}, production {1}".format(_DEVELOPMENT, _PRODUCTION)
\ No newline at end of file
diff --git a/helloworld/settings/common.py b/helloworld/settings/common.py
index 6dba931..8cf7f91 100644
--- a/helloworld/settings/common.py
+++ b/helloworld/settings/common.py
@@ -2,6 +2,9 @@
 # Django settings for helloworld project.
 
 import os
+import sys
+
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 ADMINS = (
     # ('Your Name', 'your_email@example.com'),
@@ -9,6 +12,15 @@ ADMINS = (
 
 MANAGERS = ADMINS
 
+INSTALLED_APPS = (
+    'django.contrib.auth',
+    'django.contrib.contenttypes',
+    'django.contrib.sessions',
+    'django.contrib.sites',
+    'django.contrib.messages',
+    'django.contrib.staticfiles'
+)
+
 # Local time zone for this installation. Choices can be found here:
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
 # although not all choices may be available on all operating systems.
@@ -99,15 +111,47 @@ TEMPLATE_DIRS = (
     os.path.join(os.path.dirname(__file__), "templates"),
 )
 
-INSTALLED_APPS = (
-    'django.contrib.auth',
-    'django.contrib.contenttypes',
-    'django.contrib.sessions',
-    'django.contrib.sites',
-    'django.contrib.messages',
-    'django.contrib.staticfiles',
-    # Uncomment the next line to enable the admin:
-    'django.contrib.admin',
-    # Uncomment the next line to enable admin documentation:
-    'django.contrib.admindocs',
-)
+LOGGING = {
+    'version': 1,
+    'disable_existing_loggers': False,
+    'formatters': {
+        'with_timestamp': {
+            'format': '[%(asctime)s %(levelname).3s %(process)d] %(message)s',
+            'datefmt': '%Y-%m-%d %H:%M:%S',
+        },
+    },
+    'handlers': {
+        'console': {
+            'stream': sys.stdout,
+            'level': 'INFO',
+            'class': 'logging.StreamHandler',
+            'formatter': 'with_timestamp'
+        },
+    },
+    'loggers': {
+        '': {
+            'handlers': ['console'],
+            'level': 'DEBUG'
+        },
+        'django': {
+            'level': 'WARNING',
+            'handlers': ['console'],
+            'propagate': False,
+        },
+        'django.db': {
+            'level': 'DEBUG',
+            'handlers': ['console'],
+            'propagate': False,
+        },
+        'pika': {
+            'level': 'WARNING',
+            'handlers': ['console'],
+            'propagate': False,
+        },
+        'pikatasks': {
+            'level': 'INFO',
+            'handlers': ['console'],
+            'propagate': False,
+        },
+    }
+}
\ No newline at end of file
diff --git a/helloworld/settings/dev_example.py b/helloworld/settings/dev_example.py
new file mode 100644
index 0000000..eb6a1c1
--- /dev/null
+++ b/helloworld/settings/dev_example.py
@@ -0,0 +1,46 @@
+from .common import *
+
+
+DEBUG = True
+
+
+SECRET_KEY = str(getattr(os.uname(), "nodename"))
+
+
+ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
+
+
+SESSION_COOKIE_SECURE = False
+
+
+DATABASES = {
+    "default": {
+        "ENGINE": "django.db.backends.sqlite3",
+        "NAME": os.path.join(BASE_DIR, "../db.sqlite3"),
+    }
+}
+
+
+LOGGING["handlers"]["console"]["level"] = "DEBUG"
+
+
+PIKATASKS_BROKER_HOST = "localhost"
+PIKATASKS_VIRTUAL_HOST = "idm"
+PIKATASKS_SSL_ENABLED = False
+PIKATASKS_BROKER_PORT = 5672  # No TLS/SSL
+
+PIKATASKS_USERNAME = "accountmgr"
+PIKATASKS_PASSWORD = ""
+PIKATASKS_CLIENT_EXCHANGE_NAME = ""
+
+
+try:  # Colored logger CaaS. Auto downloaded and verified.
+    import os
+    import hashlib
+    from urllib import request
+    cache, url = "/tmp/_colored_logger.py", "https://lab.it.hs-hannover.de/lukyanch/pydevutils/raw/fa4af6555a8eb996e1be158ca12691de9b33ba45/colored_logger.py"
+    code = bool(os.path.exists(cache) or request.urlretrieve(url, cache)) and open(cache, "r").read()
+    assert hashlib.sha256(code.encode()).hexdigest() == "d4d261a40f95733f9fb184fc4ccb55d007b880cb62c8d6a06824d43eeb1391ac", "unrecognized content in " + cache
+    exec(code)
+except Exception as e:
+    print("No colored logger: {e.__class__.__name__}: {e}".format(e=e))
diff --git a/helloworld/settings/private.example.py b/helloworld/settings/private.example.py
deleted file mode 100644
index db867db..0000000
--- a/helloworld/settings/private.example.py
+++ /dev/null
@@ -1,13 +0,0 @@
-DATABASES = {
-    'default': {
-        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
-        'NAME': 'helloworld.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.
-        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
-    }
-}
-
-# Make this unique, and don't share it with anybody.
-SECRET_KEY = '0h@p1(0e%*b9r4k+dnpc+7s18lpap6+=cu^#^d=hll+38zv_)r'
diff --git a/helloworld/settings/prod_example.py b/helloworld/settings/prod_example.py
new file mode 100644
index 0000000..313e729
--- /dev/null
+++ b/helloworld/settings/prod_example.py
@@ -0,0 +1,12 @@
+SECRET_KEY = r""
+
+
+ALLOWED_HOSTS = []
+
+
+DATABASES = {
+    "default": {
+        "ENGINE": "django.db.backends.sqlite3",
+        "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
+    }
+}
diff --git a/helloworld/urls.py b/helloworld/urls.py
index fcae7dd..d16df74 100644
--- a/helloworld/urls.py
+++ b/helloworld/urls.py
@@ -1,29 +1,9 @@
 # -*- coding: utf-8 -*-
 
-from django.conf.urls import patterns, include
-from django.conf import settings
+from django.conf.urls import url
+from .views import index
 
-# Uncomment the next two lines to enable the admin:
-from django.contrib import admin
-admin.autodiscover()
-
-urlpatterns = patterns('',
-    # Example:
-    # (r'^helloworld/', include('helloworld.foo.urls')),
-
-    # Uncomment the admin/doc line below to enable admin documentation:
-    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
-
-    # Uncomment the next line to enable the admin:
-    (r'^admin/', include(admin.site.urls)),
-
-    # Hello, world!
-    (r'', 'helloworld.views.index'),
+urlpatterns = (
+    url(r'', index),
 )
 
-if settings.DEBUG:
-    urlpatterns += patterns('',
-        (r'^media/(?P<path>.*)$', 'django.views.static.serve',
-         {'document_root': settings.MEDIA_ROOT}),
-    )
-
diff --git a/requirements.txt b/requirements.txt
index 9607b39..3439e99 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,2 @@
 # requirements.txt
-
-Django==1.9.4
\ No newline at end of file
+Django>=2.2,<2.3
\ No newline at end of file
-- 
GitLab