Skip to content

Commit

Permalink
Remplace des URLs hard-codées par des appels à reverse() (#6523)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippemilink authored Aug 26, 2023
1 parent 2017176 commit 46b253e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
17 changes: 12 additions & 5 deletions zds/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class TopicForm(forms.Form, FieldValidatorMixin):
label=_("Tag(s) séparés par une virgule (exemple: python,django,web)"),
max_length=64,
required=False,
widget=forms.TextInput(
attrs={"data-autocomplete": '{ "type": "multiple", "fieldname": "title", "url": "/api/tags/?search=%s" }'}
),
widget=forms.TextInput(),
)

text = forms.CharField(
Expand All @@ -44,6 +42,15 @@ class TopicForm(forms.Form, FieldValidatorMixin):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["tags"].widget.attrs.update(
{
"data-autocomplete": '{ "type": "multiple", "fieldname": "title", "url": "'
+ reverse("api:utils:tags-list")
+ '?search=%s" }'
}
)

self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
Expand All @@ -53,11 +60,11 @@ def __init__(self, *args, **kwargs):
Field("subtitle", autocomplete="off"),
Field("tags"),
HTML(
"""<div id="topic-suggest" style="display:none;" url="/rechercher/sujets-similaires/">
"""<div id="topic-suggest" style="display:none;" url="{}">
<label>{}</label>
<div id="topic-result-container" data-neither="{}"></div>
</div>""".format(
_("Sujets similaires au vôtre :"), _("Aucun résultat")
reverse("search:similar"), _("Sujets similaires au vôtre :"), _("Aucun résultat")
)
),
CommonLayoutEditor(),
Expand Down
9 changes: 7 additions & 2 deletions zds/mp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ class PrivateTopicForm(forms.Form, ParticipantsStringValidator, TitleValidator,
label=_("Participants"),
widget=forms.TextInput(
attrs={
"placeholder": _("Les participants doivent " "être séparés par une virgule."),
"placeholder": _("Les participants doivent être séparés par une virgule."),
"required": "required",
"data-autocomplete": '{ "type": "multiple", "url": "/api/membres/?search=%s" }',
}
),
)
Expand All @@ -40,6 +39,12 @@ class PrivateTopicForm(forms.Form, ParticipantsStringValidator, TitleValidator,

def __init__(self, username, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["participants"].widget.attrs.update(
{
"data-autocomplete": '{ "type": "multiple", "url": "' + reverse("api:member:list") + '?search=%s" }',
}
)
self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
Expand Down
37 changes: 23 additions & 14 deletions zds/tutorialv2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ class EditContentTagsForm(forms.Form):
label=_("Tags séparés par des virgules (exemple : python,api,web) :"),
max_length=64,
required=False,
widget=forms.TextInput(
attrs={"data-autocomplete": '{ "type": "multiple", "fieldname": "title", "url": "/api/tags/?search=%s" }'}
),
widget=forms.TextInput(),
error_messages={"max_length": _("La liste de tags saisie dépasse la longueur maximale autorisée.")},
)

Expand All @@ -331,6 +329,14 @@ def __init__(self, content, db_content, *args, **kwargs):
kwargs["initial"] = {"tags": ", ".join(db_content.tags.values_list("title", flat=True))}
super(forms.Form, self).__init__(*args, **kwargs)

self.fields["tags"].widget.attrs.update(
{
"data-autocomplete": '{ "type": "multiple", "fieldname": "title", "url": "'
+ reverse("api:utils:tags-list")
+ '?search=%s" }',
}
)

self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
Expand All @@ -341,7 +347,9 @@ def __init__(self, content, db_content, *args, **kwargs):
HTML(
"""<p>Les tags permettent de grouper les publications plus finement que les catégories.
Par exemple, vous pouvez indiquer une technologie ou une sous-discipline.
Consultez <a href="/contenus/tags">la page des tags</a> pour voir des exemples."""
Consultez <a href="{}">la page des tags</a> pour voir des exemples.""".format(
reverse("content:tags")
)
),
Field("tags"),
ButtonHolder(StrictButton("Valider", type="submit")),
Expand Down Expand Up @@ -1302,21 +1310,23 @@ class SearchSuggestionForm(forms.Form):
suggestion_pk = forms.CharField(
label="Contenu à suggérer",
required=False,
widget=forms.TextInput(
attrs={
"data-autocomplete": '{"type": "multiple_checkbox",'
'"limit": 10,'
'"fieldname": "title",'
'"url": "/rechercher/suggestion-contenu/?q=%s&excluded=%e"}',
"placeholder": "Rechercher un contenu",
}
),
widget=forms.TextInput(),
)
excluded_pk = forms.CharField(required=False, widget=forms.HiddenInput(attrs={"class": "excluded_field"}))

def __init__(self, content, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["suggestion_pk"].widget.attrs.update(
{
"data-autocomplete": '{"type": "multiple_checkbox",'
'"limit": 10,'
'"fieldname": "title",'
'"url": "' + reverse("search:suggestion") + '?q=%s&excluded=%e"}',
"placeholder": "Rechercher un contenu",
}
)

self.helper = FormHelper()
self.helper.form_action = reverse("content:add-suggestion", kwargs={"pk": content.pk})
self.helper.form_class = "modal modal-large"
Expand All @@ -1326,7 +1336,6 @@ def __init__(self, content, *args, **kwargs):
self.helper.layout = Layout(
Field("suggestion_pk"), Field("excluded_pk"), StrictButton(_("Ajouter"), type="submit")
)
super().__init__(*args, **kwargs)


class RemoveSuggestionForm(forms.Form):
Expand Down

0 comments on commit 46b253e

Please sign in to comment.