| @ -0,0 +1,3 @@ | |||||
| from django.contrib import admin | |||||
| # Register your models here. | |||||
| @ -0,0 +1,5 @@ | |||||
| from django.apps import AppConfig | |||||
| class TestConfig(AppConfig): | |||||
| name = 'test' | |||||
| @ -0,0 +1,3 @@ | |||||
| from django.db import models | |||||
| # Create your models here. | |||||
| @ -0,0 +1,3 @@ | |||||
| from django.test import TestCase | |||||
| # Create your tests here. | |||||
| @ -0,0 +1,3 @@ | |||||
| from django.shortcuts import render | |||||
| # Create your views here. | |||||
| @ -1,11 +0,0 @@ | |||||
| from django.contrib import admin | |||||
| from .models import PageView | |||||
| # Register your models here. | |||||
| class PageViewAdmin(admin.ModelAdmin): | |||||
| list_display = ['hostname', 'timestamp'] | |||||
| admin.site.register(PageView, PageViewAdmin) | |||||
| @ -1,22 +0,0 @@ | |||||
| 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', | |||||
| } | |||||
| @ -1,21 +0,0 @@ | |||||
| # -*- coding: utf-8 -*- | |||||
| from __future__ import unicode_literals | |||||
| from django.db import models, migrations | |||||
| class Migration(migrations.Migration): | |||||
| dependencies = [ | |||||
| ] | |||||
| operations = [ | |||||
| migrations.CreateModel( | |||||
| name='PageView', | |||||
| fields=[ | |||||
| ('id', models.AutoField(verbose_name='ID', auto_created=True, serialize=False, primary_key=True)), | |||||
| ('hostname', models.CharField(max_length=32)), | |||||
| ('timestamp', models.DateTimeField(auto_now_add=True)), | |||||
| ], | |||||
| ), | |||||
| ] | |||||
| @ -1,7 +0,0 @@ | |||||
| from django.db import models | |||||
| # Create your models here. | |||||
| class PageView(models.Model): | |||||
| hostname = models.CharField(max_length=32) | |||||
| timestamp = models.DateTimeField(auto_now_add=True) | |||||
| @ -1,27 +0,0 @@ | |||||
| import os | |||||
| from .models import PageView | |||||
| from .database import info | |||||
| from django.test import TestCase | |||||
| # These basic tests are to be used as an example for running tests in S2I | |||||
| # and OpenShift when building an application image. | |||||
| class PageViewModelTest(TestCase): | |||||
| def test_viewpage_model(self): | |||||
| pageview = PageView.objects.create(hostname='localhost') | |||||
| pagetest = PageView.objects.get(hostname='localhost') | |||||
| self.assertEqual(pagetest.hostname, 'localhost') | |||||
| class PageViewTest(TestCase): | |||||
| def test_index(self): | |||||
| resp = self.client.get('/') | |||||
| self.assertEqual(resp.status_code, 200) | |||||
| class DbEngine(TestCase): | |||||
| def setUp(self): | |||||
| os.environ['ENGINE'] = 'SQLite' | |||||
| def test_engine_setup(self): | |||||
| settings = info() | |||||
| self.assertEqual(settings['engine'], 'SQLite') | |||||
| self.assertEqual(settings['is_sqlite'], True) | |||||
| @ -1,24 +0,0 @@ | |||||
| import os | |||||
| from django.shortcuts import render | |||||
| from django.conf import settings | |||||
| from django.http import HttpResponse | |||||
| from . import database | |||||
| from .models import PageView | |||||
| # Create your views here. | |||||
| def index(request): | |||||
| """Takes an request object as a parameter and creates an pageview object then responds by rendering the index view.""" | |||||
| hostname = os.getenv('HOSTNAME', 'unknown') | |||||
| PageView.objects.create(hostname=hostname) | |||||
| return render(request, 'welcome/index.html', { | |||||
| 'hostname': hostname, | |||||
| 'database': database.info(), | |||||
| 'count': PageView.objects.count() | |||||
| }) | |||||
| def health(request): | |||||
| """Takes an request as a parameter and gives the count of pageview objects as reponse""" | |||||
| return HttpResponse(PageView.objects.count()) | |||||