From 3015a46f9a2593fcc624ec77d441fc6b784760eb Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 29 May 2015 17:14:34 +0200 Subject: [PATCH] Improve database info Support for MySQL, include SQLite persistence warning. --- welcome/database.py | 22 ++++++++++++++++++++++ welcome/templates/welcome/index.html | 11 ++++++++++- welcome/views.py | 17 ++--------------- 3 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 welcome/database.py diff --git a/welcome/database.py b/welcome/database.py new file mode 100644 index 0000000..2ac4a3f --- /dev/null +++ b/welcome/database.py @@ -0,0 +1,22 @@ +from django.conf import settings + + +def info(): + db_settings = settings.DATABASES['default'] + if 'postgres' in db_settings['ENGINE']: + engine = 'PostgreSQL' + url = '{HOST}:{PORT}/{NAME}'.format(**db_settings) + elif 'mysql' in db_settings['ENGINE']: + engine = 'MySQL' + url = '{HOST}:{PORT}/{NAME}'.format(**db_settings) + elif 'sqlite' in db_settings['ENGINE']: + engine = 'SQLite' + url = '{NAME}'.format(**db_settings) + else: + engine = 'unknown' + url = '' + return { + 'engine': engine, + 'url': url, + 'is_sqlite': engine == 'SQLite', + } diff --git a/welcome/templates/welcome/index.html b/welcome/templates/welcome/index.html index 75e58c3..a26aa92 100644 --- a/welcome/templates/welcome/index.html +++ b/welcome/templates/welcome/index.html @@ -14,6 +14,7 @@ #explanation { background:#eee; } #instructions { background:#f6f6f6; } #instructions ol li { margin: 0.2em 0 0.2em 2em; } + .warning { color: orange } @@ -51,7 +52,15 @@

Server hostname: {{ hostname }}
- Database server: {{ database_info }}
+ Database server: {{ database.engine }} ({{ database.url }})
+ {% if database.is_sqlite %} + + Data persistence warning: + You are currently using SQLite. + This is fine for development, but your data won't be persisted + across application deployments. +
+ {% endif %} Page views: {{ count }}

diff --git a/welcome/views.py b/welcome/views.py index d88d346..1d89a66 100644 --- a/welcome/views.py +++ b/welcome/views.py @@ -2,30 +2,17 @@ import os from django.shortcuts import render from django.conf import settings +from . import database from .models import PageView # Create your views here. - -def _database_info(): - db_settings = settings.DATABASES['default'] - if 'postgres' in db_settings['ENGINE']: - engine = 'PostgreSQL' - info = '{HOST}:{PORT}/{NAME}'.format(**db_settings) - else: - engine = 'SQLite' - info = '{NAME}'.format(**db_settings) - return '{} ({})'.format(engine, info) - -database_info = _database_info() - - def index(request): hostname = os.getenv('HOSTNAME', 'unknown') PageView.objects.create(hostname=hostname) return render(request, 'welcome/index.html', { 'hostname': hostname, - 'database_info': database_info, + 'database': database.info(), 'count': PageView.objects.count() })