From 6ef56bfed56da571941b06d4848270e629c91805 Mon Sep 17 00:00:00 2001 From: Lukas Kucharczyk Date: Sun, 11 Aug 2024 17:53:36 +0200 Subject: [PATCH] list, edit, and delete devices --- games/deviceviews.py | 89 +++++++++++++++++++ games/migrations/0035_alter_session_device.py | 25 ++++++ games/models.py | 2 +- games/templates/navbar.html | 2 +- games/urls.py | 7 +- 5 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 games/deviceviews.py create mode 100644 games/migrations/0035_alter_session_device.py diff --git a/games/deviceviews.py b/games/deviceviews.py new file mode 100644 index 0000000..6a68933 --- /dev/null +++ b/games/deviceviews.py @@ -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") diff --git a/games/migrations/0035_alter_session_device.py b/games/migrations/0035_alter_session_device.py new file mode 100644 index 0000000..2f02601 --- /dev/null +++ b/games/migrations/0035_alter_session_device.py @@ -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", + ), + ), + ] diff --git a/games/models.py b/games/models.py index a7f8aff..540a5fe 100644 --- a/games/models.py +++ b/games/models.py @@ -182,7 +182,7 @@ class Session(models.Model): duration_calculated = models.DurationField(blank=True, null=True) device = models.ForeignKey( "Device", - on_delete=models.CASCADE, + on_delete=models.SET_DEFAULT, null=True, blank=True, default=None, diff --git a/games/templates/navbar.html b/games/templates/navbar.html index d33b407..56d6ea9 100644 --- a/games/templates/navbar.html +++ b/games/templates/navbar.html @@ -95,7 +95,7 @@