From 78f21178236a3e89b3e37724f415a95cb6dc7ed4 Mon Sep 17 00:00:00 2001 From: John Janus Date: Wed, 17 Apr 2019 00:22:22 +0200 Subject: [PATCH] redo model -> database needs to be dropped! - fix dynamic loading - add eyecandy - reorganize js --- core/admin.py | 4 +- core/migrations/0001_initial.py | 62 +++++++++++++--- core/migrations/0002_auto_20190407_2011.py | 38 ---------- core/migrations/0003_bay_name.py | 18 ----- core/migrations/0004_auto_20190409_2242.py | 19 ----- core/migrations/0005_auto_20190409_2256.py | 18 ----- core/migrations/0006_auto_20190409_2258.py | 18 ----- core/models.py | 45 ++++++++---- core/static/core/dynamic_card_load.js | 21 ++++++ core/templates/core/stablepart_detail.html | 24 +++--- core/templates/core/stablepart_list.html | 11 ++- static/base.css | 85 ++++++++++++++++++++-- static/base.js | 27 +------ templates/base.html | 2 +- 14 files changed, 204 insertions(+), 188 deletions(-) delete mode 100644 core/migrations/0002_auto_20190407_2011.py delete mode 100644 core/migrations/0003_bay_name.py delete mode 100644 core/migrations/0004_auto_20190409_2242.py delete mode 100644 core/migrations/0005_auto_20190409_2256.py delete mode 100644 core/migrations/0006_auto_20190409_2258.py create mode 100644 core/static/core/dynamic_card_load.js diff --git a/core/admin.py b/core/admin.py index 1b930ed..f87b7ca 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from core.models import StablePart, Bay, Horse, FodderPlan, Misc, IsInBay +from core.models import StablePart, Bay, Horse, FodderPlan, Misc, IsInBay, HasFodderPlan, HasMisc # Register your models here. admin.site.register(StablePart) @@ -8,3 +8,5 @@ admin.site.register(Horse) admin.site.register(FodderPlan) admin.site.register(Misc) admin.site.register(IsInBay) +admin.site.register(HasFodderPlan) +admin.site.register(HasMisc) diff --git a/core/migrations/0001_initial.py b/core/migrations/0001_initial.py index 6edecb3..0dd7d89 100644 --- a/core/migrations/0001_initial.py +++ b/core/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2 on 2019-04-06 22:06 +# Generated by Django 2.2 on 2019-04-16 21:29 from django.db import migrations, models import django.db.models.deletion @@ -16,6 +16,7 @@ class Migration(migrations.Migration): name='Bay', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(blank=True, max_length=120, null=True)), ('bay_number', models.IntegerField()), ], ), @@ -24,15 +25,38 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('fodder', models.TextField()), - ('valid_since', models.DateTimeField()), - ('valid_until', models.DateTimeField()), ], ), + migrations.CreateModel( + name='HasFodderPlan', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('valid_since', models.DateField()), + ('valid_until', models.DateField()), + ('fodderplan', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.FodderPlan')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='HasMisc', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('valid_since', models.DateField()), + ('valid_until', models.DateField()), + ], + options={ + 'abstract': False, + }, + ), migrations.CreateModel( name='Horse', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=180)), + ('colour', models.CharField(blank=True, max_length=120, null=True)), + ('markings', models.CharField(blank=True, max_length=120, null=True)), ], ), migrations.CreateModel( @@ -40,8 +64,6 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('value', models.TextField()), - ('valid_since', models.DateTimeField()), - ('valid_until', models.DateTimeField()), ], ), migrations.CreateModel( @@ -55,30 +77,48 @@ class Migration(migrations.Migration): name='IsInBay', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('in_bay_since', models.DateField()), - ('in_bay_until', models.DateField()), + ('valid_since', models.DateField()), + ('valid_until', models.DateField()), ('bay', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.Bay')), ('horse', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.Horse')), ], + options={ + 'abstract': False, + }, ), migrations.AddField( model_name='horse', name='bay', - field=models.ManyToManyField(blank=True, null=True, through='core.IsInBay', to='core.Bay'), + field=models.ManyToManyField(blank=True, related_name='horses', through='core.IsInBay', to='core.Bay'), ), migrations.AddField( model_name='horse', name='fodder', - field=models.ManyToManyField(blank=True, null=True, to='core.FodderPlan'), + field=models.ManyToManyField(blank=True, related_name='horses', through='core.HasFodderPlan', to='core.FodderPlan'), ), migrations.AddField( model_name='horse', name='misc_remarks', - field=models.ManyToManyField(blank=True, null=True, to='core.Misc'), + field=models.ManyToManyField(blank=True, related_name='horses', through='core.HasMisc', to='core.Misc'), + ), + migrations.AddField( + model_name='hasmisc', + name='horse', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.Horse'), + ), + migrations.AddField( + model_name='hasmisc', + name='misc', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.Misc'), + ), + migrations.AddField( + model_name='hasfodderplan', + name='horse', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.Horse'), ), migrations.AddField( model_name='bay', name='located_in', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.StablePart'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bays', to='core.StablePart'), ), ] diff --git a/core/migrations/0002_auto_20190407_2011.py b/core/migrations/0002_auto_20190407_2011.py deleted file mode 100644 index 3c0de7d..0000000 --- a/core/migrations/0002_auto_20190407_2011.py +++ /dev/null @@ -1,38 +0,0 @@ -# Generated by Django 2.2 on 2019-04-07 20:11 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('core', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='horse', - name='colour', - field=models.CharField(blank=True, max_length=120, null=True), - ), - migrations.AddField( - model_name='horse', - name='markings', - field=models.CharField(blank=True, max_length=120, null=True), - ), - migrations.AlterField( - model_name='horse', - name='bay', - field=models.ManyToManyField(blank=True, through='core.IsInBay', to='core.Bay'), - ), - migrations.AlterField( - model_name='horse', - name='fodder', - field=models.ManyToManyField(blank=True, to='core.FodderPlan'), - ), - migrations.AlterField( - model_name='horse', - name='misc_remarks', - field=models.ManyToManyField(blank=True, to='core.Misc'), - ), - ] diff --git a/core/migrations/0003_bay_name.py b/core/migrations/0003_bay_name.py deleted file mode 100644 index beec7a0..0000000 --- a/core/migrations/0003_bay_name.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2 on 2019-04-07 20:56 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('core', '0002_auto_20190407_2011'), - ] - - operations = [ - migrations.AddField( - model_name='bay', - name='name', - field=models.CharField(blank=True, max_length=120, null=True), - ), - ] diff --git a/core/migrations/0004_auto_20190409_2242.py b/core/migrations/0004_auto_20190409_2242.py deleted file mode 100644 index a49076a..0000000 --- a/core/migrations/0004_auto_20190409_2242.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 2.2 on 2019-04-09 22:42 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('core', '0003_bay_name'), - ] - - operations = [ - migrations.AlterField( - model_name='bay', - name='located_in', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bays', to='core.StablePart'), - ), - ] diff --git a/core/migrations/0005_auto_20190409_2256.py b/core/migrations/0005_auto_20190409_2256.py deleted file mode 100644 index a0061be..0000000 --- a/core/migrations/0005_auto_20190409_2256.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2 on 2019-04-09 22:56 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('core', '0004_auto_20190409_2242'), - ] - - operations = [ - migrations.AlterField( - model_name='horse', - name='bay', - field=models.ManyToManyField(blank=True, related_name='in_bay', through='core.IsInBay', to='core.Bay'), - ), - ] diff --git a/core/migrations/0006_auto_20190409_2258.py b/core/migrations/0006_auto_20190409_2258.py deleted file mode 100644 index c8b4184..0000000 --- a/core/migrations/0006_auto_20190409_2258.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2 on 2019-04-09 22:58 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('core', '0005_auto_20190409_2256'), - ] - - operations = [ - migrations.AlterField( - model_name='horse', - name='bay', - field=models.ManyToManyField(blank=True, related_name='horses', through='core.IsInBay', to='core.Bay'), - ), - ] diff --git a/core/models.py b/core/models.py index 5e024af..9953bdc 100644 --- a/core/models.py +++ b/core/models.py @@ -5,7 +5,7 @@ from django.db import models class StablePart(models.Model): name = models.CharField(max_length=140) - + def __str__(self): return 'Stable Part ' + self.name @@ -13,7 +13,8 @@ class StablePart(models.Model): class Bay(models.Model): name = models.CharField(max_length=120, null=True, blank=True) bay_number = models.IntegerField() - located_in = models.ForeignKey(StablePart, on_delete=models.CASCADE, related_name='bays') + located_in = models.ForeignKey( + StablePart, on_delete=models.CASCADE, related_name='bays') def __str__(self): return (self.name if self.name is not None else '')\ @@ -22,33 +23,45 @@ class Bay(models.Model): class FodderPlan(models.Model): fodder = models.TextField() - valid_since = models.DateTimeField() - valid_until = models.DateTimeField() - - def __str__(self): - return self.valid_since + ' - ' + self.valid_until class Misc(models.Model): value = models.TextField() - valid_since = models.DateTimeField() - valid_until = models.DateTimeField() class Horse(models.Model): name = models.CharField(max_length=180) colour = models.CharField(max_length=120, null=True, blank=True) markings = models.CharField(max_length=120, null=True, blank=True) - bay = models.ManyToManyField(Bay, through='IsInBay', blank=True, related_name='horses') - fodder = models.ManyToManyField(FodderPlan, blank=True) - misc_remarks = models.ManyToManyField(Misc, blank=True) - + bay = models.ManyToManyField( + Bay, through='IsInBay', blank=True, related_name='horses') + fodder = models.ManyToManyField( + FodderPlan, through='HasFodderPlan', blank=True, related_name='horses') + misc_remarks = models.ManyToManyField( + Misc, through='HasMisc', blank=True, related_name='horses') + def __str__(self): return self.name -class IsInBay(models.Model): +class IntervalBasedRelation(models.Model): + valid_since = models.DateField() + valid_until = models.DateField() + + class Meta: + abstract = True + + +class IsInBay(IntervalBasedRelation): horse = models.ForeignKey(Horse, on_delete=models.CASCADE) bay = models.ForeignKey(Bay, on_delete=models.CASCADE) - in_bay_since = models.DateField() - in_bay_until = models.DateField() + + +class HasFodderPlan(IntervalBasedRelation): + fodderplan = models.ForeignKey(FodderPlan, on_delete=models.CASCADE) + horse = models.ForeignKey(Horse, on_delete=models.CASCADE) + + +class HasMisc(IntervalBasedRelation): + misc = models.ForeignKey(Misc, on_delete=models.CASCADE) + horse = models.ForeignKey(Horse, on_delete=models.CASCADE) diff --git a/core/static/core/dynamic_card_load.js b/core/static/core/dynamic_card_load.js new file mode 100644 index 0000000..96f43c6 --- /dev/null +++ b/core/static/core/dynamic_card_load.js @@ -0,0 +1,21 @@ +// var cards; +var idToUse; + +function generateCards() { + // console.log(this.response); + // cards += this.response; + document.getElementById(idToUse).innerHTML = this.response; + // console.log(cards); +} + +function getBayCards(id, stablepartId) { + // console.log(bayIds); + // cards = ''; + idToUse = id; + const xhr = new XMLHttpRequest(); + // console.log('loading bay', bay); + xhr.open('GET', '/stablepart/'.concat(stablepartId)); + xhr.addEventListener('load', generateCards); + xhr.send(); + +} diff --git a/core/templates/core/stablepart_detail.html b/core/templates/core/stablepart_detail.html index 3ef8b7f..4826a73 100644 --- a/core/templates/core/stablepart_detail.html +++ b/core/templates/core/stablepart_detail.html @@ -1,18 +1,16 @@ -{% extends 'base.html' %} - -{% block title %}{{ object.name }} - {{ block.super }}{% endblock %} - -{% block main %} +

