Skip to content

Commit

Permalink
292 serialize person person relations into listperson (#293)
Browse files Browse the repository at this point in the history
* tei (relation) endpoint

* add docstring to NetworkView class for improved clarity
  • Loading branch information
csae8092 authored Jan 12, 2025
1 parent 7e1e907 commit 750f563
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions network/templates/network/list_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h1 class="display-1 text-center">
<div class="row">
<div class="btn-group" role="group" class="text-end">
<a type="button" class="btn btn-outline-primary" href="{% url 'network:data' %}{% querystring %}&format=csv">CSV</a>
<a type="button" class="btn btn-outline-primary" href="{% url 'network:tei' %}{% querystring %}">TEI</a>
<a type="button" class="btn btn-outline-primary" href="{% url 'network:data' %}{% querystring %}&format=cosmograph">JSON</a>
<a type="button" class="btn btn-outline-primary" href="{% url 'network:network' %}{% querystring %}&format=cosmograph">Als Netzwerk</a>
<a type="button" class="btn btn-outline-primary" href="{% url 'network:calender' %}{% querystring %}">Als Kalender</a>
Expand Down
2 changes: 2 additions & 0 deletions network/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
edges_as_geojson,
edges_as_calender,
MapView,
get_person_person_tei,
)


Expand All @@ -19,4 +20,5 @@
path("calender-data/", edges_as_calender, name="calender_data"),
path("calender/", CalenderView.as_view(), name="calender"),
path("map/", MapView.as_view(), name="map"),
path("tei/", get_person_person_tei, name="tei"),
]
60 changes: 58 additions & 2 deletions network/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import csv
import json
import pandas as pd
import lxml.etree as ET
from django.apps import apps
from django.http import HttpResponse, JsonResponse
from django.utils.text import slugify
from django.views.generic import TemplateView

from acdh_tei_pyutils.tei import TeiReader

from browsing.browsing_utils import (
GenericListView,
Expand All @@ -17,7 +19,61 @@
from network.utils import get_coords, df_to_geojson_vect, iso_to_lat_long


tei_template = """
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>PMB-Beziehungen</title>
</titleStmt>
<publicationStmt>
<p>Publication Information</p>
</publicationStmt>
<sourceDesc>
<p>PMB-Export</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<body>
<listRelation/>
</body>
</text>
</TEI>
"""


def get_person_person_tei(request):
doc = TeiReader(tei_template)
root = doc.any_xpath(".//tei:listRelation")[0]
query_params = request.GET
qs = EdgeListFilter(query_params, queryset=Edge.objects.all()).qs
for x in qs:
relation = ET.SubElement(root, "{http://www.tei-c.org/ns/1.0}relation")
print(x.edge_label)
relation.attrib["name"] = slugify(x.edge_label)
relation.attrib["active"] = f"#pmb{x.source_id}"
relation.attrib["passive"] = f"#pmb{x.target_id}"
if x.start_date:
relation.attrib["from-iso"] = f"{x.start_date}"
if x.end_date:
relation.attrib["to-iso"] = f"{x.end_date}"
relation.attrib["n"] = f"{x.target_label}{x.edge_label}{x.source_label}"
xml_str = doc.return_string()
return HttpResponse(xml_str, content_type="application/xml")


class NetworkView(TemplateView):
"""
A Django view that renders the network template and provides context data for the template.
Attributes:
template_name (str): The path to the template used by this view.
Methods:
get_context_data(**kwargs):
Retrieves context data for the template, including a list of models with their associated
color, icon, and verbose name. Models that do not have these attributes are skipped.
"""

template_name = "network/network.html"

def get_context_data(self, **kwargs):
Expand Down Expand Up @@ -69,7 +125,7 @@ def edges_as_calender(request):
.exclude(start_date__lte="0001-01-01")
)
values_list = [x.name for x in Edge._meta.get_fields()]
qs = EdgeListFilter(request.GET, queryset=queryset).qs
qs = EdgeListFilter(query_params, queryset=queryset).qs
items = list(qs.values_list(*values_list))
df = pd.DataFrame(list(items), columns=values_list)
start_date = str(df["start_date"].min())
Expand Down

0 comments on commit 750f563

Please sign in to comment.