Skip to content

Commit

Permalink
Merge pull request #1611 from laws-africa/portion-popups
Browse files Browse the repository at this point in the history
provision popups
  • Loading branch information
longhotsummer authored Nov 3, 2023
2 parents 89c36df + 647aa14 commit 0cbc91a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
8 changes: 8 additions & 0 deletions peachjam/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 25 additions & 17 deletions peachjam/templates/peachjam/document_popup.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
{% load i18n %}
<div class="document-popup-content">
<div class="mb-2">
<div>
{% block title %}
<b>{{ document.title }}</b>
{% block title-badges %}
{% if document.metadata_json.repealed %}
<span class="badge bg-danger">{% trans 'repealed' %}</span>
{% endif %}
{% if portion_html %}
{% block portion %}
<la-akoma-ntoso frbr-expression-uri="{{ document.expression_frbr_uri }}">
{{ portion_html|safe }}
</la-akoma-ntoso>
{% endblock %}
{% else %}
<div class="mb-2">
<div>
{% block title %}
<b>{{ document.title }}</b>
{% block title-badges %}
{% if document.metadata_json.repealed %}
<span class="badge bg-danger">{% trans 'repealed' %}</span>
{% endif %}
{% endblock %}
{% endblock %}
</div>
{% block citation %}
{% if document.citation %}
<div>
<i>{{ document.citation }}</i>
</div>
{% endif %}
{% endblock %}
</div>
{% block citation %}
{% if document.citation %}
<div>
<i>{{ document.citation }}</i>
</div>
{% endif %}
{% endblock %}
</div>
{% block date %}<div>{{ document.date }}</div>{% endblock %}
{% block date %}<div>{{ document.date }}</div>{% endblock %}
{% endif %}
</div>
40 changes: 36 additions & 4 deletions peachjam/views/widgets.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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

0 comments on commit 0cbc91a

Please sign in to comment.