From 020dbe6a3a0d2f6d68cd63df583cc094edbc9521 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Mon, 30 Sep 2024 16:01:30 +0200 Subject: [PATCH] reuse abstract Publication in partner app, renamed logo into image --- .../forum/management/commands/populate.py | 2 +- lacommunaute/forum/tests/tests_views.py | 2 +- lacommunaute/partner/factories.py | 2 +- ...rtner_image_alter_partner_name_and_more.py | 41 +++++++++++++++++++ lacommunaute/partner/models.py | 29 +------------ lacommunaute/partner/tests/tests_model.py | 9 ++-- .../partner/tests/tests_partner_detailview.py | 4 +- .../partner/tests/tests_partner_listview.py | 2 +- .../templates/forum/partials/partner.html | 4 +- lacommunaute/templates/partner/detail.html | 4 +- lacommunaute/templates/partner/list.html | 4 +- 11 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 lacommunaute/partner/migrations/0003_remove_partner_logo_partner_image_alter_partner_name_and_more.py diff --git a/lacommunaute/forum/management/commands/populate.py b/lacommunaute/forum/management/commands/populate.py index 546deaf6b..5536a8bb9 100644 --- a/lacommunaute/forum/management/commands/populate.py +++ b/lacommunaute/forum/management/commands/populate.py @@ -19,7 +19,7 @@ def handle(self, *args, **options): UserFactory(username="communaute", password="password", is_superuser=True, is_staff=True) sys.stdout.write("superuser created\n") - partners = PartnerFactory.create_batch(5, with_logo=True) + partners = PartnerFactory.create_batch(5, with_image=True) sys.stdout.write("partners created\n") forum = ForumFactory(name="Espace d'échanges", with_public_perms=True) diff --git a/lacommunaute/forum/tests/tests_views.py b/lacommunaute/forum/tests/tests_views.py index ea4ac5610..c457a8789 100644 --- a/lacommunaute/forum/tests/tests_views.py +++ b/lacommunaute/forum/tests/tests_views.py @@ -580,7 +580,7 @@ def test_documentation_forum_header_content(self, client, db, snapshot, reset_fo assert len(content.find_all("a", href=sibling_forum.get_absolute_url())) == 1 def test_documentation_forum_with_partner(self, client, db, snapshot, documentation_forum): - documentation_forum.partner = PartnerFactory(for_snapshot=True, with_logo=True) + documentation_forum.partner = PartnerFactory(for_snapshot=True, with_image=True) documentation_forum.save() response = client.get(documentation_forum.get_absolute_url()) content = parse_response_to_soup(response, replace_in_href=[documentation_forum.partner], replace_img_src=True) diff --git a/lacommunaute/partner/factories.py b/lacommunaute/partner/factories.py index c6780fcf1..b055bde26 100644 --- a/lacommunaute/partner/factories.py +++ b/lacommunaute/partner/factories.py @@ -19,7 +19,7 @@ class Meta: url = factory.LazyAttribute(lambda x: faker.url()) class Params: - with_logo = factory.Trait(logo=factory.django.ImageField(filename="test.jpg")) + with_image = factory.Trait(image=factory.django.ImageField(filename="test.jpg")) for_snapshot = factory.Trait( name="Best Partner Ever", description="### h3 long MD description \n lorem ipsum dolor sit amet, consectetur adipiscing elit.", diff --git a/lacommunaute/partner/migrations/0003_remove_partner_logo_partner_image_alter_partner_name_and_more.py b/lacommunaute/partner/migrations/0003_remove_partner_logo_partner_image_alter_partner_name_and_more.py new file mode 100644 index 000000000..bc6b9ac6c --- /dev/null +++ b/lacommunaute/partner/migrations/0003_remove_partner_logo_partner_image_alter_partner_name_and_more.py @@ -0,0 +1,41 @@ +# Generated by Django 5.0.9 on 2024-09-30 13:52 + +import storages.backends.s3 +from django.db import migrations, models + +import lacommunaute.utils.validators + + +class Migration(migrations.Migration): + dependencies = [ + ("partner", "0002_alter_partner_options"), + ] + + operations = [ + migrations.RemoveField( + model_name="partner", + name="logo", + ), + migrations.AddField( + model_name="partner", + name="image", + field=models.ImageField( + blank=True, + null=True, + storage=storages.backends.s3.S3Storage(bucket_name="private-bucket", file_overwrite=False), + upload_to="", + validators=[lacommunaute.utils.validators.validate_image_size], + ), + ), + migrations.AlterField( + model_name="partner", + name="name", + field=models.CharField(max_length=100, unique=True, verbose_name="Name"), + ), + migrations.AlterField( + model_name="partner", + name="short_description", + field=models.CharField(default="to be updated", max_length=400, verbose_name="Short Description"), + preserve_default=False, + ), + ] diff --git a/lacommunaute/partner/models.py b/lacommunaute/partner/models.py index e8d01b270..0ad369fe2 100644 --- a/lacommunaute/partner/models.py +++ b/lacommunaute/partner/models.py @@ -1,29 +1,11 @@ -from django.conf import settings from django.db import models from django.urls import reverse -from django.utils.encoding import force_str -from django.utils.text import slugify from django.utils.translation import gettext_lazy as _ -from machina.models.abstract_models import DatedModel -from machina.models.fields import MarkupTextField -from storages.backends.s3boto3 import S3Boto3Storage -from lacommunaute.utils.validators import validate_image_size +from lacommunaute.utils.abstract_models import Publication -class Partner(DatedModel): - name = models.CharField(max_length=100, verbose_name="Nom", unique=True) - slug = models.SlugField(max_length=255, verbose_name="Slug") - short_description = models.CharField( - max_length=400, blank=True, null=True, verbose_name="Description courte (SEO)" - ) - description = MarkupTextField(verbose_name="Description", null=True, blank=True) - logo = models.ImageField( - storage=S3Boto3Storage(bucket_name=settings.AWS_STORAGE_BUCKET_NAME, file_overwrite=False), - validators=[validate_image_size], - upload_to="logos/", - help_text="1200x600 recommandé", - ) +class Partner(Publication): url = models.URLField(verbose_name="Lien vers le site du partenaire", null=True, blank=True) objects = models.Manager() @@ -33,12 +15,5 @@ class Meta: verbose_name_plural = _("Partners") ordering = ["-updated"] - def __str__(self): - return f"{self.name}" - - def save(self, *args, **kwargs): - self.slug = slugify(force_str(self.name), allow_unicode=True) - super().save(*args, **kwargs) - def get_absolute_url(self): return reverse("partner:detail", kwargs={"slug": self.slug, "pk": self.pk}) diff --git a/lacommunaute/partner/tests/tests_model.py b/lacommunaute/partner/tests/tests_model.py index 8ccb9e4e1..43cc83883 100644 --- a/lacommunaute/partner/tests/tests_model.py +++ b/lacommunaute/partner/tests/tests_model.py @@ -17,12 +17,13 @@ def test_slug_is_generated(db): assert partner.slug == "test-partner" -def test_logo_url(db): - partner = PartnerFactory(with_logo=True) +def test_image_url(db): + partner = PartnerFactory(with_image=True) assert ( - partner.logo.url.split("?")[0] == f"{settings.MEDIA_URL}{settings.AWS_STORAGE_BUCKET_NAME}/{partner.logo.name}" + partner.image.url.split("?")[0] + == f"{settings.MEDIA_URL}{settings.AWS_STORAGE_BUCKET_NAME}/{partner.image.name}" ) - assert "AWSAccessKeyId=" in partner.logo.url + assert "AWSAccessKeyId=" in partner.image.url def test_get_absolute_url(db): diff --git a/lacommunaute/partner/tests/tests_partner_detailview.py b/lacommunaute/partner/tests/tests_partner_detailview.py index 1fd5473ec..f2ec8a471 100644 --- a/lacommunaute/partner/tests/tests_partner_detailview.py +++ b/lacommunaute/partner/tests/tests_partner_detailview.py @@ -15,7 +15,7 @@ ], ) def test_partner_detailview(client, db, snapshot, user, snapshot_name): - partner = PartnerFactory(for_snapshot=True, with_logo=True) + partner = PartnerFactory(for_snapshot=True, with_image=True) user = user() if user: client.force_login(user) @@ -27,7 +27,7 @@ def test_partner_detailview(client, db, snapshot, user, snapshot_name): def test_partner_with_forums(client, db, snapshot): - partner = PartnerFactory(for_snapshot=True, with_logo=True) + partner = PartnerFactory(for_snapshot=True, with_image=True) forum = ForumFactory(with_partner=partner, with_public_perms=True, with_tags=["sun", "moon"], for_snapshot=True) response = client.get(partner.get_absolute_url()) assert response.status_code == 200 diff --git a/lacommunaute/partner/tests/tests_partner_listview.py b/lacommunaute/partner/tests/tests_partner_listview.py index f63633fa3..d9e2a46ff 100644 --- a/lacommunaute/partner/tests/tests_partner_listview.py +++ b/lacommunaute/partner/tests/tests_partner_listview.py @@ -12,7 +12,7 @@ def url_fixture(): def test_listview(client, db, snapshot, url): - partner = PartnerFactory(for_snapshot=True, with_logo=True) + partner = PartnerFactory(for_snapshot=True, with_image=True) response = client.get(url) assert response.status_code == 200 assert str( diff --git a/lacommunaute/templates/forum/partials/partner.html b/lacommunaute/templates/forum/partials/partner.html index a40782717..bcbe989d4 100644 --- a/lacommunaute/templates/forum/partials/partner.html +++ b/lacommunaute/templates/forum/partials/partner.html @@ -1,7 +1,7 @@
- {% if partner.logo %} - {{ partner.name }} + {% if partner.image %} + {{ partner.name }} {% endif %}

Fiche co-rédigée en partenariat avec {{ partner.name }}

diff --git a/lacommunaute/templates/partner/detail.html b/lacommunaute/templates/partner/detail.html index 7910893cb..c1fc521b0 100644 --- a/lacommunaute/templates/partner/detail.html +++ b/lacommunaute/templates/partner/detail.html @@ -13,8 +13,8 @@
- {% if partner.logo %} - {{ partner.name }} + {% if partner.image %} + {{ partner.name }} {% endif %}

{{ partner.name }}

diff --git a/lacommunaute/templates/partner/list.html b/lacommunaute/templates/partner/list.html index 680ae42ce..b029e3b92 100644 --- a/lacommunaute/templates/partner/list.html +++ b/lacommunaute/templates/partner/list.html @@ -21,9 +21,9 @@

{% trans "Partners" %}

{% for partner in partners %}