Commit 34a23f65 authored by nachtgold's avatar nachtgold
Browse files

new highscore table in the menu with a view of the snakegames and users table

parent 559461e2
......@@ -31,6 +31,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'highscore'
]
MIDDLEWARE = [
......
......@@ -4,6 +4,7 @@ from django.contrib.auth import views as auth_views
from django.views.generic import TemplateView
from django.views.generic.base import RedirectView
from core import views as core_views
from highscore import views as highscore_views
urlpatterns = [
path('', TemplateView.as_view(template_name='index.html'), name='home'),
......@@ -16,4 +17,5 @@ urlpatterns = [
path('snake/', core_views.snake_list, name='snake'),
path('snake/create', core_views.snake_create, name='snake_create'),
path('snake/<int:snake_id>', core_views.snake_edit, name='snake_edit'),
path('highscore/', highscore_views.table, name='highscore_table')
]
# Generated by Django 2.0.3 on 2018-03-30 20:45
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('core', '0005_remove_snake_selected_version'),
]
operations = [
migrations.AlterModelOptions(
name='snakeversion',
options={'get_latest_by': 'created'},
),
migrations.RemoveField(
model_name='snakegame',
name='team',
),
migrations.AddField(
model_name='snakegame',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
]
......@@ -32,7 +32,7 @@ class SnakeVersion(models.Model):
class SnakeGame(models.Model):
snake_version = models.ForeignKey(SnakeVersion, on_delete=models.CASCADE)
team = models.ForeignKey(Team, null=True, blank=True, on_delete=models.SET_NULL)
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
score = models.IntegerField()
......@@ -41,3 +41,4 @@ class SnakeGame(models.Model):
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
current_team = models.ForeignKey(Team, blank=True, null=True, on_delete=models.SET_NULL)
.highscore-title {
font-size: 1.4em;
margin-bottom: 1em;
}
.highscore-table thead th {
background-color: #333;
padding: 5px;
}
.highscore-table td:nth-child(1),
.highscore-table th:nth-child(1) {
border-right: 1px solid #666;
}
.highscore-table tbody td {
text-align: right;
min-width: 200px;
padding-right: 5px;
}
.highscore-table tbody tr:nth-child(odd) td {
background-color: #444;
}
.highscore-table tbody tr:nth-child(even) td {
background-color: #555;
}
\ No newline at end of file
......@@ -5,6 +5,7 @@
<meta charset="UTF-8">
<title>Programmierspiel</title>
<link rel="stylesheet" type="text/css" href="{% static "core/styles.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "highscore/highscore.css" %}" />
<link rel="stylesheet" href="{% static "core/highlight.js/styles/agate.css" %}" />
{% block css %}
{% endblock %}
......@@ -19,6 +20,7 @@
<ul class="nav">
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'watch' %}">Watch</a></li>
<li><a href="{% url 'highscore_table' %}">Highscores</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'snake' %}">My Snakes</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
......
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class HighscoreConfig(AppConfig):
name = 'highscore'
# Generated by Django 2.0.1 on 2018-01-10 21:30
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
operations = [
migrations.RunSQL(
"""
DROP VIEW IF EXISTS highscore_highscore;
CREATE VIEW highscore_highscore AS
SELECT (SELECT COUNT(0) FROM core_snakegame i WHERE i.score >= a.score) as id,
u.username,
a.score
FROM core_snakegame a INNER JOIN auth_user u ON a.user_id = u.id
ORDER BY score DESC;
"""
),
]
# Generated by Django 2.0.3 on 2018-03-30 22:23
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('highscore', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Highscore',
fields=[
('id', models.BigIntegerField(primary_key=True, serialize=False)),
('username', models.CharField(max_length=150)),
('score', models.IntegerField()),
],
options={
'db_table': 'highscore_highscore',
'managed': False,
},
),
]
from django.db import models
import core
class Highscore(models.Model):
id = models.BigIntegerField(primary_key=True)
username = models.CharField(max_length=150)
score = models.IntegerField()
class Meta:
managed = False
db_table = 'highscore_highscore'
{% for row in rows %}
<tr>
<td class="name">{{ row.attr }}</td>
<td class="field">{{ row.value }}</td>
</tr>
{% endfor %}
\ No newline at end of file
{% load model_helpers %}
<table>
{% model_as_table %}
</table>
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<div class="highscore-title">Highscores</div>
<table class="highscore-table" cellspacing="0">
<thead>
<tr>
<th>User</th>
<th>Score</th>
</tr>
</thead>
<tbody>
{% for score in highscores %}
<tr>
<td>{{score.username}}</td>
<td>{{score.score}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
\ No newline at end of file
from django import template
register = template.Library()
@register.inclusion_tag('highscore/model_table.html', takes_context=True)
def model_as_table(context, model_key=None, model_table_attrs_key=None):
if model_key is None:
model_key = 'object'
if model_table_attrs_key is None:
model_table_attrs_key = 'model_table_attrs'
attrs = context[model_table_attrs_key]
table_context = {'rows': []}
for row in context[model_key]:
for attr in attrs:
value = str(getattr(row, attr))
if value:
table_context['rows'].append({'attr': attr,
'value': value})
return table_context
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
from .models import Highscore
def table(request):
output = Highscore.objects.all()
# listing macro
#return render(request, 'highscore/outputtable.html', {'object': output, 'model_table_attrs': ['user_id', 'score']})
return render(request, 'highscore/table.html', context={'highscores': output})
\ No newline at end of file
......@@ -2,3 +2,4 @@ Django==2.0.3
mysqlclient==1.3.12
Pillow==5.0.0
pytz==2017.3
sqlparse=0.2.4
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment