Add character list

This commit is contained in:
Gabriel Augendre 2022-10-31 10:25:26 +01:00
parent c63129dfe1
commit df8ea865bb
5 changed files with 49 additions and 21 deletions

View file

@ -0,0 +1,16 @@
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">
<a href="{% url "character:view" pk=character.pk %}">
{{ character.name }}
</a>
</h5>
<p class="card-text">
{{ character.race.name }} {{ character.profile.name }} niv. {{ character.level }}<br>
{{ character.get_gender_display }}, {{ character.age }} ans, {{ character.height_m }}m, {{ character.weight }}kg<br>
PV {{ character.health_remaining }}/{{ character.health_max }}, PM {{ character.mana_remaining }}/{{ character.mana_max }}
</p>
</div>
</div>
</div>

View file

@ -0,0 +1,13 @@
{% extends "common/base.html" %}
{% load character_extras %}
{% block title %}Characters list{% endblock %}
{% block content %}
<h1>Characters</h1>
<div class="row row-cols-4 g-4">
{% for character in characters %}
{% include "character/character_card.html" %}
{% endfor %}
</div>
{% endblock %}

View file

@ -4,6 +4,7 @@ from character import views
app_name = "character" app_name = "character"
urlpatterns = [ urlpatterns = [
path("", views.characters_list, name="list"),
path("<int:pk>/", views.character_view, name="view"), path("<int:pk>/", views.character_view, name="view"),
path("<int:pk>/health_change", views.character_health_change, name="health_change"), path("<int:pk>/health_change", views.character_health_change, name="health_change"),
path("<int:pk>/mana_change", views.character_mana_change, name="mana_change"), path("<int:pk>/mana_change", views.character_mana_change, name="mana_change"),

View file

@ -1,5 +1,4 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django_htmx.http import trigger_client_event from django_htmx.http import trigger_client_event
@ -9,7 +8,13 @@ from character.models import Character
@login_required @login_required
def character_view(request: WSGIRequest, pk: int) -> HttpResponse: def characters_list(request):
context = {"characters": Character.objects.filter(player=request.user)}
return render(request, "character/list.html", context)
@login_required
def character_view(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.select_related( Character.objects.select_related(
"player", "racial_capability", "profile", "race" "player", "racial_capability", "profile", "race"
@ -21,7 +26,7 @@ def character_view(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required @login_required
def character_health_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_health_change(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.only("health_max", "health_remaining"), pk=pk Character.objects.only("health_max", "health_remaining"), pk=pk
) )
@ -32,7 +37,7 @@ def character_health_change(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required @login_required
def character_mana_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_mana_change(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.only( Character.objects.only(
"mana_remaining", "level", "value_intelligence", "profile" "mana_remaining", "level", "value_intelligence", "profile"
@ -46,7 +51,7 @@ def character_mana_change(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required @login_required
def character_recovery_points_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_recovery_points_change(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.only("recovery_points_remaining"), pk=pk Character.objects.only("recovery_points_remaining"), pk=pk
) )
@ -59,7 +64,7 @@ def character_recovery_points_change(request: WSGIRequest, pk: int) -> HttpRespo
@login_required @login_required
def character_defense_misc_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_defense_misc_change(request, pk: int):
character = get_object_or_404(Character.objects.only("defense_misc"), pk=pk) character = get_object_or_404(Character.objects.only("defense_misc"), pk=pk)
value = get_updated_value(request, character.defense_misc, float("inf")) value = get_updated_value(request, character.defense_misc, float("inf"))
character.defense_misc = value character.defense_misc = value
@ -69,7 +74,7 @@ def character_defense_misc_change(request: WSGIRequest, pk: int) -> HttpResponse
@login_required @login_required
def character_luck_points_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_luck_points_change(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.only("luck_points_remaining", "value_charisma"), pk=pk Character.objects.only("luck_points_remaining", "value_charisma"), pk=pk
) )
@ -81,9 +86,7 @@ def character_luck_points_change(request: WSGIRequest, pk: int) -> HttpResponse:
return HttpResponse(value) return HttpResponse(value)
def get_updated_value( def get_updated_value(request, remaining_value: int, max_value: int | float) -> int:
request: WSGIRequest, remaining_value: int, max_value: int | float
) -> int:
form_value = request.GET.get("value") form_value = request.GET.get("value")
if form_value == "ko": if form_value == "ko":
remaining_value = 0 remaining_value = 0
@ -98,7 +101,7 @@ def get_updated_value(
@login_required @login_required
def character_get_defense(request: WSGIRequest, pk: int) -> HttpResponse: def character_get_defense(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.only("defense_misc", "armor", "shield", "value_dexterity"), Character.objects.only("defense_misc", "armor", "shield", "value_dexterity"),
pk=pk, pk=pk,
@ -107,12 +110,12 @@ def character_get_defense(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required @login_required
def character_notes_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_notes_change(request, pk: int):
return update_text_field(request, pk, "notes") return update_text_field(request, pk, "notes")
@login_required @login_required
def character_equipment_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_equipment_change(request, pk: int):
field = "equipment" field = "equipment"
character = get_object_or_404(Character.objects.only(field), pk=pk) character = get_object_or_404(Character.objects.only(field), pk=pk)
context = {"character": character} context = {"character": character}
@ -128,7 +131,7 @@ def character_equipment_change(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required @login_required
def character_damage_reduction_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_damage_reduction_change(request, pk: int):
return update_text_field(request, pk, "damage_reduction") return update_text_field(request, pk, "damage_reduction")

View file

@ -1,12 +1,7 @@
import random
from django.core.handlers.wsgi import WSGIRequest from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render from django.shortcuts import redirect
def hello_world(request: WSGIRequest) -> HttpResponse: def hello_world(request: WSGIRequest) -> HttpResponse:
context = {"value": random.randint(1, 1000)} # noqa: S311 return redirect("character:list")
if request.htmx:
return render(request, "common/hello-random.html", context)
return render(request, "common/base.html", context)