From 812f74d79fc5fbbc7e8a8fc324b6be8052a0e559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 18 Feb 2023 20:49:46 +0100 Subject: [PATCH] Add support for game editions (#28) --- CHANGELOG.md | 4 ++ games/forms.py | 16 ++++++-- games/migrations/0008_edition.py | 41 +++++++++++++++++++ games/migrations/0009_create_editions.py | 34 +++++++++++++++ games/migrations/0010_alter_purchase_game.py | 21 ++++++++++ .../0011_rename_game_purchase_edition.py | 18 ++++++++ games/models.py | 13 +++++- games/templates/base.html | 3 ++ games/templates/list_sessions.html | 13 +++--- games/urls.py | 7 ++-- games/views.py | 25 ++++++++--- 11 files changed, 172 insertions(+), 23 deletions(-) create mode 100644 games/migrations/0008_edition.py create mode 100644 games/migrations/0009_create_editions.py create mode 100644 games/migrations/0010_alter_purchase_game.py create mode 100644 games/migrations/0011_rename_game_purchase_edition.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 50c2b59..a51ab95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +* Add support for game editions (https://git.kucharczyk.xyz/lukas/timetracker/issues/28) + ## 1.0.1 / 2023-01-30 22:17+01:00 * Make it possible to edit sessions (https://git.kucharczyk.xyz/lukas/timetracker/issues/46) diff --git a/games/forms.py b/games/forms.py index 01ce376..708a6c3 100644 --- a/games/forms.py +++ b/games/forms.py @@ -1,10 +1,12 @@ from django import forms -from games.models import Game, Platform, Purchase, Session +from games.models import Game, Platform, Purchase, Session, Edition class SessionForm(forms.ModelForm): - purchase = forms.ModelChoiceField(queryset=Purchase.objects.order_by("game__name")) + purchase = forms.ModelChoiceField( + queryset=Purchase.objects.order_by("edition__name") + ) class Meta: model = Session @@ -18,12 +20,18 @@ class SessionForm(forms.ModelForm): class PurchaseForm(forms.ModelForm): - game = forms.ModelChoiceField(queryset=Game.objects.order_by("name")) + edition = forms.ModelChoiceField(queryset=Edition.objects.order_by("name")) platform = forms.ModelChoiceField(queryset=Platform.objects.order_by("name")) class Meta: model = Purchase - fields = ["game", "platform", "date_purchased", "date_refunded"] + fields = ["edition", "platform", "date_purchased", "date_refunded"] + + +class EditionForm(forms.ModelForm): + class Meta: + model = Edition + fields = ["game", "name", "platform"] class GameForm(forms.ModelForm): diff --git a/games/migrations/0008_edition.py b/games/migrations/0008_edition.py new file mode 100644 index 0000000..1da157a --- /dev/null +++ b/games/migrations/0008_edition.py @@ -0,0 +1,41 @@ +# Generated by Django 4.1.5 on 2023-02-18 16:29 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("games", "0007_alter_purchase_game_alter_purchase_platform_and_more"), + ] + + operations = [ + migrations.CreateModel( + name="Edition", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255)), + ( + "game", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="games.game" + ), + ), + ( + "platform", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="games.platform" + ), + ), + ], + ), + ] diff --git a/games/migrations/0009_create_editions.py b/games/migrations/0009_create_editions.py new file mode 100644 index 0000000..662485c --- /dev/null +++ b/games/migrations/0009_create_editions.py @@ -0,0 +1,34 @@ +# Generated by Django 4.1.5 on 2023-02-18 18:51 + +from django.db import migrations + + +def create_edition_of_game(apps, schema_editor): + Game = apps.get_model("games", "Game") + Edition = apps.get_model("games", "Edition") + Platform = apps.get_model("games", "Platform") + first_platform = Platform.objects.first() + all_games = Game.objects.all() + all_editions = Edition.objects.all() + for game in all_games: + existing_edition = None + try: + existing_edition = all_editions.objects.get(game=game.id) + except: + pass + if existing_edition == None: + edition = Edition() + edition.id = game.id + edition.game = game + edition.name = game.name + edition.platform = first_platform + edition.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("games", "0008_edition"), + ] + + operations = [migrations.RunPython(create_edition_of_game)] diff --git a/games/migrations/0010_alter_purchase_game.py b/games/migrations/0010_alter_purchase_game.py new file mode 100644 index 0000000..925eab5 --- /dev/null +++ b/games/migrations/0010_alter_purchase_game.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.5 on 2023-02-18 19:06 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("games", "0009_create_editions"), + ] + + operations = [ + migrations.AlterField( + model_name="purchase", + name="game", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="games.edition" + ), + ), + ] diff --git a/games/migrations/0011_rename_game_purchase_edition.py b/games/migrations/0011_rename_game_purchase_edition.py new file mode 100644 index 0000000..741ea74 --- /dev/null +++ b/games/migrations/0011_rename_game_purchase_edition.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.5 on 2023-02-18 19:18 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("games", "0010_alter_purchase_game"), + ] + + operations = [ + migrations.RenameField( + model_name="purchase", + old_name="game", + new_name="edition", + ), + ] diff --git a/games/models.py b/games/models.py index 2d6d916..7969df4 100644 --- a/games/models.py +++ b/games/models.py @@ -16,14 +16,23 @@ class Game(models.Model): return self.name -class Purchase(models.Model): +class Edition(models.Model): game = models.ForeignKey("Game", on_delete=models.CASCADE) + name = models.CharField(max_length=255) + platform = models.ForeignKey("Platform", on_delete=models.CASCADE) + + def __str__(self): + return self.name + + +class Purchase(models.Model): + edition = models.ForeignKey("Edition", on_delete=models.CASCADE) platform = models.ForeignKey("Platform", on_delete=models.CASCADE) date_purchased = models.DateField() date_refunded = models.DateField(blank=True, null=True) def __str__(self): - return f"{self.game} ({self.platform})" + return f"{self.edition} ({self.platform})" class Platform(models.Model): diff --git a/games/templates/base.html b/games/templates/base.html index c65cdc5..efd432e 100644 --- a/games/templates/base.html +++ b/games/templates/base.html @@ -27,6 +27,9 @@
  • New Game
  • New Platform
  • {% if game_available and platform_available %} +
  • New Edition
  • + {% endif %} + {% if edition_available %}
  • New Purchase
  • {% endif %} {% if purchase_available %} diff --git a/games/templates/list_sessions.html b/games/templates/list_sessions.html index c552511..e1cb812 100644 --- a/games/templates/list_sessions.html +++ b/games/templates/list_sessions.html @@ -10,15 +10,12 @@ {% if dataset.count >= 1 %}
    Total playtime: {{ total_duration }} over {{ dataset.count }} sessions.
    {% endif %} - {% if purchase or platform or game %} - - - - Filtering by "{% firstof purchase platform game %}" - {% if purchase %}See all platforms{% endif %} + {% if purchase or platform or edition %} + Filtering by "{% firstof purchase platform edition %}" + {% if purchase %}See all platforms{% endif %} {% endif %} {% if dataset.count >= 1 %} - +