Skip to content

Commit

Permalink
Merge pull request #664 from City-of-Helsinki/LINK-1447-image-alt-non…
Browse files Browse the repository at this point in the history
…-translatable

LINK-1447 | Make Image alt_text non_translatable
  • Loading branch information
nicobav authored Aug 18, 2023
2 parents f2cd2e4 + de9a01b commit a3dbcf3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
16 changes: 3 additions & 13 deletions events/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@
UserIsAdminInAnyOrganization,
)
from events.renderers import DOCXRenderer
from events.translation import (
EventTranslationOptions,
ImageTranslationOptions,
PlaceTranslationOptions,
)
from events.translation import EventTranslationOptions, PlaceTranslationOptions
from helevents.models import User
from helevents.serializers import UserSerializer
from linkedevents.registry import register_view, viewset_classes_by_model
Expand Down Expand Up @@ -1786,15 +1782,9 @@ def get_queryset(self):
val = self.request.query_params.get("text")
if val:
val = val.lower()
qset = Q(name__icontains=val)
filter_query = Q(name__icontains=val) | Q(alt_text__icontains=val)

# Free string search from all translated event fields
image_fields = ImageTranslationOptions.fields
for field in image_fields:
# check all languages for each field
qset |= _text_qset_by_translated_field(field, val)

queryset = queryset.filter(qset)
queryset = queryset.filter(filter_query)
return queryset

def perform_destroy(self, instance):
Expand Down
37 changes: 37 additions & 0 deletions events/migrations/0094_remove_image_alt_text_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 3.2.20 on 2023-08-18 06:46

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("events", "0093_increase_provider_contact_info_max_length"),
]

operations = [
migrations.RemoveField(
model_name="image",
name="alt_text_ar",
),
migrations.RemoveField(
model_name="image",
name="alt_text_en",
),
migrations.RemoveField(
model_name="image",
name="alt_text_fi",
),
migrations.RemoveField(
model_name="image",
name="alt_text_ru",
),
migrations.RemoveField(
model_name="image",
name="alt_text_sv",
),
migrations.RemoveField(
model_name="image",
name="alt_text_zh_hans",
),
]
20 changes: 20 additions & 0 deletions events/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def setUp(self):
data_source=self.data_source,
publisher=self.org_1,
url="http://fake.url/image-1/",
alt_text="Lorem",
)
self.image_2 = Image.objects.create(
name="image-2",
Expand Down Expand Up @@ -265,3 +266,22 @@ def test_get_image_list_with_publisher(self):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["data"]), 1)

def test_get_image_alt_text(self):
url = reverse("image-detail", kwargs={"pk": self.image_1.id})

response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

alt_text = response.data["alt_text"]
self.assertEqual(alt_text, "Lorem")

def test_text_search_by_image_alt_text(self):
url = "{0}?text=lorem".format(reverse("image-list"))

response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["data"]), 1)

alt_text = response.data["data"][0]["alt_text"]
self.assertEqual(alt_text, "Lorem")
20 changes: 20 additions & 0 deletions events/tests/test_event_images_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,26 @@ def test__upload_an_url(api_client, settings, list_url, image_url, user, organiz
assert image.url == "https://commons.wikimedia.org/wiki/File:Common_Squirrel.jpg"


@pytest.mark.django_db
def test_upload_an_url_with_alt_text(
api_client, settings, list_url, image_url, user, organization
):
organization.admin_users.add(user)
api_client.force_authenticate(user)

image_url = image_url.copy()
image_url["alt_text"] = "Lorem"

response = api_client.post(list_url, image_url)
assert response.status_code == 201
assert Image.objects.all().count() == 1

image = Image.objects.get(pk=response.data["id"])
assert image.created_by == user
assert image.last_modified_by == user
assert image.alt_text == "Lorem"


@pytest.mark.django_db
def test__upload_an_url_with_api_key(
api_client, settings, list_url, image_url, data_source, organization
Expand Down
9 changes: 1 addition & 8 deletions events/translation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from modeltranslation.translator import TranslationOptions, translator

from .models import Event, Image, Keyword, KeywordSet, Language, License, Offer, Place
from .models import Event, Keyword, KeywordSet, Language, License, Offer, Place


class LanguageTranslationOptions(TranslationOptions):
Expand Down Expand Up @@ -67,10 +67,3 @@ class LicenseTranslationOptions(TranslationOptions):


translator.register(License, LicenseTranslationOptions)


class ImageTranslationOptions(TranslationOptions):
fields = ("alt_text",)


translator.register(Image, ImageTranslationOptions)

0 comments on commit a3dbcf3

Please sign in to comment.