From 647aa141f64a269f488c829cd192b7bb073b6c01 Mon Sep 17 00:00:00 2001 From: Greg Kempe Date: Fri, 3 Nov 2023 11:56:01 +0200 Subject: [PATCH] provision popups --- peachjam/helpers.py | 8 ++++ .../templates/peachjam/document_popup.html | 42 +++++++++++-------- peachjam/views/widgets.py | 40 ++++++++++++++++-- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/peachjam/helpers.py b/peachjam/helpers.py index 3c3770b73..6ce7cc8fe 100644 --- a/peachjam/helpers.py +++ b/peachjam/helpers.py @@ -70,3 +70,11 @@ def to_python(self, value): def to_url(self, value): # invalid values will raise ValueError which will raise NoReverseMatch return datetime.strptime(value, "%Y-%m-%d").date().strftime("%Y-%m-%d") + + +def parse_utf8_html(html): + """Parse html assuming utf8 encoding and return lxml tree.""" + import lxml.html + + parser = lxml.html.HTMLParser(encoding="utf-8") + return lxml.html.fromstring(html, parser=parser) diff --git a/peachjam/templates/peachjam/document_popup.html b/peachjam/templates/peachjam/document_popup.html index 132cf88e9..14f0d179a 100644 --- a/peachjam/templates/peachjam/document_popup.html +++ b/peachjam/templates/peachjam/document_popup.html @@ -1,23 +1,31 @@ {% load i18n %}
-
-
- {% block title %} - {{ document.title }} - {% block title-badges %} - {% if document.metadata_json.repealed %} - {% trans 'repealed' %} - {% endif %} + {% if portion_html %} + {% block portion %} + + {{ portion_html|safe }} + + {% endblock %} + {% else %} +
+
+ {% block title %} + {{ document.title }} + {% block title-badges %} + {% if document.metadata_json.repealed %} + {% trans 'repealed' %} + {% endif %} + {% endblock %} {% endblock %} +
+ {% block citation %} + {% if document.citation %} +
+ {{ document.citation }} +
+ {% endif %} {% endblock %}
- {% block citation %} - {% if document.citation %} -
- {{ document.citation }} -
- {% endif %} - {% endblock %} -
- {% block date %}
{{ document.date }}
{% endblock %} + {% block date %}
{{ document.date }}
{% endblock %} + {% endif %}
diff --git a/peachjam/views/widgets.py b/peachjam/views/widgets.py index 2c1cf5f89..c7f2d4cd1 100644 --- a/peachjam/views/widgets.py +++ b/peachjam/views/widgets.py @@ -1,8 +1,10 @@ +import lxml.html +from cobalt.uri import FrbrUri from django.http import Http404 from django.utils.translation import get_language from django.views.generic import DetailView -from peachjam.helpers import add_slash +from peachjam.helpers import add_slash, parse_utf8_html from peachjam.models import CoreDocument @@ -14,9 +16,39 @@ class DocumentPopupView(DetailView): template_name = "peachjam/document_popup.html" def get_object(self, *args, **kwargs): - obj = self.model.objects.best_for_frbr_uri( - add_slash(self.kwargs.get("frbr_uri")), get_language() - )[0] + try: + frbr_uri = FrbrUri.parse(add_slash(self.kwargs["frbr_uri"])) + except ValueError: + raise Http404() + + self.portion = frbr_uri.portion + frbr_uri.portion = None + if frbr_uri.expression_date: + uri = frbr_uri.expression_uri() + else: + uri = frbr_uri.work_uri() + + obj = self.model.objects.best_for_frbr_uri(uri, get_language())[0] if not obj: raise Http404() return obj + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + if self.portion: + if not (self.object.content_html and self.object.content_html_is_akn): + raise Http404() + + # try to find the portion within the object + try: + tree = parse_utf8_html(self.object.content_html) + elems = tree.xpath(f'//*[@id="{self.portion}"]') + if elems: + context["portion_html"] = lxml.html.tostring( + elems[0], encoding="unicode" + ) + except ValueError: + raise Http404() + + return context