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

Rend générique en fonction du type la vue "WarnTypo" #6669

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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: 2 additions & 2 deletions templates/tutorialv2/messages/warn_typo.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@



{% blocktrans with username=user.username|safe title=content.title|safe type=type|safe %}
{% blocktrans with username=user.username|safe title=content.title|safe %}
Salut !

Il me semble avoir déniché une erreur dans {{ type }} « [{{ title }}]({{ content_url }}) ».{% endblocktrans %}
Il me semble avoir déniché une erreur dans « [{{ title }}]({{ content_url }}) ».{% endblocktrans %}
{% if target != content %}
{% blocktrans with title=target.title|safe %}Fourbe, elle se situe sournoisement dans {{ feminized }} {{ level }} « [{{ title }}]({{ target_url }}) ».{% endblocktrans %}
{% endif %}
Expand Down
2 changes: 0 additions & 2 deletions zds/member/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from datetime import datetime
from hashlib import md5
import logging

from django.conf import settings
from django.contrib.auth.models import User
Expand Down
83 changes: 0 additions & 83 deletions zds/tutorialv2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,89 +693,6 @@ def __init__(self, *args, **kwargs):
)


class WarnTypoForm(forms.Form):
text = forms.CharField(
label="",
required=True,
widget=forms.Textarea(attrs={"placeholder": _("Expliquez la faute"), "rows": "3", "id": "warn_text"}),
)

target = forms.CharField(widget=forms.HiddenInput(), required=False)
version = forms.CharField(widget=forms.HiddenInput(), required=True)

def __init__(self, content, targeted, public=True, *args, **kwargs):
super().__init__(*args, **kwargs)

self.content = content
self.targeted = targeted

# modal form, send back to previous page if any:
if public:
self.previous_page_url = targeted.get_absolute_url_online()
else:
self.previous_page_url = targeted.get_absolute_url_beta()

# add an additional link to send PM if needed
type_ = _("l'article")

if content.is_tutorial:
type_ = _("le tutoriel")
elif content.is_opinion:
type_ = _("le billet")

if targeted.get_tree_depth() == 0:
pm_title = _("J'ai trouvé une faute dans {} « {} ».").format(type_, targeted.title)
else:
pm_title = _("J'ai trouvé une faute dans le chapitre « {} ».").format(targeted.title)

usernames = ""
num_of_authors = content.authors.count()
for index, user in enumerate(content.authors.all()):
if index != 0:
usernames += "&"
usernames += "username=" + user.username

msg = _('<p>Pas assez de place ? <a href="{}?title={}&{}">Envoyez un MP {}</a> !</a>').format(
reverse("mp:create"), pm_title, usernames, _("à l'auteur") if num_of_authors == 1 else _("aux auteurs")
)

version = content.sha_beta
if public:
version = content.sha_public

# create form
self.helper = FormHelper()
self.helper.form_action = reverse("content:warn-typo") + f"?pk={content.pk}"
self.helper.form_method = "post"
self.helper.form_class = "modal modal-flex"
self.helper.form_id = "warn-typo-modal"
self.helper.layout = Layout(
Field("target"),
Field("text"),
HTML(msg),
Hidden("pk", "{{ content.pk }}"),
Hidden("version", version),
ButtonHolder(StrictButton(_("Envoyer"), type="submit", css_class="btn-submit")),
)

def clean(self):
cleaned_data = super().clean()

text = cleaned_data.get("text")

if text is None or not text.strip():
self._errors["text"] = self.error_class([_("Vous devez indiquer la faute commise.")])
if "text" in cleaned_data:
del cleaned_data["text"]

elif len(text) < 3:
self._errors["text"] = self.error_class([_("Votre commentaire doit faire au moins 3 caractères.")])
if "text" in cleaned_data:
del cleaned_data["text"]

return cleaned_data


