From 5a2b11b44e9fefa7e8676ccd0fdc3134a96dfe24 Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 10 Sep 2025 17:44:33 +0200 Subject: [PATCH] feat: Add basic embeddable view for ans of rescue orgs --- src/fellchensammlung/aviews/__init__.py | 0 src/fellchensammlung/aviews/embeddables.py | 18 ++++++++ src/fellchensammlung/aviews/helpers.py | 23 ++++++++++ src/fellchensammlung/aviews/urls.py | 12 ++++++ src/fellchensammlung/models.py | 8 +++- .../embeddables/embedding-base.html | 43 +++++++++++++++++++ .../embeddables/list-adoption-notices.html | 10 +++++ src/fellchensammlung/urls.py | 8 +++- src/fellchensammlung/views.py | 2 - 9 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 src/fellchensammlung/aviews/__init__.py create mode 100644 src/fellchensammlung/aviews/embeddables.py create mode 100644 src/fellchensammlung/aviews/helpers.py create mode 100644 src/fellchensammlung/aviews/urls.py create mode 100644 src/fellchensammlung/templates/fellchensammlung/embeddables/embedding-base.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/embeddables/list-adoption-notices.html diff --git a/src/fellchensammlung/aviews/__init__.py b/src/fellchensammlung/aviews/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/fellchensammlung/aviews/embeddables.py b/src/fellchensammlung/aviews/embeddables.py new file mode 100644 index 0000000..6fa25ec --- /dev/null +++ b/src/fellchensammlung/aviews/embeddables.py @@ -0,0 +1,18 @@ +from django.shortcuts import get_object_or_404, render + +from fellchensammlung.aviews.helpers import headers +from fellchensammlung.models import RescueOrganization, AdoptionNotice, Species + + +@headers({"X-Robots-Tag": "noindex"}) +def list_ans_per_rescue_organization(request, rescue_organization_id, species_slug=None): + org = get_object_or_404(RescueOrganization, pk=rescue_organization_id) + if species_slug is None: + adoption_notices = AdoptionNotice.objects.filter(organization=org) + else: + ans_of_rescue_org = AdoptionNotice.objects.filter(organization=org) + species = get_object_or_404(Species, slug=species_slug) + adoption_notices = [adoption_notice for adoption_notice in ans_of_rescue_org if species in adoption_notice.species] + + template = 'fellchensammlung/embeddables/list-adoption-notices.html' + return render(request, template, context={"adoption_notices": adoption_notices}) \ No newline at end of file diff --git a/src/fellchensammlung/aviews/helpers.py b/src/fellchensammlung/aviews/helpers.py new file mode 100644 index 0000000..9d13f6f --- /dev/null +++ b/src/fellchensammlung/aviews/helpers.py @@ -0,0 +1,23 @@ + +def headers(headers): + """Decorator adding arbitrary HTTP headers to the response. + + This decorator adds HTTP headers specified in the argument (map), to the + HTTPResponse returned by the function being decorated. + + Example: + + @headers({'Refresh': '10', 'X-Bender': 'Bite my shiny, metal ass!'}) + def index(request): + .... + Source: https://djangosnippets.org/snippets/275/ + """ + def headers_wrapper(fun): + def wrapped_function(*args, **kwargs): + response = fun(*args, **kwargs) + for key in headers: + response[key] = headers[key] + return response + return wrapped_function + return headers_wrapper + diff --git a/src/fellchensammlung/aviews/urls.py b/src/fellchensammlung/aviews/urls.py new file mode 100644 index 0000000..402449c --- /dev/null +++ b/src/fellchensammlung/aviews/urls.py @@ -0,0 +1,12 @@ +from django.urls import path + +from . import embeddables + +urlpatterns = [ + path("tierschutzorganisationen//vermittlungen/", + embeddables.list_ans_per_rescue_organization, + name="list-adoption-notices-for-rescue-organization"), + path("tierschutzorganisationen//vermittlungen//", + embeddables.list_ans_per_rescue_organization, + name="list-adoption-notices-for-rescue-organization-species"), +] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 6495897..21ddc4c 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -421,6 +421,13 @@ class AdoptionNotice(models.Model): num_per_sex[sex] = self.animals.filter(sex=sex).count() return num_per_sex + @property + def species(self): + species = set() + for animal in self.animals: + species.add(animal.species) + return species + @property def last_checked_hr(self): time_since_last_checked = timezone.now() - self.last_checked @@ -466,7 +473,6 @@ class AdoptionNotice(models.Model): def description_100_short(self): return self._get_short_description(90) - def get_absolute_url(self): """Returns the url to access a detailed page for the adoption notice.""" return reverse('adoption-notice-detail', args=[str(self.id)]) diff --git a/src/fellchensammlung/templates/fellchensammlung/embeddables/embedding-base.html b/src/fellchensammlung/templates/fellchensammlung/embeddables/embedding-base.html new file mode 100644 index 0000000..5f216c6 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/embeddables/embedding-base.html @@ -0,0 +1,43 @@ +{% load custom_tags %} +{% load i18n %} +{% load static %} +{% get_current_language as LANGUAGE_CODE %} + + + + + + {% block title %}{% endblock %} + {% block og_title %}{% endblock %} + {% block description %}{% endblock %} + {% block og_description %}{% endblock %} + {% block og_image %}{% endblock %} + + + + + + + + + + + + + + {% block additional_scrips %}{% endblock %} + + + + + + {% get_oxitraffic_script_if_enabled %} + + + +
+ {% block content %}{% endblock %} +
+ + + diff --git a/src/fellchensammlung/templates/fellchensammlung/embeddables/list-adoption-notices.html b/src/fellchensammlung/templates/fellchensammlung/embeddables/list-adoption-notices.html new file mode 100644 index 0000000..f9cf111 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/embeddables/list-adoption-notices.html @@ -0,0 +1,10 @@ +{% extends "fellchensammlung/embeddables/embedding-base.html" %} +{% load custom_tags %} +{% load i18n %} + + +{% block content %} +
+ {% include "fellchensammlung/lists/list-adoption-notices.html" %} +
+{% endblock %} diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 7dc9042..9fc3518 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -4,6 +4,7 @@ from .forms import CustomRegistrationForm from .feeds import LatestAdoptionNoticesFeed from . import views, registration_views +from fellchensammlung.aviews import embeddables from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView from django.contrib.sitemaps.views import sitemap @@ -30,7 +31,8 @@ urlpatterns = [ # 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"), + 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 @@ -129,4 +131,8 @@ urlpatterns = [ ############### path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="django.contrib.sitemaps.views.sitemap"), + ########### + ## EMBED ## + + path('embed/', include('fellchensammlung.aviews.urls')), ] diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 36baa34..a25b70a 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -764,8 +764,6 @@ def external_site_warning(request, template_name='fellchensammlung/external-site def list_rescue_organizations(request, species=None, template='fellchensammlung/animal-shelters.html'): if species is None: - # rescue_organizations = RescueOrganization.objects.all() - org_search = RescueOrgSearch(request) rescue_organizations = org_search.get_rescue_orgs() else: