Compare commits

..

No commits in common. "d4ab0596da1765ec8ace73d74f1e2c50f842b8ba" and "8296ebcf3165f51cd4df0de34a5f31aa71a26a2d" have entirely different histories.

12 changed files with 30 additions and 195 deletions

View File

@ -1,7 +1,3 @@
## Unreleased
* Add support for game editions (https://git.kucharczyk.xyz/lukas/timetracker/issues/28)
## 1.0.1 / 2023-01-30 22:17+01:00 ## 1.0.1 / 2023-01-30 22:17+01:00
* Make it possible to edit sessions (https://git.kucharczyk.xyz/lukas/timetracker/issues/46) * Make it possible to edit sessions (https://git.kucharczyk.xyz/lukas/timetracker/issues/46)

View File

@ -7,11 +7,11 @@ HTMLFILES := $(shell find games/templates -type f)
npm: npm:
npm install npm install
css: common/input.css css: input.css
npx tailwindcss -i ./common/input.css -o ./games/static/base.css npx tailwindcss -i ./input.css -o ./games/static/base.css
css-dev: css css-dev: css
npx tailwindcss -i ./common/input.css -o ./games/static/base.css --watch npx tailwindcss -i ./input.css -o ./games/static/base.css --watch
makemigrations: makemigrations:
poetry run python manage.py makemigrations poetry run python manage.py makemigrations

View File

@ -1,12 +1,10 @@
from django import forms from django import forms
from games.models import Game, Platform, Purchase, Session, Edition from games.models import Game, Platform, Purchase, Session
class SessionForm(forms.ModelForm): class SessionForm(forms.ModelForm):
purchase = forms.ModelChoiceField( purchase = forms.ModelChoiceField(queryset=Purchase.objects.order_by("game__name"))
queryset=Purchase.objects.order_by("edition__name")
)
class Meta: class Meta:
model = Session model = Session
@ -20,18 +18,12 @@ class SessionForm(forms.ModelForm):
class PurchaseForm(forms.ModelForm): class PurchaseForm(forms.ModelForm):
edition = forms.ModelChoiceField(queryset=Edition.objects.order_by("name")) game = forms.ModelChoiceField(queryset=Game.objects.order_by("name"))
platform = forms.ModelChoiceField(queryset=Platform.objects.order_by("name")) platform = forms.ModelChoiceField(queryset=Platform.objects.order_by("name"))
class Meta: class Meta:
model = Purchase model = Purchase
fields = ["edition", "platform", "date_purchased", "date_refunded"] fields = ["game", "platform", "date_purchased", "date_refunded"]
class EditionForm(forms.ModelForm):
class Meta:
model = Edition
fields = ["game", "name", "platform"]
class GameForm(forms.ModelForm): class GameForm(forms.ModelForm):

View File

@ -1,41 +0,0 @@
# 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"
),
),
],
),
]

View File

@ -1,34 +0,0 @@
# 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)]

View File

@ -1,21 +0,0 @@
# 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"
),
),
]

View File

@ -1,18 +0,0 @@
# 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",
),
]

View File

@ -16,23 +16,14 @@ class Game(models.Model):
return self.name return self.name
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): class Purchase(models.Model):
edition = models.ForeignKey("Edition", on_delete=models.CASCADE) game = models.ForeignKey("Game", on_delete=models.CASCADE)
platform = models.ForeignKey("Platform", on_delete=models.CASCADE) platform = models.ForeignKey("Platform", on_delete=models.CASCADE)
date_purchased = models.DateField() date_purchased = models.DateField()
date_refunded = models.DateField(blank=True, null=True) date_refunded = models.DateField(blank=True, null=True)
def __str__(self): def __str__(self):
return f"{self.edition} ({self.platform})" return f"{self.game} ({self.platform})"
class Platform(models.Model): class Platform(models.Model):

View File

@ -27,9 +27,6 @@
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_game' %}">New Game</a></li> <li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_game' %}">New Game</a></li>
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_platform' %}">New Platform</a></li> <li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_platform' %}">New Platform</a></li>
{% if game_available and platform_available %} {% if game_available and platform_available %}
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_edition' %}">New Edition</a></li>
{% endif %}
{% if edition_available %}
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_purchase' %}">New Purchase</a></li> <li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_purchase' %}">New Purchase</a></li>
{% endif %} {% endif %}
{% if purchase_available %} {% if purchase_available %}

View File

