Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrige une erreur si la durée d'une sanction temporaire est manquante #6564

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion templates/member/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ <h3>{% trans 'Sanctions' %}</h3>
name="ls-jrs"
class="expand"
placeholder="{% trans 'Durée de la lecture seule, en jours' %}"
required="required"
min="1">
{% csrf_token %}
<button type="submit"
Expand Down Expand Up @@ -795,12 +796,13 @@ <h3>{% trans 'Sanctions' %}</h3>
<input type="number"
name="ban-jrs"
class="expand"
required="required"
placeholder="{% trans 'Durée du bannissement, en jours' %}"
min="1">
{% csrf_token %}
<button type="submit"
name="ban-temp"
class="btn btn-submit">
class="btn btn-submit">
{% trans "Confirmer" %}
</button>
</form>
Expand Down
14 changes: 14 additions & 0 deletions zds/member/tests/views/tests_moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ def test_ls_followed_by_unls(self):
self.assertEqual(result.status_code, 200)
self.assertEqual(nb_users + 1, len(result.context["members"])) # LS guy still shows up, good

def test_temp_ban_missing_duration(self):
# Test error is caught when the duration for temporary sanction is missing in submitted form.
staff = StaffProfileFactory()
self.client.force_login(staff.user)

profile = ProfileFactory()
ls_text = "Texte de test pour LS TEMP"
result = self.client.post(
reverse("member-modify-profile", kwargs={"user_pk": profile.user.id}),
{"ls-temp": "", "ls-text": ls_text},
follow=False,
)
self.assertEqual(result.status_code, 302)

def test_temp_ls_followed_by_unls(self):
"""
Test various sanctions.
Expand Down
29 changes: 17 additions & 12 deletions zds/member/views/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.validators import validate_ipv46_address
from django.db import transaction
from django.http import Http404, HttpResponseBadRequest
from django.http import Http404
from django.shortcuts import redirect, render, get_object_or_404
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -178,19 +178,24 @@ def modify_profile(request, user_pk):

try:
ban = state.get_sanction(request.user, profile.user)
except ValueError:
raise HttpResponseBadRequest
except (ValueError, TypeError):
# These exception can be raised if content of the POST parameters are
# not correctly (eg, empty string instead of integer), making the
# object state having invalid data. The validity of parameters should
# be checked when creating the `state` object above.
messages.error(request, _("Une erreur est survenue lors de la récupération de la sanction."))
else:
state.apply_sanction(profile, ban)

state.apply_sanction(profile, ban)
if "un-ls" in request.POST or "un-ban" in request.POST:
msg = state.get_message_unsanction()
else:
msg = state.get_message_sanction()

if "un-ls" in request.POST or "un-ban" in request.POST:
msg = state.get_message_unsanction()
else:
msg = state.get_message_sanction()
msg = msg.format(
ban.user, ban.moderator, ban.type, state.get_detail(), ban.note, settings.ZDS_APP["site"]["literal_name"]
)

msg = msg.format(
ban.user, ban.moderator, ban.type, state.get_detail(), ban.note, settings.ZDS_APP["site"]["literal_name"]
)
state.notify_member(ban, msg)

state.notify_member(ban, msg)
return redirect(profile.get_absolute_url())