Prevent users from interacting with other characters

This commit is contained in:
Gabriel Augendre 2022-10-31 17:42:30 +01:00
parent ccd4b7f1e2
commit fec051b646

View file

@ -25,9 +25,9 @@ def character_create(request):
@login_required @login_required
def character_view(request, pk: int): def character_view(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.select_related( Character.objects.filter(player=request.user)
"player", "racial_capability", "profile", "race" .select_related("player", "racial_capability", "profile", "race")
).prefetch_related("capabilities__path", "weapons"), .prefetch_related("capabilities__path", "weapons"),
pk=pk, pk=pk,
) )
context = {"character": character} context = {"character": character}
@ -37,7 +37,10 @@ def character_view(request, pk: int):
@login_required @login_required
def character_health_change(request, pk: int): 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.filter(player=request.user).only(
"health_max", "health_remaining"
),
pk=pk,
) )
value = get_updated_value(request, character.health_remaining, character.health_max) value = get_updated_value(request, character.health_remaining, character.health_max)
character.health_remaining = value character.health_remaining = value
@ -48,9 +51,9 @@ def character_health_change(request, pk: int):
@login_required @login_required
def character_mana_change(request, pk: int): def character_mana_change(request, pk: int):
character = get_object_or_404( character = get_object_or_404(
Character.objects.only( Character.objects.filter(player=request.user)
"mana_remaining", "level", "value_intelligence", "profile" .only("mana_remaining", "level", "value_intelligence", "profile")
).select_related("profile"), .select_related("profile"),
pk=pk, pk=pk,
) )
value = get_updated_value(request, character.mana_remaining, character.mana_max) value = get_updated_value(request, character.mana_remaining, character.mana_max)
@ -62,7 +65,8 @@ def character_mana_change(request, pk: int):
@login_required @login_required
def character_recovery_points_change(request, pk: int): 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.filter(player=request.user).only("recovery_points_remaining"),
pk=pk,
) )
value = get_updated_value( value = get_updated_value(
request, character.recovery_points_remaining, character.recovery_points_max request, character.recovery_points_remaining, character.recovery_points_max
@ -74,7 +78,9 @@ def character_recovery_points_change(request, pk: int):
@login_required @login_required
def character_defense_misc_change(request, pk: int): 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.filter(player=request.user).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
character.save(update_fields=["defense_misc"]) character.save(update_fields=["defense_misc"])
@ -85,7 +91,10 @@ def character_defense_misc_change(request, pk: int):
@login_required @login_required
def character_luck_points_change(request, pk: int): 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.filter(player=request.user).only(
"luck_points_remaining", "value_charisma"
),
pk=pk,
) )
value = get_updated_value( value = get_updated_value(
request, character.luck_points_remaining, character.luck_points_max request, character.luck_points_remaining, character.luck_points_max
@ -112,7 +121,9 @@ def get_updated_value(request, remaining_value: int, max_value: int | float) ->
@login_required @login_required
def character_get_defense(request, pk: int): 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.filter(player=request.user).only(
"defense_misc", "armor", "shield", "value_dexterity"
),
pk=pk, pk=pk,
) )
return HttpResponse(character.defense) return HttpResponse(character.defense)
@ -126,7 +137,9 @@ def character_notes_change(request, pk: int):
@login_required @login_required
def character_equipment_change(request, pk: int): 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.filter(player=request.user).only(field), pk=pk
)
context = {"character": character} context = {"character": character}
if request.method == "GET": if request.method == "GET":
return render(request, f"character/{field}_update.html", context) return render(request, f"character/{field}_update.html", context)
@ -145,7 +158,9 @@ def character_damage_reduction_change(request, pk: int):
def update_text_field(request, pk, field): def update_text_field(request, pk, field):
character = get_object_or_404(Character.objects.only(field), pk=pk) character = get_object_or_404(
Character.objects.filter(player=request.user).only(field), pk=pk
)
context = {"character": character} context = {"character": character}
if request.method == "GET": if request.method == "GET":
return render(request, f"character/{field}_update.html", context) return render(request, f"character/{field}_update.html", context)