@ -10,18 +10,15 @@
{% if dataset.count >= 1 %} {% if dataset.count >= 1 %}
<div class="mb-4">Total playtime: {{ total_duration }} over {{ dataset.count }} sessions.</div> <div class="mb-4">Total playtime: {{ total_duration }} over {{ dataset.count }} sessions.</div>
{% endif %} {% endif %}
{% if purchase or platform or edition %} {% if purchase or platform or game %}
<span class="block"> <a class="text-red-400 inline" href="{% url 'list_sessions' %}"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 inline">
<a class="text-red-400 inline" href="{% url 'list_sessions' %}"> <path stroke-linecap="round" stroke-linejoin="round" d="M9.75 9.75l4.5 4.5m0-4.5l-4.5 4.5M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 inline"> </svg>
<path stroke-linecap="round" stroke-linejoin="round" d="M9.75 9.75l4.5 4.5m0-4.5l-4.5 4.5M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> </a><span>Filtering by "{% firstof purchase platform game %}"</span>
</svg> {% if purchase %}<a class="dark:text-white hover:underline block" href="{% url 'list_sessions_by_game' purchase.game.id %}">See all platforms</a>{% endif %}
</a>Filtering by "{% firstof purchase platform edition %}"
</span>
{% if purchase %}<a class="dark:text-white hover:underline block" href="{% url 'list_sessions_by_edition' purchase.edition.id %}">See all platforms</a>{% endif %}
{% endif %} {% endif %}
{% if dataset.count >= 1 %} {% if dataset.count >= 1 %}
<a href="{% url 'start_session' last.purchase.id %}"> <a class="clear-both" href="{% url 'start_session' last.purchase.id %}">
<button type="button" title="Track last tracked" class="mt-10 py-1 px-2 bg-green-600 hover:bg-green-700 focus:ring-green-500 focus:ring-offset-blue-200 text-white transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-lg "> <button type="button" title="Track last tracked" class="mt-10 py-1 px-2 bg-green-600 hover:bg-green-700 focus:ring-green-500 focus:ring-offset-blue-200 text-white transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-lg ">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="self-center w-6 h-6 inline"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="self-center w-6 h-6 inline">
<path stroke-linecap="round" stroke-linejoin="round" d="M5.25 5.653c0-.856.917-1.398 1.667-.986l11.54 6.348a1.125 1.125 0 010 1.971l-11.54 6.347a1.125 1.125 0 01-1.667-.985V5.653z" /> <path stroke-linecap="round" stroke-linejoin="round" d="M5.25 5.653c0-.856.917-1.398 1.667-.986l11.54 6.348a1.125 1.125 0 010 1.971l-11.54 6.347a1.125 1.125 0 01-1.667-.985V5.653z" />
@ -39,7 +36,7 @@
<div class="dark:border-white dark:text-slate-300 text-lg">Duration</div> <div class="dark:border-white dark:text-slate-300 text-lg">Duration</div>
<div class="dark:border-white dark:text-slate-300 text-lg text-right">Manage</div> <div class="dark:border-white dark:text-slate-300 text-lg text-right">Manage</div>
{% for data in dataset %} {% for data in dataset %}
<div class="dark:text-white overflow-hidden text-ellipsis whitespace-nowrap"><a class="hover:underline" href="{% url 'list_sessions_by_purchase' data.purchase.id %}">{{ data.purchase.edition }}</a></div> <div class="dark:text-white overflow-hidden text-ellipsis whitespace-nowrap"><a class="hover:underline" href="{% url 'list_sessions_by_purchase' data.purchase.id %}">{{ data.purchase.game }}</a></div>
<div class="dark:text-white overflow-hidden text-ellipsis whitespace-nowrap"><a class="hover:underline" href="{% url 'list_sessions_by_platform' data.purchase.platform.id %}">{{ data.purchase.platform }}</a></div> <div class="dark:text-white overflow-hidden text-ellipsis whitespace-nowrap"><a class="hover:underline" href="{% url 'list_sessions_by_platform' data.purchase.platform.id %}">{{ data.purchase.platform }}</a></div>
<div class="dark:text-slate-400 text-center">{{ data.timestamp_start | date:"d/m/Y H:i" }}</div> <div class="dark:text-slate-400 text-center">{{ data.timestamp_start | date:"d/m/Y H:i" }}</div>
<div class="dark:text-slate-400 text-center"> <div class="dark:text-slate-400 text-center">

View File

