Compare commits

..

4 Commits

Author SHA1 Message Date
Lukáš Kucharczyk f61cde180f Pass search_string to search_field.html
Django CI/CD / test (push) Successful in 1m10s Details
Django CI/CD / build-and-push (push) Successful in 2m1s Details
2024-11-10 00:05:33 +01:00
Lukáš Kucharczyk a53818257c Fix being unable to override c-vars from render_from_string 2024-11-10 00:05:11 +01:00
Lukáš Kucharczyk 2d3ea714c4 Extend session search 2024-11-09 23:52:09 +01:00
Lukáš Kucharczyk 832bb48983 Device: safe long type names directly in database 2024-11-09 23:51:28 +01:00
5 changed files with 74 additions and 12 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 5.1.2 on 2024-11-09 22:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('games', '0038_alter_purchase_price'),
]
operations = [
migrations.AlterField(
model_name='device',
name='type',
field=models.CharField(choices=[('PC', 'PC'), ('Console', 'Console'), ('Handheld', 'Handheld'), ('Mobile', 'Mobile'), ('Single-board computer', 'Single-board computer'), ('Unknown', 'Unknown')], default='Unknown', max_length=255),
),
]

View File

@ -0,0 +1,33 @@
# Generated by Django 5.1.2 on 2024-11-09 22:39
from django.db import migrations
def update_device_types(apps, schema_editor):
Device = apps.get_model("games", "Device")
# Mapping of short names to long names
type_map = {
"pc": "PC",
"co": "Console",
"ha": "Handheld",
"mo": "Mobile",
"sbc": "Single-board computer",
"un": "Unknown",
}
# Loop through all devices and update the type field
for device in Device.objects.all():
if device.type in type_map:
device.type = type_map[device.type]
device.save()
class Migration(migrations.Migration):
dependencies = [
("games", "0039_alter_device_type"),
]
operations = [
migrations.RunPython(update_device_types),
]

View File

@ -259,12 +259,12 @@ class Session(models.Model):
class Device(models.Model): class Device(models.Model):
PC = "pc" PC = "PC"
CONSOLE = "co" CONSOLE = "Console"
HANDHELD = "ha" HANDHELD = "Handheld"
MOBILE = "mo" MOBILE = "Mobile"
SBC = "sbc" SBC = "Single-board computer"
UNKNOWN = "un" UNKNOWN = "Unknown"
DEVICE_TYPES = [ DEVICE_TYPES = [
(PC, "PC"), (PC, "PC"),
(CONSOLE, "Console"), (CONSOLE, "Console"),
@ -274,8 +274,8 @@ class Device(models.Model):
(UNKNOWN, "Unknown"), (UNKNOWN, "Unknown"),
] ]
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
type = models.CharField(max_length=3, choices=DEVICE_TYPES, default=UNKNOWN) type = models.CharField(max_length=255, choices=DEVICE_TYPES, default=UNKNOWN)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
def __str__(self): def __str__(self):
return f"{self.name} ({self.get_type_display()})" return f"{self.name} ({self.type})"

View File

@ -1,4 +1,4 @@
<c-vars placeholder="Search" :name="id" /> <c-vars :name="id" />
<div class="pb-4 bg-white dark:bg-gray-900"> <div class="pb-4 bg-white dark:bg-gray-900">
<label for="table-search" class="sr-only">Search</label> <label for="table-search" class="sr-only">Search</label>
<div class="relative mt-1"> <div class="relative mt-1">
@ -7,6 +7,6 @@
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/> <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
</svg> </svg>
</div> </div>
<input type="text" id="{{ id }}" name="{{ name }}" value="{{ search_string }}" class="block pt-2 ps-10 text-sm text-gray-900 border border-gray-300 rounded-lg w-80 bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="{{ placeholder }}"> <input type="text" id="{{ id }}" name="{{ name }}" value="{{ search_string }}" class="block pt-2 ps-10 text-sm text-gray-900 border border-gray-300 rounded-lg w-80 bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="{% if placeholder %}{{ placeholder }}{% else %}Search{% endif %}">
</div> </div>
</div> </div>

View File

@ -2,6 +2,7 @@ from typing import Any
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import Q
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string from django.template.loader import render_to_string
@ -39,7 +40,13 @@ def list_sessions(request: HttpRequest, search_string: str = "") -> HttpResponse
sessions = Session.objects.order_by("-timestamp_start") sessions = Session.objects.order_by("-timestamp_start")
search_string = request.GET.get("search_string", search_string) search_string = request.GET.get("search_string", search_string)
if search_string != "": if search_string != "":
sessions = sessions.filter(purchase__edition__name__icontains=search_string) sessions = sessions.filter(
Q(purchase__edition__name__icontains=search_string)
| Q(purchase__edition__game__name__icontains=search_string)
| Q(purchase__platform__name__icontains=search_string)
| Q(device__name__icontains=search_string)
| Q(device__type__icontains=search_string)
)
last_session = sessions.latest() last_session = sessions.latest()
page_obj = None page_obj = None
if int(limit) != 0: if int(limit) != 0:
@ -63,7 +70,11 @@ def list_sessions(request: HttpRequest, search_string: str = "") -> HttpResponse
Form( Form(
children=[ children=[
render_to_string( render_to_string(
"cotton/search_field.html", {"id": "search_string"} "cotton/search_field.html",
{
"id": "search_string",
"search_string": search_string,
},
) )
] ]
), ),