2024-08-11 18:21:27 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.paginator import Paginator
|
2024-08-12 19:42:34 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
|
2024-08-11 18:21:27 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.urls import reverse
|
|
|
|
|
2024-09-08 19:03:37 +00:00
|
|
|
from common.components import A, Button, Icon
|
2024-09-04 19:55:22 +00:00
|
|
|
from common.time import dateformat, local_strftime
|
2024-09-08 19:03:37 +00:00
|
|
|
from common.utils import truncate_with_popover
|
2024-08-11 18:21:27 +00:00
|
|
|
from games.forms import EditionForm
|
2024-08-12 19:42:34 +00:00
|
|
|
from games.models import Edition, Game
|
2024-08-11 18:21:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def list_editions(request: HttpRequest) -> HttpResponse:
|
|
|
|
context: dict[Any, Any] = {}
|
|
|
|
page_number = request.GET.get("page", 1)
|
|
|
|
limit = request.GET.get("limit", 10)
|
|
|
|
editions = Edition.objects.order_by("-created_at")
|
|
|
|
page_obj = None
|
|
|
|
if int(limit) != 0:
|
|
|
|
paginator = Paginator(editions, limit)
|
|
|
|
page_obj = paginator.get_page(page_number)
|
|
|
|
editions = page_obj.object_list
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"title": "Manage editions",
|
|
|
|
"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": {
|
2024-09-03 13:25:14 +00:00
|
|
|
"header_action": A([], Button([], "Add edition"), url="add_edition"),
|
2024-08-11 18:21:27 +00:00
|
|
|
"columns": [
|
|
|
|
"Game",
|
|
|
|
"Name",
|
|
|
|
"Sort Name",
|
|
|
|
"Platform",
|
|
|
|
"Year",
|
|
|
|
"Wikidata",
|
|
|
|
"Created",
|
|
|
|
"Actions",
|
|
|
|
],
|
|
|
|
"rows": [
|
|
|
|
[
|
2024-09-02 18:04:21 +00:00
|
|
|
A(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"href",
|
|
|
|
reverse(
|
|
|
|
"view_game",
|
|
|
|
args=[edition.game.pk],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
truncate_with_popover(edition.game.name),
|
|
|
|
),
|
2024-08-11 18:21:27 +00:00
|
|
|
truncate_with_popover(
|
|
|
|
edition.name
|
|
|
|
if edition.game.name != edition.name
|
|
|
|
else "(identical)"
|
|
|
|
),
|
|
|
|
truncate_with_popover(
|
|
|
|
edition.sort_name
|
|
|
|
if edition.sort_name is not None
|
|
|
|
and edition.game.name != edition.sort_name
|
|
|
|
else "(identical)"
|
|
|
|
),
|
|
|
|
truncate_with_popover(str(edition.platform)),
|
|
|
|
edition.year_released,
|
|
|
|
edition.wikidata,
|
2024-09-04 19:55:22 +00:00
|
|
|
local_strftime(edition.created_at, dateformat),
|
2024-08-11 18:21:27 +00:00
|
|
|
render_to_string(
|
2024-09-08 19:03:37 +00:00
|
|
|
"cotton/button_group.html",
|
2024-08-11 18:21:27 +00:00
|
|
|
{
|
|
|
|
"buttons": [
|
|
|
|
{
|
|
|
|
"href": reverse("edit_edition", args=[edition.pk]),
|
2024-09-08 19:03:37 +00:00
|
|
|
"slot": Icon("edit"),
|
2024-09-02 15:43:41 +00:00
|
|
|
"color": "gray",
|
2024-08-11 18:21:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"href": reverse(
|
|
|
|
"delete_edition", args=[edition.pk]
|
|
|
|
),
|
2024-09-08 19:03:37 +00:00
|
|
|
"slot": Icon("delete"),
|
2024-08-11 18:21:27 +00:00
|
|
|
"color": "red",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
for edition in editions
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return render(request, "list_purchases.html", context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2024-08-12 19:42:34 +00:00
|
|
|
def edit_edition(request: HttpRequest, edition_id: int = 0) -> HttpResponse:
|
2024-08-11 18:21:27 +00:00
|
|
|
edition = get_object_or_404(Edition, id=edition_id)
|
|
|
|
form = EditionForm(request.POST or None, instance=edition)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect("list_editions")
|
|
|
|
|
|
|
|
context: dict[str, Any] = {"form": form, "title": "Edit edition"}
|
|
|
|
return render(request, "add.html", context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def delete_edition(request: HttpRequest, edition_id: int) -> HttpResponse:
|
|
|
|
edition = get_object_or_404(Edition, id=edition_id)
|
|
|
|
edition.delete()
|
|
|
|
return redirect("list_editions")
|
2024-08-12 19:42:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def add_edition(request: HttpRequest, game_id: int = 0) -> HttpResponse:
|
|
|
|
context: dict[str, Any] = {}
|
|
|
|
if request.method == "POST":
|
|
|
|
form = EditionForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
edition = form.save()
|
|
|
|
if "submit_and_redirect" in request.POST:
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"add_purchase_for_edition", kwargs={"edition_id": edition.id}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return redirect("index")
|
|
|
|
else:
|
|
|
|
if game_id:
|
|
|
|
game = get_object_or_404(Game, id=game_id)
|
|
|
|
form = EditionForm(
|
|
|
|
initial={
|
|
|
|
"game": game,
|
|
|
|
"name": game.name,
|
|
|
|
"sort_name": game.sort_name,
|
|
|
|
"year_released": game.year_released,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
form = EditionForm()
|
|
|
|
|
|
|
|
context["form"] = form
|
|
|
|
context["title"] = "Add New Edition"
|
|
|
|
context["script_name"] = "add_edition.js"
|
|
|
|
return render(request, "add_edition.html", context)
|