-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(partner): ajout des pages de creation / mise à jour depuis la pa…
…ge "nos partenaires" (#780) ## Description 🎸 Permettre l'ajout et la mise à jour des `Partner` depuis la page `partner:list` 🎸 Accès limité aux super-utilisateurs ## Type de changement 🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une fonctionnalité). ### Points d'attention 🦺 factorisation de la méthode `wrap_iframe_in_div_tag` 🦺 mixin et template communs pour `PartnerCreateView` et `PartnerUpdateView` ### Captures d'écran (optionnel) ![image](https://github.com/user-attachments/assets/9b2937cb-e1bd-454b-a5bc-cb85fddbae76) ![image](https://github.com/user-attachments/assets/08be764f-9aac-4c6e-aab1-e591a93e2197)
- Loading branch information
1 parent
732bd5c
commit 4001074
Showing
16 changed files
with
312 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django import forms | ||
from django.conf import settings | ||
|
||
from lacommunaute.partner.models import Partner | ||
from lacommunaute.utils.html import wrap_iframe_in_div_tag | ||
|
||
|
||
class PartnerForm(forms.ModelForm): | ||
logo = forms.ImageField( | ||
required=False, | ||
label="Logo, format 200 x 200 pixels recommandé", | ||
widget=forms.FileInput(attrs={"accept": settings.SUPPORTED_IMAGE_FILE_TYPES.keys()}), | ||
) | ||
|
||
def save(self, commit=True): | ||
partner = super().save(commit=False) | ||
partner.description = wrap_iframe_in_div_tag(self.cleaned_data.get("description")) | ||
|
||
if commit: | ||
partner.save() | ||
|
||
return partner | ||
|
||
class Meta: | ||
model = Partner | ||
fields = ("name", "short_description", "description", "logo", "url") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest # noqa | ||
|
||
from lacommunaute.partner.forms import PartnerForm | ||
from lacommunaute.partner.models import Partner | ||
|
||
|
||
def test_saved_partner_description(db): | ||
form = PartnerForm( | ||
data={ | ||
"name": "test", | ||
"short_description": "test", | ||
"description": "Text\n<iframe src='xxx'></iframe>\ntext\n<div><iframe src='yyy'></iframe></div>\nbye", | ||
} | ||
) | ||
assert form.is_valid() | ||
partner = form.save() | ||
assert partner.description.rendered == ( | ||
"<p>Text</p>\n\n<div><iframe src='xxx'></iframe></div>\n\n" | ||
"<p>text</p>\n\n<div><iframe src='yyy'></iframe></div>\n\n<p>bye</p>" | ||
) | ||
|
||
|
||
def test_form_field(): | ||
form = PartnerForm() | ||
assert form.Meta.model == Partner | ||
assert form.Meta.fields == ("name", "short_description", "description", "logo", "url") | ||
assert form.fields["name"].required | ||
assert list(form.fields["logo"].widget.attrs["accept"]) == ["image/png", "image/jpeg", "image/jpg", "image/gif"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest # noqa | ||
|
||
from django.urls import reverse | ||
|
||
from lacommunaute.users.factories import UserFactory | ||
from lacommunaute.partner.models import Partner | ||
|
||
|
||
@pytest.fixture(name="url") | ||
def url_fixture(): | ||
return reverse("partner:create") | ||
|
||
|
||
@pytest.fixture(name="superuser") | ||
def superuser_fixture(): | ||
return UserFactory(is_superuser=True) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"user,status_code", [(None, 302), (lambda: UserFactory(), 403), (lambda: UserFactory(is_superuser=True), 200)] | ||
) | ||
def test_user_passes_test_mixin(client, db, url, user, status_code): | ||
if user: | ||
client.force_login(user()) | ||
response = client.get(url) | ||
assert response.status_code == status_code | ||
|
||
|
||
def test_view(client, db, url, superuser): | ||
client.force_login(superuser) | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
assert response.context["title"] == "Créer une nouvelle page partenaire" | ||
assert response.context["back_url"] == reverse("partner:list") | ||
assert list(response.context["form"].fields.keys()) == ["name", "short_description", "description", "logo", "url"] | ||
|
||
|
||
def test_post_partner(client, db, url, superuser): | ||
client.force_login(superuser) | ||
data = { | ||
"name": "Test", | ||
"short_description": "Short description", | ||
"description": "# Titre\n<iframe src='https://www.example.com'></iframe>", | ||
"url": "https://www.example.com", | ||
} | ||
response = client.post(url, data) | ||
assert response.status_code == 302 | ||
|
||
partner = Partner.objects.get() | ||
assert response.url == reverse("partner:detail", kwargs={"pk": partner.pk, "slug": partner.slug}) | ||
assert partner.description.raw == "# Titre\n<div><iframe src='https://www.example.com'></iframe></div>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest # noqa | ||
|
||
from django.urls import reverse | ||
|
||
from lacommunaute.partner.factories import PartnerFactory | ||
from lacommunaute.users.factories import UserFactory | ||
|
||
|
||
@pytest.fixture(name="partner") | ||
def partner_fixture(): | ||
return PartnerFactory(for_snapshot=True) | ||
|
||
|
||
@pytest.fixture(name="url") | ||
def url_fixture(partner): | ||
return reverse("partner:update", kwargs={"pk": partner.id, "slug": partner.slug}) | ||
|
||
|
||
@pytest.fixture(name="superuser") | ||
def superuser_fixture(): | ||
return UserFactory(is_superuser=True) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"user,status_code", [(None, 302), (lambda: UserFactory(), 403), (lambda: UserFactory(is_superuser=True), 200)] | ||
) | ||
def test_user_passes_test_mixin(client, db, url, user, status_code): | ||
if user: | ||
client.force_login(user()) | ||
response = client.get(url) | ||
assert response.status_code == status_code | ||
|
||
|
||
def test_view(client, db, url, superuser, partner): | ||
client.force_login(superuser) | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
assert response.context["title"] == f"Modifier la page {partner.name}" | ||
assert response.context["back_url"] == reverse("partner:detail", kwargs={"pk": partner.pk, "slug": partner.slug}) | ||
assert list(response.context["form"].fields.keys()) == ["name", "short_description", "description", "logo", "url"] | ||
|
||
|
||
def test_post_partner(client, db, url, superuser, partner): | ||
client.force_login(superuser) | ||
data = { | ||
"name": "Test", | ||
"short_description": "Short description", | ||
"description": "# Titre\ndescription", | ||
"url": "https://www.example.com", | ||
} | ||
response = client.post(url, data) | ||
assert response.status_code == 302 | ||
|
||
partner.refresh_from_db() | ||
assert response.url == reverse("partner:detail", kwargs={"pk": partner.pk, "slug": partner.slug}) | ||
assert partner.description.rendered == "<h1>Titre</h1>\n\n<p>description</p>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends "board_base.html" %} | ||
{% block sub_title %} | ||
{{ title }} | ||
{% endblock sub_title %} | ||
{% block content %} | ||
<div class="row"> | ||
<div class="col-12"> | ||
<div class="card post-edit"> | ||
<div class="card-header"> | ||
<h3 class="m-0 h4 card-title">{{ title }}</h3> | ||
</div> | ||
<div class="card-body">{% include "partner/partials/partner_form.html" %}</div> | ||
</div> | ||
</div> | ||
</div> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{% load i18n %} | ||
<form method="post" action="." class="form" enctype="multipart/form-data" novalidate> | ||
{% csrf_token %} | ||
{% for error in post_form.non_field_errors %} | ||
<div class="alert alert-danger"> | ||
<i class="icon-exclamation-sign"></i> {{ error }} | ||
</div> | ||
{% endfor %} | ||
{% include "partials/form_field.html" with field=form.name %} | ||
{% include "partials/form_field.html" with field=form.short_description %} | ||
{% include "partials/form_field.html" with field=form.description %} | ||
{% include "partials/form_field.html" with field=form.logo %} | ||
{% include "partials/form_field.html" with field=form.url %} | ||
<hr class="mb-5"> | ||
<div class="form-actions form-row"> | ||
<div class="form-group col-auto"> | ||
<input type="submit" class="btn btn-primary" value="{% trans "Submit" %}" /> | ||
<a href="{{ back_url }}" class="btn btn-outline-warning">{% trans "Cancel" %}</a> | ||
</div> | ||
</div> | ||
</form> |
Oops, something went wrong.