Improve database info

Support for MySQL, include SQLite persistence warning.
This commit is contained in:
Rodolfo Carvalho 2015-05-29 17:14:34 +02:00
parent a6f9284570
commit 3015a46f9a
3 changed files with 34 additions and 16 deletions

22
welcome/database.py Normal file
View file

@ -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',
}

View file

@ -14,6 +14,7 @@
#explanation { background:#eee; } #explanation { background:#eee; }
#instructions { background:#f6f6f6; } #instructions { background:#f6f6f6; }
#instructions ol li { margin: 0.2em 0 0.2em 2em; } #instructions ol li { margin: 0.2em 0 0.2em 2em; }
.warning { color: orange }
</style> </style>
</head> </head>
@ -51,7 +52,15 @@
<div id="hostname"> <div id="hostname">
<p> <p>
Server hostname: {{ hostname }}<br> Server hostname: {{ hostname }}<br>
Database server: {{ database_info }}<br> Database server: {{ database.engine }} ({{ database.url }})<br>
{% if database.is_sqlite %}
<span class="warning">
Data persistence warning:
You are currently using SQLite.
This is fine for development, but your data won't be persisted
across application deployments.
</span><br>
{% endif %}
Page views: {{ count }} Page views: {{ count }}
</p> </p>
</div> </div>

View file

@ -2,30 +2,17 @@ import os
from django.shortcuts import render from django.shortcuts import render
from django.conf import settings from django.conf import settings
from . import database
from .models import PageView from .models import PageView
# Create your views here. # 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): def index(request):
hostname = os.getenv('HOSTNAME', 'unknown') hostname = os.getenv('HOSTNAME', 'unknown')
PageView.objects.create(hostname=hostname) PageView.objects.create(hostname=hostname)
return render(request, 'welcome/index.html', { return render(request, 'welcome/index.html', {
'hostname': hostname, 'hostname': hostname,
'database_info': database_info, 'database': database.info(),
'count': PageView.objects.count() 'count': PageView.objects.count()
}) })