Compare commits
1 Commits
2d8eb32e90
...
purchase_s
Author | SHA1 | Date | |
---|---|---|---|
a30c54ef44
|
@ -3,9 +3,6 @@
|
|||||||
## Improved
|
## Improved
|
||||||
* game overview: improve how editions and purchases are displayed
|
* game overview: improve how editions and purchases are displayed
|
||||||
* add purchase: only allow choosing purchases of selected edition
|
* add purchase: only allow choosing purchases of selected edition
|
||||||
* session list:
|
|
||||||
* starting and ending sessions is much faster/doest not reload the page
|
|
||||||
* listing sessions is much faster
|
|
||||||
|
|
||||||
## 1.5.1 / 2023-11-14 21:10+01:00
|
## 1.5.1 / 2023-11-14 21:10+01:00
|
||||||
|
|
||||||
|
@ -83,7 +83,6 @@ class PurchaseForm(forms.ModelForm):
|
|||||||
"date_purchased": custom_date_widget,
|
"date_purchased": custom_date_widget,
|
||||||
"date_refunded": custom_date_widget,
|
"date_refunded": custom_date_widget,
|
||||||
"date_finished": custom_date_widget,
|
"date_finished": custom_date_widget,
|
||||||
"date_dropped": custom_date_widget,
|
|
||||||
}
|
}
|
||||||
model = Purchase
|
model = Purchase
|
||||||
fields = [
|
fields = [
|
||||||
@ -92,8 +91,7 @@ class PurchaseForm(forms.ModelForm):
|
|||||||
"date_purchased",
|
"date_purchased",
|
||||||
"date_refunded",
|
"date_refunded",
|
||||||
"date_finished",
|
"date_finished",
|
||||||
"date_dropped",
|
"status",
|
||||||
"infinite",
|
|
||||||
"price",
|
"price",
|
||||||
"price_currency",
|
"price_currency",
|
||||||
"ownership_type",
|
"ownership_type",
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
# Generated by Django 4.2.7 on 2024-01-03 21:27
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
("games", "0033_alter_edition_unique_together"),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.AddField(
|
|
||||||
model_name="purchase",
|
|
||||||
name="date_dropped",
|
|
||||||
field=models.DateField(blank=True, null=True),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name="purchase",
|
|
||||||
name="infinite",
|
|
||||||
field=models.BooleanField(default=False),
|
|
||||||
),
|
|
||||||
]
|
|
26
games/migrations/0034_purchase_status.py
Normal file
26
games/migrations/0034_purchase_status.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Generated by Django 4.2.7 on 2023-12-22 10:07
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("games", "0033_alter_edition_unique_together"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="purchase",
|
||||||
|
name="status",
|
||||||
|
field=models.IntegerField(
|
||||||
|
choices=[
|
||||||
|
(0, "Unplayed"),
|
||||||
|
(1, "Playing"),
|
||||||
|
(2, "Dropped"),
|
||||||
|
(3, "Finished"),
|
||||||
|
],
|
||||||
|
default=0,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
22
games/migrations/0035_auto_20231222_1109.py
Normal file
22
games/migrations/0035_auto_20231222_1109.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Generated by Django 4.2.7 on 2023-12-22 10:09
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
from games.models import Purchase
|
||||||
|
|
||||||
|
|
||||||
|
def set_default_state(apps, schema_editor):
|
||||||
|
Purchase.objects.filter(session__isnull=False).update(
|
||||||
|
status=Purchase.PurchaseState.PLAYING
|
||||||
|
)
|
||||||
|
Purchase.objects.filter(date_finished__isnull=False).update(
|
||||||
|
status=Purchase.PurchaseState.FINISHED
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("games", "0034_purchase_status"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [migrations.RunPython(set_default_state)]
|
@ -116,8 +116,6 @@ class Purchase(models.Model):
|
|||||||
date_purchased = models.DateField()
|
date_purchased = models.DateField()
|
||||||
date_refunded = models.DateField(blank=True, null=True)
|
date_refunded = models.DateField(blank=True, null=True)
|
||||||
date_finished = models.DateField(blank=True, null=True)
|
date_finished = models.DateField(blank=True, null=True)
|
||||||
date_dropped = models.DateField(blank=True, null=True)
|
|
||||||
infinite = models.BooleanField(default=False)
|
|
||||||
price = models.IntegerField(default=0)
|
price = models.IntegerField(default=0)
|
||||||
price_currency = models.CharField(max_length=3, default="USD")
|
price_currency = models.CharField(max_length=3, default="USD")
|
||||||
ownership_type = models.CharField(
|
ownership_type = models.CharField(
|
||||||
@ -135,6 +133,25 @@ class Purchase(models.Model):
|
|||||||
)
|
)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
class PurchaseState(models.IntegerChoices):
|
||||||
|
UNPLAYED = (
|
||||||
|
0,
|
||||||
|
"Unplayed",
|
||||||
|
)
|
||||||
|
PLAYING = (1, "Playing")
|
||||||
|
DROPPED = (
|
||||||
|
2,
|
||||||
|
"Dropped",
|
||||||
|
)
|
||||||
|
FINISHED = (
|
||||||
|
3,
|
||||||
|
"Finished",
|
||||||
|
)
|
||||||
|
|
||||||
|
status = models.IntegerField(
|
||||||
|
choices=PurchaseState.choices, default=PurchaseState.UNPLAYED
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
additional_info = [
|
additional_info = [
|
||||||
self.get_type_display() if self.type != Purchase.GAME else "",
|
self.get_type_display() if self.type != Purchase.GAME else "",
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
{% load django_htmx %}
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
{% load static %}
|
{% load static %}
|
||||||
@ -13,7 +12,6 @@
|
|||||||
{% endblock title %}
|
{% endblock title %}
|
||||||
</title>
|
</title>
|
||||||
<script src="{% static 'js/htmx.min.js' %}"></script>
|
<script src="{% static 'js/htmx.min.js' %}"></script>
|
||||||
{% django_htmx_script %}
|
|
||||||
<link rel="stylesheet" href="{% static 'base.css' %}" />
|
<link rel="stylesheet" href="{% static 'base.css' %}" />
|
||||||
</head>
|
</head>
|
||||||
<body class="dark" hx-indicator="#indicator">
|
<body class="dark" hx-indicator="#indicator">
|
||||||
|
@ -4,20 +4,21 @@
|
|||||||
{{ title }}
|
{{ title }}
|
||||||
{% endblock title %}
|
{% endblock title %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% if dataset_count >= 1 %}
|
{% if dataset.count >= 1 %}
|
||||||
<div class="mx-auto text-center my-4">
|
<div class="mx-auto text-center my-4">
|
||||||
<a id="last-session-start"
|
<a id="last-session-start"
|
||||||
href="{% url 'start_session_same_as_last' last.id %}"
|
href="{% url 'start_session_same_as_last' last.id %}"
|
||||||
hx-get="{% url 'start_session_same_as_last' last.id %}"
|
hx-get="{% url 'start_session_same_as_last' last.id %}"
|
||||||
hx-swap="afterbegin"
|
hx-swap="afterbegin"
|
||||||
hx-target=".responsive-table tbody"
|
hx-target=".responsive-table tbody"
|
||||||
onClick="document.querySelector('#last-session-start').classList.add('invisible')"
|
hx-select=".responsive-table tbody tr:first-child"
|
||||||
class="{% if last.timestamp_end == null %}invisible{% endif %}">
|
onClick="document.querySelector('#last-session-start').classList.add('invisible')"
|
||||||
|
class="{% if last.timestamp_end == null %}invisible{% endif %}">
|
||||||
{% include 'components/button_start.html' with text=last.purchase title="Start session of last played game" only %}
|
{% include 'components/button_start.html' with text=last.purchase title="Start session of last played game" only %}
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if dataset_count != 0 %}
|
{% if dataset.count != 0 %}
|
||||||
<table class="responsive-table">
|
<table class="responsive-table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@ -28,37 +29,36 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for session in dataset %}
|
{% for data in dataset %}
|
||||||
{% partialdef session-row inline=True %}
|
<tr>
|
||||||
<tr>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 purchase-name truncate max-w-20char md:max-w-40char">
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 purchase-name truncate max-w-20char md:max-w-40char">
|
<a class="underline decoration-slate-500 sm:decoration-2"
|
||||||
<a class="underline decoration-slate-500 sm:decoration-2"
|
href="{% url 'view_game' data.purchase.edition.game.id %}">
|
||||||
href="{% url 'view_game' session.purchase.edition.game.id %}">
|
{{ data.purchase.edition }}
|
||||||
{{ session.purchase.edition }}
|
</a>
|
||||||
</a>
|
</td>
|
||||||
</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono hidden sm:table-cell">
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono hidden sm:table-cell">
|
{{ data.timestamp_start | date:"d/m/Y H:i" }}
|
||||||
{{ session.timestamp_start | date:"d/m/Y H:i" }}
|
</td>
|
||||||
</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono hidden lg:table-cell">
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono hidden lg:table-cell">
|
{% if data.unfinished %}
|
||||||
{% if not session.timestamp_end %}
|
<a href="{% url 'update_session' data.id %}"
|
||||||
<a href="{% url 'update_session' session.id %}"
|
hx-get="{% url 'update_session' data.id %}"
|
||||||
hx-get="{% url 'update_session' session.id %}"
|
hx-swap="outerHTML"
|
||||||
hx-target="closest tr"
|
hx-target=".responsive-table tbody tr:first-child"
|
||||||
hx-swap="outerHTML"
|
hx-select=".responsive-table tbody tr:first-child"
|
||||||
hx-indicator="#indicator"
|
hx-indicator="#indicator"
|
||||||
onClick="document.querySelector('#last-session-start').classList.remove('invisible')">
|
onClick="document.querySelector('#last-session-start').classList.remove('invisible')">
|
||||||
<span class="text-yellow-300">Finish now?</span>
|
<span class="text-yellow-300">Finish now?</span>
|
||||||
</a>
|
</a>
|
||||||
{% elif session.duration_manual %}
|
{% elif data.duration_manual %}
|
||||||
--
|
--
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ session.timestamp_end | date:"d/m/Y H:i" }}
|
{{ data.timestamp_end | date:"d/m/Y H:i" }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ session.duration_formatted }}</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ data.duration_formatted }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endpartialdef %}
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
@ -43,11 +43,11 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Finished</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2">Finished</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ all_finished_this_year_count }}</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ all_finished_this_year.count }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Finished ({{ year }})</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2">Finished ({{ year }})</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ this_year_finished_this_year_count }}</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ this_year_finished_this_year.count }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Longest session</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2">Longest session</td>
|
||||||
@ -63,14 +63,6 @@
|
|||||||
{{ highest_session_average }} ({{ highest_session_average_game }})
|
{{ highest_session_average }} ({{ highest_session_average_game }})
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">First play</td>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ first_play_name }} ({{ first_play_date }})</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Last play</td>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ last_play_name }} ({{ last_play_date }})</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<h1 class="text-5xl text-center my-6">Purchases</h1>
|
<h1 class="text-5xl text-center my-6">Purchases</h1>
|
||||||
@ -78,18 +70,18 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Total</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2">Total</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ all_purchased_this_year_count }}</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ all_purchased_this_year.count }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Refunded</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2">Refunded</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
|
||||||
{{ all_purchased_refunded_this_year_count }} ({{ refunded_percent }}%)
|
{{ all_purchased_refunded_this_year.count }} ({{ refunded_percent }}%)
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2">Unfinished</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2">Unfinished</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
|
||||||
{{ purchased_unfinished_count }} ({{ unfinished_purchases_percent }}%)
|
{{ purchased_unfinished.count }} ({{ unfinished_purchases_percent }}%)
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -145,6 +137,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th class="px-2 sm:px-4 md:px-6 md:py-2 purchase-name truncate max-w-20char">Name</th>
|
<th class="px-2 sm:px-4 md:px-6 md:py-2 purchase-name truncate max-w-20char">Name</th>
|
||||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Date</th>
|
<th class="px-2 sm:px-4 md:px-6 md:py-2">Date</th>
|
||||||
|
<th class="px-2 sm:px-4 md:px-6 md:py-2">Playtime</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@ -161,6 +154,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.date_finished | date:"d/m/Y" }}</td>
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.date_finished | date:"d/m/Y" }}</td>
|
||||||
|
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.formatted_playtime }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
@ -205,33 +199,6 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<h1 class="text-5xl text-center my-6">Unfinished Purchases</h1>
|
|
||||||
<table class="responsive-table">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th class="px-2 sm:px-4 md:px-6 md:py-2 purchase-name truncate max-w-20char">Name</th>
|
|
||||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Price ({{ total_spent_currency }})</th>
|
|
||||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Date</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for purchase in purchased_unfinished %}
|
|
||||||
<tr>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
|
|
||||||
<a class="underline decoration-slate-500 sm:decoration-2"
|
|
||||||
href="{% url 'edit_purchase' purchase.id %}">
|
|
||||||
{{ purchase.edition.name }}
|
|
||||||
{% if purchase.type == "dlc" %}({{ purchase.name }}, {{ purchase.get_type_display }}){% endif %}
|
|
||||||
</a>
|
|
||||||
</td>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.price }}</td>
|
|
||||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.date_purchased | date:"d/m/Y" }}</td>
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h1 class="text-5xl text-center my-6">All Purchases</h1>
|
<h1 class="text-5xl text-center my-6">All Purchases</h1>
|
||||||
<table class="responsive-table">
|
<table class="responsive-table">
|
||||||
<thead>
|
<thead>
|
||||||
|
194
games/views.py
194
games/views.py
@ -1,17 +1,9 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from typing import Any, Callable
|
from typing import Any, Callable
|
||||||
|
|
||||||
from django.db.models import (
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
Avg,
|
from django.db.models import Avg, Count, ExpressionWrapper, F, Prefetch, Q, Sum, fields
|
||||||
Count,
|
from django.db.models.functions import Extract, TruncDate
|
||||||
ExpressionWrapper,
|
|
||||||
F,
|
|
||||||
Prefetch,
|
|
||||||
Q,
|
|
||||||
Sum,
|
|
||||||
fields,
|
|
||||||
)
|
|
||||||
from django.db.models.functions import TruncDate
|
|
||||||
from django.http import (
|
from django.http import (
|
||||||
HttpRequest,
|
HttpRequest,
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
@ -21,7 +13,6 @@ from django.http import (
|
|||||||
from django.shortcuts import redirect, render
|
from django.shortcuts import redirect, render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
|
|
||||||
from common.time import format_duration
|
from common.time import format_duration
|
||||||
from common.utils import safe_division
|
from common.utils import safe_division
|
||||||
@ -83,12 +74,9 @@ def add_session(request, purchase_id=None):
|
|||||||
|
|
||||||
|
|
||||||
def update_session(request, session_id=None):
|
def update_session(request, session_id=None):
|
||||||
session = get_object_or_404(Session, id=session_id)
|
session = Session.objects.get(id=session_id)
|
||||||
session.finish_now()
|
session.finish_now()
|
||||||
session.save()
|
session.save()
|
||||||
if request.htmx:
|
|
||||||
context = {"session": session}
|
|
||||||
return render(request, "list_sessions.html#session-row", context)
|
|
||||||
return redirect("list_sessions")
|
return redirect("list_sessions")
|
||||||
|
|
||||||
|
|
||||||
@ -253,17 +241,15 @@ def start_game_session(request, game_id: int):
|
|||||||
|
|
||||||
|
|
||||||
def start_session_same_as_last(request, last_session_id: int):
|
def start_session_same_as_last(request, last_session_id: int):
|
||||||
last_session = get_object_or_404(Session, id=last_session_id)
|
last_session = Session.objects.get(id=last_session_id)
|
||||||
# clone it
|
session = SessionForm(
|
||||||
session = last_session
|
{
|
||||||
session.pk = None
|
"purchase": last_session.purchase.id,
|
||||||
# set new data
|
"timestamp_start": timezone.now(),
|
||||||
session.timestamp_start = timezone.now()
|
"device": last_session.device,
|
||||||
session.timestamp_end = None
|
}
|
||||||
|
)
|
||||||
session.save()
|
session.save()
|
||||||
if request.htmx:
|
|
||||||
context = {"session": session}
|
|
||||||
return render(request, "list_sessions.html#session-row", context)
|
|
||||||
return redirect("list_sessions")
|
return redirect("list_sessions")
|
||||||
|
|
||||||
|
|
||||||
@ -285,40 +271,45 @@ def list_sessions(
|
|||||||
context = {}
|
context = {}
|
||||||
context["title"] = "Sessions"
|
context["title"] = "Sessions"
|
||||||
|
|
||||||
all_sessions = Session.objects.prefetch_related(
|
|
||||||
"purchase", "purchase__edition", "purchase__edition__game"
|
|
||||||
).order_by("-timestamp_start")
|
|
||||||
|
|
||||||
if filter == "purchase":
|
if filter == "purchase":
|
||||||
dataset = all_sessions.filter(purchase=purchase_id)
|
dataset = Session.objects.filter(purchase=purchase_id)
|
||||||
context["purchase"] = Purchase.objects.get(id=purchase_id)
|
context["purchase"] = Purchase.objects.get(id=purchase_id)
|
||||||
elif filter == "platform":
|
elif filter == "platform":
|
||||||
dataset = all_sessions.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 == "edition":
|
||||||
dataset = all_sessions.filter(purchase__edition=edition_id)
|
dataset = Session.objects.filter(purchase__edition=edition_id)
|
||||||
context["edition"] = Edition.objects.get(id=edition_id)
|
context["edition"] = Edition.objects.get(id=edition_id)
|
||||||
elif filter == "game":
|
elif filter == "game":
|
||||||
dataset = all_sessions.filter(purchase__edition__game=game_id)
|
dataset = Session.objects.filter(purchase__edition__game=game_id)
|
||||||
context["game"] = Game.objects.get(id=game_id)
|
context["game"] = Game.objects.get(id=game_id)
|
||||||
elif filter == "ownership_type":
|
elif filter == "ownership_type":
|
||||||
dataset = all_sessions.filter(purchase__ownership_type=ownership_type)
|
dataset = Session.objects.filter(purchase__ownership_type=ownership_type)
|
||||||
context["ownership_type"] = dict(Purchase.OWNERSHIP_TYPES)[ownership_type]
|
context["ownership_type"] = dict(Purchase.OWNERSHIP_TYPES)[ownership_type]
|
||||||
elif filter == "recent":
|
elif filter == "recent":
|
||||||
current_year = timezone.now().year
|
current_year = timezone.now().year
|
||||||
first_day_of_year = timezone.make_aware(datetime(current_year, 1, 1))
|
first_day_of_year = timezone.make_aware(datetime(current_year, 1, 1))
|
||||||
dataset = all_sessions.filter(timestamp_start__gte=first_day_of_year).order_by(
|
dataset = Session.objects.filter(
|
||||||
"-timestamp_start"
|
timestamp_start__gte=first_day_of_year
|
||||||
)
|
).order_by("-timestamp_start")
|
||||||
context["title"] = "This year"
|
context["title"] = "This year"
|
||||||
else:
|
else:
|
||||||
dataset = all_sessions
|
# by default, sort from newest to oldest
|
||||||
|
dataset = Session.objects.order_by("-timestamp_start")
|
||||||
|
|
||||||
context = {
|
for session in dataset:
|
||||||
"dataset": dataset,
|
if session.timestamp_end == None and session.duration_manual == timedelta(
|
||||||
"dataset_count": dataset.count(),
|
seconds=0
|
||||||
"last": Session.objects.prefetch_related("purchase__platform").latest(),
|
):
|
||||||
}
|
session.timestamp_end = timezone.now()
|
||||||
|
session.unfinished = True
|
||||||
|
|
||||||
|
context["total_duration"] = dataset.total_duration_formatted()
|
||||||
|
context["dataset"] = dataset
|
||||||
|
try:
|
||||||
|
context["last"] = Session.objects.latest()
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
context["last"] = None
|
||||||
|
|
||||||
return render(request, "list_sessions.html", context)
|
return render(request, "list_sessions.html", context)
|
||||||
|
|
||||||
@ -329,9 +320,7 @@ def stats(request, year: int = 0):
|
|||||||
return HttpResponseRedirect(reverse("stats_by_year", args=[selected_year]))
|
return HttpResponseRedirect(reverse("stats_by_year", args=[selected_year]))
|
||||||
if year == 0:
|
if year == 0:
|
||||||
year = timezone.now().year
|
year = timezone.now().year
|
||||||
this_year_sessions = Session.objects.filter(
|
this_year_sessions = Session.objects.filter(timestamp_start__year=year)
|
||||||
timestamp_start__year=year
|
|
||||||
).select_related("purchase__edition")
|
|
||||||
this_year_sessions_with_durations = this_year_sessions.annotate(
|
this_year_sessions_with_durations = this_year_sessions.annotate(
|
||||||
duration=ExpressionWrapper(
|
duration=ExpressionWrapper(
|
||||||
F("timestamp_end") - F("timestamp_start"),
|
F("timestamp_end") - F("timestamp_start"),
|
||||||
@ -339,10 +328,7 @@ def stats(request, year: int = 0):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
longest_session = this_year_sessions_with_durations.order_by("-duration").first()
|
longest_session = this_year_sessions_with_durations.order_by("-duration").first()
|
||||||
this_year_games = Game.objects.filter(
|
this_year_games_with_session_counts = Game.objects.annotate(
|
||||||
edition__purchase__session__in=this_year_sessions
|
|
||||||
).distinct()
|
|
||||||
this_year_games_with_session_counts = this_year_games.annotate(
|
|
||||||
session_count=Count(
|
session_count=Count(
|
||||||
"edition__purchase__session",
|
"edition__purchase__session",
|
||||||
filter=Q(edition__purchase__session__timestamp_start__year=year),
|
filter=Q(edition__purchase__session__timestamp_start__year=year),
|
||||||
@ -363,42 +349,48 @@ def stats(request, year: int = 0):
|
|||||||
).distinct()
|
).distinct()
|
||||||
|
|
||||||
this_year_purchases = Purchase.objects.filter(date_purchased__year=year)
|
this_year_purchases = Purchase.objects.filter(date_purchased__year=year)
|
||||||
this_year_purchases_with_currency = this_year_purchases.select_related(
|
this_year_purchases_with_currency = this_year_purchases.filter(
|
||||||
"edition"
|
price_currency__exact=selected_currency
|
||||||
).filter(price_currency__exact=selected_currency)
|
)
|
||||||
this_year_purchases_without_refunded = this_year_purchases_with_currency.filter(
|
this_year_purchases_without_refunded = this_year_purchases_with_currency.filter(
|
||||||
date_refunded=None
|
date_refunded=None
|
||||||
)
|
)
|
||||||
this_year_purchases_refunded = this_year_purchases_with_currency.refunded()
|
this_year_purchases_refunded = this_year_purchases_with_currency.refunded()
|
||||||
|
|
||||||
this_year_purchases_unfinished = (
|
this_year_purchases_unfinished = this_year_purchases_without_refunded.filter(
|
||||||
this_year_purchases_without_refunded.filter(date_finished__isnull=True)
|
date_finished__isnull=True
|
||||||
.filter(date_dropped__isnull=True)
|
).filter(
|
||||||
.filter(infinite=False)
|
Q(type=Purchase.GAME) | Q(type=Purchase.DLC)
|
||||||
.filter(Q(type=Purchase.GAME) | Q(type=Purchase.DLC))
|
|
||||||
) # do not count battle passes etc.
|
) # do not count battle passes etc.
|
||||||
|
|
||||||
this_year_purchases_without_refunded_count = (
|
|
||||||
this_year_purchases_without_refunded.count()
|
|
||||||
)
|
|
||||||
this_year_purchases_unfinished_count = this_year_purchases_unfinished.count()
|
|
||||||
this_year_purchases_unfinished_percent = int(
|
this_year_purchases_unfinished_percent = int(
|
||||||
safe_division(
|
safe_division(
|
||||||
this_year_purchases_unfinished_count,
|
this_year_purchases_unfinished.count(), this_year_purchases_refunded.count()
|
||||||
this_year_purchases_without_refunded_count,
|
|
||||||
)
|
)
|
||||||
* 100
|
* 100
|
||||||
)
|
)
|
||||||
|
|
||||||
purchases_finished_this_year = Purchase.objects.filter(date_finished__year=year)
|
purchases_finished_this_year = Purchase.objects.filter(date_finished__year=year)
|
||||||
|
purchases_finished_this_year_with_playtime = purchases_finished_this_year.annotate(
|
||||||
|
total_playtime=Sum(
|
||||||
|
F("session__duration_calculated") + F("session__duration_manual")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
for purchase in purchases_finished_this_year_with_playtime:
|
||||||
|
formatted_playtime = format_duration(purchase.total_playtime, "%2.0H")
|
||||||
|
setattr(purchase, "formatted_playtime", formatted_playtime)
|
||||||
|
|
||||||
purchases_finished_this_year_released_this_year = (
|
purchases_finished_this_year_released_this_year = (
|
||||||
purchases_finished_this_year.filter(edition__year_released=year).order_by(
|
purchases_finished_this_year.filter(edition__year_released=year).order_by(
|
||||||
"date_finished"
|
"date_finished"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
purchased_this_year_finished_this_year = (
|
purchased_this_year_finished_this_year = (
|
||||||
this_year_purchases_without_refunded.filter(date_finished__year=year)
|
this_year_purchases_without_refunded.intersection(
|
||||||
).order_by("date_finished")
|
purchases_finished_this_year
|
||||||
|
).order_by("date_finished")
|
||||||
|
)
|
||||||
|
|
||||||
this_year_spendings = this_year_purchases_without_refunded.aggregate(
|
this_year_spendings = this_year_purchases_without_refunded.aggregate(
|
||||||
total_spent=Sum(F("price"))
|
total_spent=Sum(F("price"))
|
||||||
@ -443,20 +435,6 @@ def stats(request, year: int = 0):
|
|||||||
.count()
|
.count()
|
||||||
)
|
)
|
||||||
|
|
||||||
first_play_name = "N/A"
|
|
||||||
first_play_date = "N/A"
|
|
||||||
last_play_name = "N/A"
|
|
||||||
last_play_date = "N/A"
|
|
||||||
if this_year_sessions:
|
|
||||||
first_session = this_year_sessions.earliest()
|
|
||||||
first_play_name = first_session.purchase.edition.name
|
|
||||||
first_play_date = first_session.timestamp_start.strftime("%x")
|
|
||||||
last_session = this_year_sessions.latest()
|
|
||||||
last_play_name = last_session.purchase.edition.name
|
|
||||||
last_play_date = last_session.timestamp_start.strftime("%x")
|
|
||||||
|
|
||||||
all_purchased_this_year_count = this_year_purchases_with_currency.count()
|
|
||||||
all_purchased_refunded_this_year_count = this_year_purchases_refunded.count()
|
|
||||||
context = {
|
context = {
|
||||||
"total_hours": format_duration(
|
"total_hours": format_duration(
|
||||||
this_year_sessions.total_duration_unformatted(), "%2.0H"
|
this_year_sessions.total_duration_unformatted(), "%2.0H"
|
||||||
@ -472,67 +450,45 @@ def stats(request, year: int = 0):
|
|||||||
"total_spent_currency": selected_currency,
|
"total_spent_currency": selected_currency,
|
||||||
"all_purchased_this_year": this_year_purchases_without_refunded,
|
"all_purchased_this_year": this_year_purchases_without_refunded,
|
||||||
"spent_per_game": int(
|
"spent_per_game": int(
|
||||||
safe_division(total_spent, this_year_purchases_without_refunded_count)
|
safe_division(total_spent, this_year_purchases_without_refunded.count())
|
||||||
),
|
),
|
||||||
"all_finished_this_year": purchases_finished_this_year.select_related(
|
"all_finished_this_year": purchases_finished_this_year_with_playtime.order_by(
|
||||||
"edition"
|
|
||||||
).order_by("date_finished"),
|
|
||||||
"all_finished_this_year_count": purchases_finished_this_year.count(),
|
|
||||||
"this_year_finished_this_year": purchases_finished_this_year_released_this_year.select_related(
|
|
||||||
"edition"
|
|
||||||
).order_by(
|
|
||||||
"date_finished"
|
"date_finished"
|
||||||
),
|
),
|
||||||
"this_year_finished_this_year_count": purchases_finished_this_year_released_this_year.count(),
|
"this_year_finished_this_year": purchases_finished_this_year_released_this_year.order_by(
|
||||||
"purchased_this_year_finished_this_year": purchased_this_year_finished_this_year.select_related(
|
"date_finished"
|
||||||
"edition"
|
),
|
||||||
).order_by(
|
"purchased_this_year_finished_this_year": purchased_this_year_finished_this_year.order_by(
|
||||||
"date_finished"
|
"date_finished"
|
||||||
),
|
),
|
||||||
"total_sessions": this_year_sessions.count(),
|
"total_sessions": this_year_sessions.count(),
|
||||||
"unique_days": unique_days["dates"],
|
"unique_days": unique_days["dates"],
|
||||||
"unique_days_percent": int(unique_days["dates"] / 365 * 100),
|
"unique_days_percent": int(unique_days["dates"] / 365 * 100),
|
||||||
"purchased_unfinished": this_year_purchases_unfinished,
|
"purchased_unfinished": this_year_purchases_unfinished,
|
||||||
"purchased_unfinished_count": this_year_purchases_unfinished_count,
|
|
||||||
"unfinished_purchases_percent": this_year_purchases_unfinished_percent,
|
"unfinished_purchases_percent": this_year_purchases_unfinished_percent,
|
||||||
"refunded_percent": int(
|
"refunded_percent": int(
|
||||||
safe_division(
|
safe_division(
|
||||||
all_purchased_refunded_this_year_count,
|
this_year_purchases_refunded.count(),
|
||||||
all_purchased_this_year_count,
|
this_year_purchases_with_currency.count(),
|
||||||
)
|
)
|
||||||
* 100
|
* 100
|
||||||
),
|
),
|
||||||
"all_purchased_refunded_this_year": this_year_purchases_refunded,
|
"all_purchased_refunded_this_year": this_year_purchases_refunded,
|
||||||
"all_purchased_refunded_this_year_count": all_purchased_refunded_this_year_count,
|
|
||||||
"all_purchased_this_year": this_year_purchases_with_currency.order_by(
|
"all_purchased_this_year": this_year_purchases_with_currency.order_by(
|
||||||
"date_purchased"
|
"date_purchased"
|
||||||
),
|
),
|
||||||
"all_purchased_this_year_count": all_purchased_this_year_count,
|
|
||||||
"backlog_decrease_count": backlog_decrease_count,
|
"backlog_decrease_count": backlog_decrease_count,
|
||||||
"longest_session_time": format_duration(
|
"longest_session_time": format_duration(
|
||||||
longest_session.duration, "%2.0Hh %2.0mm"
|
longest_session.duration if longest_session else timedelta(0),
|
||||||
)
|
"%2.0Hh %2.0mm",
|
||||||
if longest_session
|
),
|
||||||
else 0,
|
"longest_session_game": longest_session.purchase.edition.name,
|
||||||
"longest_session_game": longest_session.purchase.edition.name
|
"highest_session_count": game_highest_session_count.session_count,
|
||||||
if longest_session
|
"highest_session_count_game": game_highest_session_count.name,
|
||||||
else "N/A",
|
|
||||||
"highest_session_count": game_highest_session_count.session_count
|
|
||||||
if game_highest_session_count
|
|
||||||
else 0,
|
|
||||||
"highest_session_count_game": game_highest_session_count.name
|
|
||||||
if game_highest_session_count
|
|
||||||
else "N/A",
|
|
||||||
"highest_session_average": format_duration(
|
"highest_session_average": format_duration(
|
||||||
highest_session_average_game.session_average, "%2.0Hh %2.0mm"
|
highest_session_average_game.session_average, "%2.0Hh %2.0mm"
|
||||||
)
|
),
|
||||||
if highest_session_average_game
|
|
||||||
else 0,
|
|
||||||
"highest_session_average_game": highest_session_average_game,
|
"highest_session_average_game": highest_session_average_game,
|
||||||
"first_play_name": first_play_name,
|
|
||||||
"first_play_date": first_play_date,
|
|
||||||
"last_play_name": last_play_name,
|
|
||||||
"last_play_date": last_play_date,
|
|
||||||
"title": f"{year} Stats",
|
"title": f"{year} Stats",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
poetry.lock
generated
39
poetry.lock
generated
@ -1,4 +1,4 @@
|
|||||||
# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand.
|
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aniso8601"
|
name = "aniso8601"
|
||||||
@ -172,39 +172,6 @@ files = [
|
|||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
Django = ">=3.2"
|
Django = ">=3.2"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "django-htmx"
|
|
||||||
version = "1.17.2"
|
|
||||||
description = "Extensions for using Django with htmx."
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.8"
|
|
||||||
files = [
|
|
||||||
{file = "django-htmx-1.17.2.tar.gz", hash = "sha256:4089f2ed38727e9846c2f4cd1daddf6b010c7be8d834cfbcffc8c5ecf445c04e"},
|
|
||||||
{file = "django_htmx-1.17.2-py3-none-any.whl", hash = "sha256:f4971432d2ca45dbb31d9b58add1c50ae54354afe4bf59cafd591b1711b502c0"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.dependencies]
|
|
||||||
asgiref = ">=3.6"
|
|
||||||
Django = ">=3.2"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "django-template-partials"
|
|
||||||
version = "23.4"
|
|
||||||
description = "django-template-partials"
|
|
||||||
optional = false
|
|
||||||
python-versions = "*"
|
|
||||||
files = [
|
|
||||||
{file = "django-template-partials-23.4.tar.gz", hash = "sha256:f762b0b7b2222462df0845f0556792640b769eb832eae218a0e7dadd4e5606cc"},
|
|
||||||
{file = "django_template_partials-23.4-py2.py3-none-any.whl", hash = "sha256:d83d9c2d2836be769919e9aaf394d5feb1ac86e1187083030398308070122fca"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.dependencies]
|
|
||||||
Django = "*"
|
|
||||||
|
|
||||||
[package.extras]
|
|
||||||
docs = ["Sphinx"]
|
|
||||||
tests = ["coverage", "django_coverage_plugin"]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "djhtml"
|
name = "djhtml"
|
||||||
version = "1.5.2"
|
version = "1.5.2"
|
||||||
@ -1019,5 +986,5 @@ watchdog = ["watchdog (>=2.3)"]
|
|||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.11"
|
python-versions = "^3.12"
|
||||||
content-hash = "4662e73ad621b11cbe5b517ca08aae4cbeb350bfcc855a6c067861942e232d2a"
|
content-hash = "e864dc8abf6c84e5bb16ac2aa937c2a70561d15f3e8a1459866b9d6507e8773e"
|
||||||
|
@ -8,13 +8,11 @@ readme = "README.md"
|
|||||||
packages = [{include = "timetracker"}]
|
packages = [{include = "timetracker"}]
|
||||||
|
|
||||||
[tool.poetry.group.main.dependencies]
|
[tool.poetry.group.main.dependencies]
|
||||||
python = "^3.11"
|
python = "^3.12"
|
||||||
django = "^4.2.0"
|
django = "^4.2.0"
|
||||||
gunicorn = "^20.1.0"
|
gunicorn = "^20.1.0"
|
||||||
uvicorn = "^0.20.0"
|
uvicorn = "^0.20.0"
|
||||||
graphene-django = "^3.1.5"
|
graphene-django = "^3.1.5"
|
||||||
django-htmx = "^1.17.2"
|
|
||||||
django-template-partials = "^23.4"
|
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
black = "^22.12.0"
|
black = "^22.12.0"
|
||||||
@ -29,7 +27,6 @@ isort = "^5.11.4"
|
|||||||
pre-commit = "^3.5.0"
|
pre-commit = "^3.5.0"
|
||||||
django-debug-toolbar = "^4.2.0"
|
django-debug-toolbar = "^4.2.0"
|
||||||
|
|
||||||
|
|
||||||
[tool.isort]
|
[tool.isort]
|
||||||
profile = "black"
|
profile = "black"
|
||||||
|
|
||||||
|
@ -38,9 +38,7 @@ INSTALLED_APPS = [
|
|||||||
"django.contrib.sessions",
|
"django.contrib.sessions",
|
||||||
"django.contrib.messages",
|
"django.contrib.messages",
|
||||||
"django.contrib.staticfiles",
|
"django.contrib.staticfiles",
|
||||||
"template_partials",
|
|
||||||
"graphene_django",
|
"graphene_django",
|
||||||
"django_htmx",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
GRAPHENE = {"SCHEMA": "games.schema.schema"}
|
GRAPHENE = {"SCHEMA": "games.schema.schema"}
|
||||||
@ -58,7 +56,6 @@ MIDDLEWARE = [
|
|||||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"django_htmx.middleware.HtmxMiddleware",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
@ -82,7 +79,6 @@ TEMPLATES = [
|
|||||||
"games.views.model_counts",
|
"games.views.model_counts",
|
||||||
"games.views.stats_dropdown_year_range",
|
"games.views.stats_dropdown_year_range",
|
||||||
],
|
],
|
||||||
"builtins": ["template_partials.templatetags.partials"],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
Reference in New Issue
Block a user