{{ object }}

+
+{% for bay in object.bays.all %}
-

{{ object.name }}

+

{{ bay }}

-
    - {% for bay in object.bays.all %} -
  • {{ bay }}
  • - {% empty %} -
  • EMPTY
  • + {% for inbay in bay.isinbay_set.all %} +

    +
    {{ inbay.valid_since }} bis {{ inbay.valid_until }}
    +
    {% endfor %} -
-{% endblock %} +{% endfor %} +
+
diff --git a/core/templates/core/stablepart_list.html b/core/templates/core/stablepart_list.html index 40625a3..4d50e2b 100644 --- a/core/templates/core/stablepart_list.html +++ b/core/templates/core/stablepart_list.html @@ -6,11 +6,14 @@
{% endblock %} {% block nav %} - {% endblock %} diff --git a/static/base.css b/static/base.css index 1342f84..9405e2c 100644 --- a/static/base.css +++ b/static/base.css @@ -9,7 +9,7 @@ html { body { display: grid; grid-template-columns: 15em 1fr; - grid-template-rows: 1fr 6fr 1fr; + grid-template-rows: 1fr 6fr 2fr; grid-template-areas: "header header" "nav main" @@ -20,28 +20,43 @@ body { div#header { grid-area: header; - border: 1px solid red; + /* border: 1px solid red; */ + display: flex; + flex-direction: row; + justify-content: space-between; + background: rebeccapurple; + color: lightgrey; +} + +div#header > div#headertitle { + font-size: xx-large; + font-weight: bolder; + vertical-align: middle; + /* background: magenta; */ + text-align: center; } div#nav { grid-area: nav; - border: 1px solid red; + /* border: 1px solid red; */ } div#main { grid-area: main; - border: 1px solid red; + overflow-y: auto; + /* border: 1px solid red; */ } div#footer { - grid-area: footer - border: 1px solid red; + grid-area: footer; + border-top: 2px solid darkgrey; } div.card { box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2); transition: 0.3s; border-radius: 5px; + margin: 5px 0; } div.card:hover { @@ -60,3 +75,61 @@ div.card > h1,h2,h3,h4 { margin: 0; border-bottom: 1px solid lightgrey; } + +button.block { + display: block; + width: 100%; + border: none; + background-color: darkcyan; + padding: 10px 20px; + cursor: pointer; + text-align: center; + color: white; + margin: 5px 0; +} + +button.block:hover { + background-color: darkslategrey; +} + +button.text { + border: none; + background-color: inherit; + padding: 10px 20px; + cursor: pointer; + display: inline-block; + color: black; + font-size: inherit; + font-style: inherit; + font-weight: inherit; +} + +button.text:hover { + background: #eee; +} + +button { + transition-duration: 0.4s; + overflow: hidden; + cursor: pointer; +} + +button:after { + content: ""; + background: #a00; + /* display: block; */ + /* position: absolute; */ + padding-top: 300%; + /* padding-left: 350%; */ + /* margin-left: -20px!important; */ + /* margin-top: -120%; */ + opacity: 0; + transition: all 0.8s; +} + +button:active:after { + padding: 0; + /* margin: 0; */ + opacity: 1; + transition: 0s; +} diff --git a/static/base.js b/static/base.js index 7845bff..1c6cb41 100644 --- a/static/base.js +++ b/static/base.js @@ -1,9 +1,4 @@ -function setMainCards( id, cards ) { - document.getElementById(id).innerHTML = cards; - -} - -function generateCard( title, content ) { +function generateCard(title, content) { return '

'.concat(title).concat('

').concat(content).concat('
'); } @@ -11,22 +6,4 @@ function generateCardFromObject(card) { return generateCard(card.title, card.content); } -function generateCards() { - // console.log(this.response); - cards += this.response; - document.getElementById('bays').innerHTML = cards; - // console.log(cards); -} -var cards; -function getBayCards(id, bayIds) { - // console.log(bayIds); - cards = ''; - - for (const bay of bayIds) { - const xhr = new XMLHttpRequest(); - // console.log('loading bay', bay); - xhr.open('GET', '/bay/'.concat(bay)); - xhr.addEventListener('load', generateCards); - xhr.send(); - } -} + diff --git a/templates/base.html b/templates/base.html index 0c9c994..0bce048 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,7 +9,7 @@ {% block title %} Stable Management {% endblock %} - +
{% block main %}{% endblock %}
-- 2.47.0