Skip to content

Commit

Permalink
reuse abstract Publication in partner app, renamed logo into image
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 30, 2024
1 parent ca12af2 commit bacdf13
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lacommunaute/forum/management/commands/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,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)
Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/partner/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
),
]
29 changes: 2 additions & 27 deletions lacommunaute/partner/models.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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})
9 changes: 5 additions & 4 deletions lacommunaute/partner/tests/tests_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/partner/tests/tests_partner_detailview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/partner/tests/tests_partner_listview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/templates/forum/partials/partner.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a href="{{ partner.get_absolute_url }}">
<div class="d-flex gap-3 align-items-center mb-2">
{% if partner.logo %}
<img src="{{ partner.logo.url }}" alt="{{ partner.name }}" class="img-fluid" width="100" loading="lazy" />
{% if partner.image %}
<img src="{{ partner.image.url }}" alt="{{ partner.name }}" class="img-fluid" width="100" loading="lazy" />
{% endif %}
<h4>Fiche co-rédigée en partenariat avec {{ partner.name }}</h4>
</div>
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/templates/partner/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<div class="s-title-01__row row">
<div class="s-title-01__col col-12">
<div class="d-flex gap-3 align-items-center mb-2">
{% if partner.logo %}
<img src="{{ partner.logo.url }}" alt="{{ partner.name }}" class="img-fluid" width="200" />
{% if partner.image %}
<img src="{{ partner.image.url }}" alt="{{ partner.name }}" class="img-fluid" width="200" />
{% endif %}
<h1 class="mb-0">{{ partner.name }}</h1>
</div>
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/templates/partner/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ <h1 class="s-title-01__title h1">{% trans "Partners" %}</h1>
{% for partner in partners %}
<div class="col mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">
{% if partner.logo %}
{% if partner.image %}
<div class="card-header card-header--partners mb-3">
<img src="{{ partner.logo.url }}" alt="{{ partner.name }}" loading="lazy" class="img-fluid">
<img src="{{ partner.image.url }}" alt="{{ partner.name }}" loading="lazy" class="img-fluid">
</div>
{% endif %}
<div class="card-body pb-0">
Expand Down

0 comments on commit bacdf13

Please sign in to comment.