Improve cap update

This commit is contained in:
Gabriel Augendre 2022-11-01 10:56:24 +01:00
parent 22832adcb0
commit b4d5ca039f
2 changed files with 9 additions and 16 deletions

View file

@ -1,17 +1,17 @@
{% load character_extras %} {% load character_extras %}
<div class="col-xl-3 col-md-6" data-path-id="{{ path.pk }}"> <div class="col-xl-3 col-md-6">
<div class="card"> <div class="card">
<h5 class="card-header"> <h5 class="card-header">
{{ path.display_name }} {{ path.display_name }}
<button hx-get="{% url "character:remove_last_in_path" character_pk=character.pk path_pk=path.pk %}" <button hx-get="{% url "character:remove_last_in_path" character_pk=character.pk path_pk=path.pk %}"
hx-target="[data-path-id='{{ path.pk }}']" hx-target="#paths-and-capabilities"
hx-swap="outerHTML" hx-swap="outerHTML"
class="btn btn-sm btn-primary"> class="btn btn-sm btn-primary">
<i class="fa-solid fa-minus"></i> <i class="fa-solid fa-minus"></i>
</button> </button>
{% if path|has_next_capability:character %} {% if path|has_next_capability:character %}
<button hx-get="{% url "character:add_next_in_path" character_pk=character.pk path_pk=path.pk %}" <button hx-get="{% url "character:add_next_in_path" character_pk=character.pk path_pk=path.pk %}"
hx-target="[data-path-id='{{ path.pk }}']" hx-target="#paths-and-capabilities"
hx-swap="outerHTML" hx-swap="outerHTML"
class="btn btn-sm btn-primary"> class="btn btn-sm btn-primary">
<i class="fa-solid fa-plus"></i> <i class="fa-solid fa-plus"></i>

View file

@ -194,11 +194,10 @@ def add_next_in_path(request, character_pk: int, path_pk: int):
capability = path.get_next_capability(character) capability = path.get_next_capability(character)
character.capabilities.add(capability) character.capabilities.add(capability)
context = { context = {
"capabilities": character.capabilities.filter(path=path).order_by("rank"),
"path": path,
"character": character, "character": character,
"add_path_form": AddPathForm(character),
} }
return render(request, "character/path.html", context) return render(request, "character/paths_and_capabilities.html", context)
@login_required @login_required
@ -206,19 +205,13 @@ def remove_last_in_path(request, character_pk: int, path_pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.filter(player=request.user), pk=character_pk Character.objects.filter(player=request.user), pk=character_pk
) )
path = get_object_or_404(Path, pk=path_pk)
last_rank = max( last_rank = max(
character.capabilities.filter(path=path).values_list("rank", flat=True) character.capabilities.filter(path_id=path_pk).values_list("rank", flat=True)
) )
cap = Capability.objects.get(path=path, rank=last_rank) cap = Capability.objects.get(path_id=path_pk, rank=last_rank)
character.capabilities.remove(cap) character.capabilities.remove(cap)
capabilities = character.capabilities.filter(path=path).order_by("rank")
if len(capabilities) == 0:
return HttpResponse()
context = { context = {
"capabilities": capabilities,
"path": path,
"character": character, "character": character,
"add_path_form": AddPathForm(character),
} }
return render(request, "character/path.html", context) return render(request, "character/paths_and_capabilities.html", context)