From 6f4337f4e27038f0d229931ef7655d0c46eb30e3 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Thu, 7 Nov 2024 17:02:37 +0000 Subject: [PATCH] feat: add a property for a small and large agency logo --- .../migrations/0031_transitagency_logo.py | 36 +++++++++++++++++++ benefits/core/models.py | 28 +++++++++++++++ tests/pytest/core/test_models.py | 22 +++++++++++- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 benefits/core/migrations/0031_transitagency_logo.py diff --git a/benefits/core/migrations/0031_transitagency_logo.py b/benefits/core/migrations/0031_transitagency_logo.py new file mode 100644 index 000000000..365610c75 --- /dev/null +++ b/benefits/core/migrations/0031_transitagency_logo.py @@ -0,0 +1,36 @@ +# Generated by Django 5.1.2 on 2024-11-14 18:05 + +import benefits.core.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0030_enrollmentevent_extra_claims"), + ] + + operations = [ + migrations.AddField( + model_name="transitagency", + name="logo_large", + field=models.ImageField( + blank=True, + default=None, + help_text="The large version of the transit agency's logo.", + null=True, + upload_to=benefits.core.models.agency_logo_large, + ), + ), + migrations.AddField( + model_name="transitagency", + name="logo_small", + field=models.ImageField( + blank=True, + default=None, + help_text="The small version of the transit agency's logo.", + null=True, + upload_to=benefits.core.models.agency_logo_small, + ), + ), + ] diff --git a/benefits/core/models.py b/benefits/core/models.py index 3b8b93079..86e63b6a1 100644 --- a/benefits/core/models.py +++ b/benefits/core/models.py @@ -5,6 +5,7 @@ from functools import cached_property import importlib import logging +import os import uuid from django.conf import settings @@ -126,6 +127,19 @@ def __str__(self): return self.name +def _agency_logo(instance, filename, size): + base, ext = os.path.splitext(filename) + return f"agencies/{instance.slug}-{size}" + ext + + +def agency_logo_small(instance, filename): + return _agency_logo(instance, filename, "sm") + + +def agency_logo_large(instance, filename): + return _agency_logo(instance, filename, "lg") + + class TransitAgency(models.Model): """An agency offering transit service.""" @@ -191,6 +205,20 @@ class TransitAgency(models.Model): help_text="The group of users who are allowed to do in-person eligibility verification and enrollment.", related_name="+", ) + logo_large = models.ImageField( + default=None, + null=True, + blank=True, + upload_to=agency_logo_large, + help_text="The large version of the transit agency's logo.", + ) + logo_small = models.ImageField( + default=None, + null=True, + blank=True, + upload_to=agency_logo_small, + help_text="The small version of the transit agency's logo.", + ) def __str__(self): return self.long_name diff --git a/tests/pytest/core/test_models.py b/tests/pytest/core/test_models.py index 8b81f3527..c89992a46 100644 --- a/tests/pytest/core/test_models.py +++ b/tests/pytest/core/test_models.py @@ -6,7 +6,15 @@ import pytest -from benefits.core.models import SecretNameField, EnrollmentFlow, TransitAgency, EnrollmentEvent, EnrollmentMethods +from benefits.core.models import ( + SecretNameField, + EnrollmentFlow, + TransitAgency, + EnrollmentEvent, + EnrollmentMethods, + agency_logo_small, + agency_logo_large, +) import benefits.secrets @@ -413,6 +421,18 @@ def test_TransitAgency_for_user_in_group_not_linked_to_any_agency(): assert TransitAgency.for_user(user) is None +@pytest.mark.django_db +def test_agency_logo_small(model_TransitAgency): + + assert agency_logo_small(model_TransitAgency, "local_filename.png") == "agencies/test-sm.png" + + +@pytest.mark.django_db +def test_agency_logo_large(model_TransitAgency): + + assert agency_logo_large(model_TransitAgency, "local_filename.png") == "agencies/test-lg.png" + + @pytest.mark.django_db def test_EnrollmentEvent_create(model_TransitAgency, model_EnrollmentFlow): ts = timezone.now()