From df15ea100b9d74b13702c3ca776ef887b0b6a1be Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 10 Sep 2025 13:17:50 +0200 Subject: [PATCH] feat: Add stub to translate ANs to sharepics --- src/fellchensammlung/models.py | 7 +- .../images/adoption-notice.svg | 171 ++++++++++++++++++ src/fellchensammlung/tools/img.py | 9 + src/fellchensammlung/urls.py | 2 + src/fellchensammlung/views.py | 8 +- 5 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 src/fellchensammlung/templates/fellchensammlung/images/adoption-notice.svg create mode 100644 src/fellchensammlung/tools/img.py diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 8cbf9f0..6495897 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -6,7 +6,7 @@ from django.utils import timezone from django.contrib.auth.models import Group from django.contrib.auth.models import AbstractUser from django.core.exceptions import ValidationError -from docutils.nodes import description +import base64 from .tools import misc, geo from notfellchen.settings import MEDIA_URL, base_url @@ -362,6 +362,11 @@ class Image(models.Model): def as_html(self): return f'{self.alt_text}' + @property + def as_base64(self): + encoded_string = base64.b64encode(self.image.file.read()) + return encoded_string.decode("utf-8") + class AdoptionNotice(models.Model): class Meta: diff --git a/src/fellchensammlung/templates/fellchensammlung/images/adoption-notice.svg b/src/fellchensammlung/templates/fellchensammlung/images/adoption-notice.svg new file mode 100644 index 0000000..cdc49a8 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/images/adoption-notice.svg @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + {{ adoption_notice.short_description }} + + {{ adoption_notice.name }} + + + + + + Ratte 1 + + + + + Ratte 2 + + + + diff --git a/src/fellchensammlung/tools/img.py b/src/fellchensammlung/tools/img.py new file mode 100644 index 0000000..455a188 --- /dev/null +++ b/src/fellchensammlung/tools/img.py @@ -0,0 +1,9 @@ +from django.template.loader import render_to_string + +from fellchensammlung.models import AdoptionNotice + + +def export_svg(adoption_notice): + result = render_to_string(template_name="fellchensammlung/images/adoption-notice.svg", + context={"adoption_notice": adoption_notice, }) + return result diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 192dae0..7dc9042 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -29,6 +29,8 @@ urlpatterns = [ path("tier//add-photo", views.add_photo_to_animal, name="animal-add-photo"), # ex: /adoption_notice/7/ path("vermittlung//", views.adoption_notice_detail, name="adoption-notice-detail"), + # ex: /adoption_notice/7/sharepic + path("vermittlung//sharepic", views.adoption_notice_sharepic, name="adoption-notice-sharepic"), # ex: /adoption_notice/7/edit path("vermittlung//edit", views.adoption_notice_edit, name="adoption-notice-edit"), # ex: /vermittlung/5/add-photo diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 855d58f..36baa34 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -27,7 +27,7 @@ from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, Moderatio from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, \ CommentForm, ReportCommentForm, AnimalForm, AdoptionNoticeFormAutoAnimal, SpeciesURLForm, RescueOrgInternalComment from .models import Language, Announcement -from .tools import i18n +from .tools import i18n, img from .tools.fedi import post_an_to_fedi from .tools.geo import GeoAPI, zoom_level_for_radius from .tools.metrics import gather_metrics_data @@ -943,3 +943,9 @@ def deactivate_an(request, adoption_notice_id): return redirect(reverse("adoption-notice-detail", args=[adoption_notice.pk], )) context = {"adoption_notice": adoption_notice, } return render(request, 'fellchensammlung/misc/deactivate-an.html', context=context) + + +def adoption_notice_sharepic(request, adoption_notice_id): + adoption_notice = get_object_or_404(AdoptionNotice, pk=adoption_notice_id) + svg_data = img.export_svg(adoption_notice) + return HttpResponse(svg_data, content_type="image/svg+xml")