Major redesign #73
|
@ -0,0 +1,89 @@
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.core.paginator import Paginator
|
||||||
|
from django.http import HttpRequest, HttpResponse
|
||||||
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from games.forms import DeviceForm
|
||||||
|
from games.models import Device
|
||||||
|
from games.views import dateformat
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def list_devices(request: HttpRequest) -> HttpResponse:
|
||||||
|
context: dict[Any, Any] = {}
|
||||||
|
page_number = request.GET.get("page", 1)
|
||||||
|
limit = request.GET.get("limit", 10)
|
||||||
|
devices = Device.objects.order_by("-created_at")
|
||||||
|
page_obj = None
|
||||||
|
if int(limit) != 0:
|
||||||
|
paginator = Paginator(devices, limit)
|
||||||
|
page_obj = paginator.get_page(page_number)
|
||||||
|
devices = page_obj.object_list
|
||||||
|
|
||||||
|
context = {
|
||||||
|
"title": "Manage devices",
|
||||||
|
"page_obj": page_obj or None,
|
||||||
|
"elided_page_range": (
|
||||||
|
page_obj.paginator.get_elided_page_range(
|
||||||
|
page_number, on_each_side=1, on_ends=1
|
||||||
|
)
|
||||||
|
if page_obj
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"data": {
|
||||||
|
"columns": [
|
||||||
|
"Name",
|
||||||
|
"Type",
|
||||||
|
"Created",
|
||||||
|
"Actions",
|
||||||
|
],
|
||||||
|
"rows": [
|
||||||
|
[
|
||||||
|
device.name,
|
||||||
|
device.get_type_display(),
|
||||||
|
device.created_at.strftime(dateformat),
|
||||||
|
render_to_string(
|
||||||
|
"components/button_group_sm.html",
|
||||||
|
{
|
||||||
|
"buttons": [
|
||||||
|
{
|
||||||
|
"href": reverse("edit_device", args=[device.pk]),
|
||||||
|
"text": "Edit",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"href": reverse("delete_device", args=[device.pk]),
|
||||||
|
"text": "Delete",
|
||||||
|
"color": "red",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
for device in devices
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return render(request, "list_purchases.html", context)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def edit_device(request: HttpRequest, device_id: int = 0) -> HttpResponse:
|
||||||
|
device = get_object_or_404(Device, id=device_id)
|
||||||
|
form = DeviceForm(request.POST or None, instance=device)
|
||||||
|
if form.is_valid():
|
||||||
|
form.save()
|
||||||
|
return redirect("list_devices")
|
||||||
|
|
||||||
|
context: dict[str, Any] = {"form": form, "title": "Edit device"}
|
||||||
|
return render(request, "add.html", context)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def delete_device(request: HttpRequest, device_id: int) -> HttpResponse:
|
||||||
|
device = get_object_or_404(Device, id=device_id)
|
||||||
|
device.delete()
|
||||||
|
return redirect("list_sessions")
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Generated by Django 5.1 on 2024-08-11 15:50
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("games", "0034_purchase_date_dropped_purchase_infinite"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="session",
|
||||||
|
name="device",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
default=None,
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_DEFAULT,
|
||||||
|
to="games.device",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
|
@ -182,7 +182,7 @@ class Session(models.Model):
|
||||||
duration_calculated = models.DurationField(blank=True, null=True)
|
duration_calculated = models.DurationField(blank=True, null=True)
|
||||||
device = models.ForeignKey(
|
device = models.ForeignKey(
|
||||||
"Device",
|
"Device",
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.SET_DEFAULT,
|
||||||
null=True,
|
null=True,
|
||||||
blank=True,
|
blank=True,
|
||||||
default=None,
|
default=None,
|
||||||
|
|
|
@ -95,7 +95,7 @@
|
||||||
<ul class="py-2 text-sm text-gray-700 dark:text-gray-400"
|
<ul class="py-2 text-sm text-gray-700 dark:text-gray-400"
|
||||||
aria-labelledby="dropdownLargeButton">
|
aria-labelledby="dropdownLargeButton">
|
||||||
<li>
|
<li>
|
||||||
<a href="{% url 'add_device' %}"
|
<a href="{% url 'list_devices' %}"
|
||||||
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Devices</a>
|
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Devices</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from games import purchaseviews, sessionviews, views
|
from games import deviceviews, purchaseviews, sessionviews, views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.index, name="index"),
|
path("", views.index, name="index"),
|
||||||
path("device/add", views.add_device, name="add_device"),
|
path("device/add", views.add_device, name="add_device"),
|
||||||
|
path(
|
||||||
|
"device/delete/<int:device_id>", deviceviews.delete_device, name="delete_device"
|
||||||
|
),
|
||||||
|
path("device/edit/<int:device_id>", deviceviews.edit_device, name="edit_device"),
|
||||||
|
path("device/list", deviceviews.list_devices, name="list_devices"),
|
||||||
path("edition/add", views.add_edition, name="add_edition"),
|
path("edition/add", views.add_edition, name="add_edition"),
|
||||||
path(
|
path(
|
||||||
"edition/add/for-game/<int:game_id>",
|
"edition/add/for-game/<int:game_id>",
|
||||||
|
|
Loading…
Reference in New Issue