class PublicationForm(forms.Form):
"""
The publication form (used only for content without preliminary validation).
Expand Down
5 changes: 3 additions & 2 deletions zds/tutorialv2/urls/urls_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@

from zds.tutorialv2.views.lists import TagsListView, ContentOfAuthor, ListContentReactions
from zds.tutorialv2.views.alerts import SendContentAlert, SolveContentAlert
from zds.tutorialv2.views.misc import RequestFeaturedContent, FollowNewContent, WarnTypo
from zds.tutorialv2.views.misc import RequestFeaturedContent, FollowNewContent
from zds.tutorialv2.views.warntypo import WarnTypoView
from zds.tutorialv2.views.statistics import ContentStatisticsView
from zds.tutorialv2.views.comments import (
SendNoteFormView,
Expand Down Expand Up @@ -167,7 +168,7 @@ def get_version_pages():
path("alerter/<int:pk>/", SendContentAlert.as_view(), name="alert-content"),
path("resoudre/<int:pk>/", SolveContentAlert.as_view(), name="resolve-content"),
# typo:
path("reactions/typo/", WarnTypo.as_view(), name="warn-typo"),
path("reactions/typo/", WarnTypoView.as_view(), name="warn-typo"),
# create:
path("nouveau-contenu/<str:created_content_type>/", CreateContentView.as_view(), name="create-content"),
path(
Expand Down
2 changes: 1 addition & 1 deletion zds/tutorialv2/views/display/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from zds.tutorialv2.forms import WarnTypoForm
from zds.tutorialv2.views.warntypo import WarnTypoForm
from zds.tutorialv2.mixins import SingleContentDetailViewMixin, SingleOnlineContentDetailViewMixin
from zds.tutorialv2.models.database import PublishableContent
from zds.tutorialv2.utils import search_container_or_404, get_target_tagged_tree
Expand Down
3 changes: 1 addition & 2 deletions zds/tutorialv2/views/display/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
CancelValidationForm,
RevokeValidationForm,
UnpublicationForm,
WarnTypoForm,
JsFiddleActivationForm,
PublicationForm,
PickOpinionForm,
UnpickOpinionForm,
PromoteOpinionToArticleForm,
)
from zds.tutorialv2.views.warntypo import WarnTypoForm
from zds.tutorialv2.views.canonical import EditCanonicalLinkForm
from zds.tutorialv2.views.contributors import ContributionForm
from zds.tutorialv2.views.suggestions import SearchSuggestionForm
Expand Down Expand Up @@ -88,7 +88,6 @@ def get_context_data(self, **kwargs):
data_form_unpublication = data_form_revoke
context["form_unpublication"] = UnpublicationForm(versioned, initial=data_form_unpublication)

context["form_warn_typo"] = WarnTypoForm(versioned, versioned, public=False)
context["form_jsfiddle"] = JsFiddleActivationForm(initial={"js_support": self.object.js_support})
context["form_edit_license"] = EditContentLicenseForm(versioned)

Expand Down
89 changes: 1 addition & 88 deletions zds/tutorialv2/views/misc.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http import HttpResponse, Http404
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView

from zds import json_handler
from zds.featured.mixins import FeatureableMixin
from zds.member.decorator import LoggedWithReadWriteHability
from zds.member.models import Profile
from zds.notification.models import NewPublicationSubscription
from zds.tutorialv2.forms import WarnTypoForm
from zds.tutorialv2.mixins import SingleOnlineContentViewMixin, SingleContentFormViewMixin
from zds.tutorialv2.utils import search_container_or_404
from zds.mp.utils import send_mp
from zds.tutorialv2.mixins import SingleOnlineContentViewMixin
from zds.utils.misc import is_ajax


Expand Down Expand Up @@ -72,83 +65,3 @@ def post(self, request, *args, **kwargs):
if is_ajax(self.request):
return HttpResponse(json_handler.dumps(response), content_type="application/json")
return redirect(request.META.get("HTTP_REFERER"))


class WarnTypo(SingleContentFormViewMixin):
modal_form = True
form_class = WarnTypoForm
must_be_author = False

http_method_names = ["post"]
object = None

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()

versioned = self.get_versioned_object()
kwargs["content"] = versioned
kwargs["targeted"] = versioned

if "target" in self.request.POST and self.request.POST["target"] != "":
kwargs["targeted"] = search_container_or_404(versioned, self.request.POST["target"])

kwargs["public"] = True

if versioned.is_beta:
kwargs["public"] = False
elif not versioned.is_public:
raise PermissionDenied

return kwargs

def form_valid(self, form):
user = self.request.user
authors = list(Profile.objects.contactable_members().filter(user__in=self.object.authors.all()))
authors = list(author.user for author in authors)

# check if the warn is done on a public or beta version :
is_public = False

if form.content.is_public:
is_public = True
elif not form.content.is_beta:
raise Http404("Le contenu n'est ni public, ni en bêta.")

if not authors:
if self.object.authors.count() > 1:
messages.error(self.request, _("Les auteurs sont malheureusement injoignables."))
else:
messages.error(self.request, _("L'auteur est malheureusement injoignable."))

elif user in authors: # author try to PM himself
messages.error(self.request, _("Impossible d'envoyer la proposition de correction : vous êtes auteur."))

else: # send correction
text = "\n".join(["> " + line for line in form.cleaned_data["text"].split("\n")])

_type = _("l'article")
if form.content.is_tutorial:
_type = _("le tutoriel")
if form.content.is_opinion:
_type = _("le billet")

pm_title = _("J'ai trouvé une faute dans {} « {} ».").format(_type, form.content.title)

msg = render_to_string(
"tutorialv2/messages/warn_typo.md",
{
"user": user,
"content": form.content,
"target": form.targeted,
"type": _type,
"public": is_public,
"text": text,
},
)

# send it :
send_mp(user, authors, pm_title, "", msg, leave=False)

messages.success(self.request, _("Merci pour votre proposition de correction."))

return redirect(form.previous_page_url)
Loading