@ -3,13 +3,7 @@ from django.urls import path
from games import views from games import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.list_sessions, {"filter": "recent"}, name="list_sessions_recent"),
path(
"list-sessions/recent",
views.list_sessions,
{"filter": "recent"},
name="list_sessions_recent",
),
path("add-game/", views.add_game, name="add_game"), path("add-game/", views.add_game, name="add_game"),
path("add-platform/", views.add_platform, name="add_platform"), path("add-platform/", views.add_platform, name="add_platform"),
path("add-session/", views.add_session, name="add_session"), path("add-session/", views.add_session, name="add_session"),
@ -29,7 +23,6 @@ urlpatterns = [
name="delete_session", name="delete_session",
), ),
path("add-purchase/", views.add_purchase, name="add_purchase"), path("add-purchase/", views.add_purchase, name="add_purchase"),
path("add-edition/", views.add_edition, name="add_edition"),
path("edit-session/<int:session_id>", views.edit_session, name="edit_session"), path("edit-session/<int:session_id>", views.edit_session, name="edit_session"),
path("list-sessions/", views.list_sessions, name="list_sessions"), path("list-sessions/", views.list_sessions, name="list_sessions"),
path( path(
@ -45,9 +38,9 @@ urlpatterns = [
name="list_sessions_by_platform", name="list_sessions_by_platform",
), ),
path( path(
"list-sessions/by-edition/<int:edition_id>", "list-sessions/by-game/<int:game_id>",
views.list_sessions, views.list_sessions,
{"filter": "edition"}, {"filter": "game"},
name="list_sessions_by_edition", name="list_sessions_by_game",
), ),
] ]

View File

@ -6,14 +6,13 @@ from common.time import now as now_with_tz
from django.conf import settings from django.conf import settings
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from .forms import GameForm, PlatformForm, PurchaseForm, SessionForm, EditionForm from .forms import GameForm, PlatformForm, PurchaseForm, SessionForm
from .models import Game, Platform, Purchase, Session, Edition from .models import Game, Platform, Purchase, Session
def model_counts(request): def model_counts(request):
return { return {
"game_available": Game.objects.count() != 0, "game_available": Game.objects.count() != 0,
"edition_available": Edition.objects.count() != 0,
"platform_available": Platform.objects.count() != 0, "platform_available": Platform.objects.count() != 0,
"purchase_available": Purchase.objects.count() != 0, "purchase_available": Purchase.objects.count() != 0,
"session_count": Session.objects.count(), "session_count": Session.objects.count(),
@ -72,7 +71,7 @@ def delete_session(request, session_id=None):
return redirect("list_sessions") return redirect("list_sessions")
def list_sessions(request, filter="", purchase_id="", platform_id="", edition_id=""): def list_sessions(request, filter="", purchase_id="", platform_id="", game_id=""):
context = {} context = {}
context["title"] = "Sessions" context["title"] = "Sessions"
@ -82,9 +81,9 @@ def list_sessions(request, filter="", purchase_id="", platform_id="", edition_id
elif filter == "platform": elif filter == "platform":
dataset = Session.objects.filter(purchase__platform=platform_id) dataset = Session.objects.filter(purchase__platform=platform_id)
context["platform"] = Platform.objects.get(id=platform_id) context["platform"] = Platform.objects.get(id=platform_id)
elif filter == "edition": elif filter == "game":
dataset = Session.objects.filter(purchase__edition=edition_id) dataset = Session.objects.filter(purchase__game=game_id)
context["edition"] = Edition.objects.get(id=edition_id) context["game"] = Game.objects.get(id=game_id)
elif filter == "recent": elif filter == "recent":
dataset = Session.objects.filter( dataset = Session.objects.filter(
timestamp_start__gte=datetime.now() - timedelta(days=30) timestamp_start__gte=datetime.now() - timedelta(days=30)
@ -117,7 +116,7 @@ def add_purchase(request):
form = PurchaseForm(request.POST or None, initial=initial) form = PurchaseForm(request.POST or None, initial=initial)
if form.is_valid(): if form.is_valid():
form.save() form.save()
return redirect("index") return redirect("list_sessions")
context["form"] = form context["form"] = form
context["title"] = "Add New Purchase" context["title"] = "Add New Purchase"
@ -129,36 +128,20 @@ def add_game(request):
form = GameForm(request.POST or None) form = GameForm(request.POST or None)
if form.is_valid(): if form.is_valid():
form.save() form.save()
return redirect("index") return redirect("list_sessions")
context["form"] = form context["form"] = form
context["title"] = "Add New Game" context["title"] = "Add New Game"
return render(request, "add.html", context) return render(request, "add.html", context)
def add_edition(request):
context = {}
form = EditionForm(request.POST or None)
if form.is_valid():
form.save()
return redirect("index")
context["form"] = form
context["title"] = "Add New Edition"
return render(request, "add.html", context)
def add_platform(request): def add_platform(request):
context = {} context = {}
form = PlatformForm(request.POST or None) form = PlatformForm(request.POST or None)
if form.is_valid(): if form.is_valid():
form.save() form.save()
return redirect("index") return redirect("list_sessions")
context["form"] = form context["form"] = form
context["title"] = "Add New Platform" context["title"] = "Add New Platform"
return render(request, "add.html", context) return render(request, "add.html", context)
def index(request):
return redirect("list_sessions_recent")