list games

This commit is contained in:
Lukáš Kucharczyk 2024-08-11 18:21:11 +02:00
parent 298ecb4092
commit c61adad180
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
3 changed files with 76 additions and 2 deletions

73
games/gameviews.py Normal file
View File

@ -0,0 +1,73 @@
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 render
from django.template.loader import render_to_string
from django.urls import reverse
from games.models import Game
from games.views import dateformat
@login_required
def list_games(request: HttpRequest) -> HttpResponse:
context: dict[Any, Any] = {}
page_number = request.GET.get("page", 1)
limit = request.GET.get("limit", 10)
games = Game.objects.order_by("-created_at")
page_obj = None
if int(limit) != 0:
paginator = Paginator(games, limit)
page_obj = paginator.get_page(page_number)
games = page_obj.object_list
context = {
"title": "Manage games",
"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",
"Sort Name",
"Year",
"Wikidata",
"Created",
"Actions",
],
"rows": [
[
game.name,
game.sort_name,
game.year_released,
game.wikidata,
game.created_at.strftime(dateformat),
render_to_string(
"components/button_group_sm.html",
{
"buttons": [
{
"href": reverse("edit_game", args=[game.pk]),
"text": "Edit",
},
{
"href": reverse("delete_game", args=[game.pk]),
"text": "Delete",
"color": "red",
},
]
},
),
]
for game in games
],
},
}
return render(request, "list_purchases.html", context)

View File

@ -99,7 +99,7 @@
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>
<a href="{% url 'add_game' %}" <a href="{% url 'list_games' %}"
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Games</a> class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Games</a>
</li> </li>
<li> <li>

View File

@ -1,6 +1,6 @@
from django.urls import path from django.urls import path
from games import deviceviews, purchaseviews, sessionviews, views from games import deviceviews, gameviews, purchaseviews, sessionviews, views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
@ -21,6 +21,7 @@ urlpatterns = [
path("game/<int:game_id>/edit", views.edit_game, name="edit_game"), path("game/<int:game_id>/edit", views.edit_game, name="edit_game"),
path("game/<int:game_id>/view", views.view_game, name="view_game"), path("game/<int:game_id>/view", views.view_game, name="view_game"),
path("game/<int:game_id>/delete", views.delete_game, name="delete_game"), path("game/<int:game_id>/delete", views.delete_game, name="delete_game"),
path("game/list", gameviews.list_games, name="list_games"),
path("platform/add", views.add_platform, name="add_platform"), path("platform/add", views.add_platform, name="add_platform"),
path("platform/<int:platform_id>/edit", views.edit_platform, name="edit_platform"), path("platform/<int:platform_id>/edit", views.edit_platform, name="edit_platform"),
path("purchase/add", views.add_purchase, name="add_purchase"), path("purchase/add", views.add_purchase, name="add_purchase"),