Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counters #2347

Merged
merged 8 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions indigo/xmlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ def rewrite_all_attachment_work_components(xml_doc):
'a:FRBRExpression': lambda: frbr_uri.expression_uri(),
'a:FRBRManifestation': lambda: frbr_uri.manifestation_uri(),
}[part]()
return counter
14 changes: 11 additions & 3 deletions indigo_app/static/javascript/indigo/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,21 @@
rewriteIds: function() {
// rewrite all eIds before setting the content
// in provision mode, retain the eId of the parent element as the prefix
let eidPrefix;
// and use the counters of all preceding provisions for context
let eidPrefix,
eidRewriter = new indigoAkn.EidRewriter(),
workComponentRewriter = new indigoAkn.WorkComponentRewriter();
if (Indigo.Preloads.provisionEid && Indigo.Preloads.provisionEid.lastIndexOf('__') > -1) {
eidPrefix = Indigo.Preloads.provisionEid.substring(0, Indigo.Preloads.provisionEid.lastIndexOf('__'));
}
new indigoAkn.EidRewriter().rewriteAllEids(this.xmlDocument.documentElement, eidPrefix);
if (Indigo.Preloads.provisionEid) {
eidRewriter.counters = JSON.parse(JSON.stringify(Indigo.Preloads.provisionCounters));
eidRewriter.eidCounter = JSON.parse(JSON.stringify(Indigo.Preloads.eidCounter));
workComponentRewriter.counters = JSON.parse(JSON.stringify(Indigo.Preloads.attachmentCounters));
}
eidRewriter.rewriteEid(this.xmlDocument.documentElement, eidPrefix);
// rewrite all attachment FRBR URI work components too
new indigoAkn.WorkComponentRewriter().rewriteAllAttachmentWorkComponents(this.xmlDocument.documentElement);
workComponentRewriter.rewriteAttachmentWorkComponent(this.xmlDocument.documentElement);
},

// serialise an XML node, or the entire document if node is not given, to a string
Expand Down
3 changes: 3 additions & 0 deletions indigo_app/templates/indigo_api/document/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ <h5 class="main-header-title">
window.Indigo.Preloads.document = {{ document_json|safe }};
window.Indigo.Preloads.documentContent = {{ document_content_json|safe }};
window.Indigo.Preloads.provisionEid = "{{ provision_eid }}";
window.Indigo.Preloads.provisionCounters = {% if provision_counters %}{{ provision_counters|safe }}{% else %}null{% endif %};
window.Indigo.Preloads.eidCounter = {% if eid_counter %}{{ eid_counter|safe }}{% else %}null{% endif %};
window.Indigo.Preloads.attachmentCounters = {% if attachment_counters %}{{ attachment_counters|safe }}{% else %}null{% endif %};
window.Indigo.Preloads.amendments = {{ amendments_json|safe }};
window.Indigo.Preloads.expressions = {{ expressions_json|safe }};
{% if pyodide_packages_json %}
Expand Down
31 changes: 29 additions & 2 deletions indigo_app/views/documents.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json

from django.conf import settings
from django.contrib import messages
from django.http import Http404
Expand All @@ -11,8 +10,9 @@

import bluebell
import cobalt
from cobalt import Portion
from bluebell.xml import XmlGenerator
from indigo.plugins import plugins
from indigo.xmlutils import rewrite_all_attachment_work_components
from indigo_api.models import Document, Country, Subtype, Work
from indigo_api.serializers import DocumentSerializer, WorkSerializer, WorkAmendmentSerializer
from indigo_api.views.documents import DocumentViewSet
Expand Down Expand Up @@ -139,11 +139,38 @@ def get(self, request, *args, **kwargs):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['provision_eid'] = self.eid
context['provision_counters'], context['eid_counter'], context['attachment_counters'] = self.get_counters()
return context

def get_document_content_json(self, document):
return json.dumps(etree.tostring(self.provision_xml, encoding='unicode'))

def get_counters(self):
""" Generate provision and eid counters based on the content preceding our element.
This way we don't end up 'correcting' e.g. part_II_2 to part_II,
because the context will now be aware of the preceding part_II that isn't being edited.
"""
root = etree.fromstring(self.object.document_xml)
element = root.xpath(f'.//a:*[@eId="{self.eid}"]', namespaces={'a': self.object.doc.namespace})[0]
parent = element.getparent()
# remove everything from our element onwards (inclusive) from the tree
for sibling in element.itersiblings():
# this will remove e.g. parts III, IV, etc -- or if we're lower down, e.g. part_II__sec_3__subsec_2 etc
parent.remove(sibling)
parent.remove(element)
# now remove all the ancestors' following siblings too, e.g. attachments,
# or e.g. part_II__sec_4, part_III, up to attachments
while parent is not None:
for aunt in parent.itersiblings():
aunt.getparent().remove(aunt)
parent = parent.getparent()

generator = XmlGenerator(self.object.frbr_uri)
generator.generate_eids(root)
doc = cobalt.StructuredDocument.for_document_type(self.object.work.work_uri.doctype)(etree.tostring(root, encoding='unicode'))
attachment_counters = {'__attachments': rewrite_all_attachment_work_components(doc)}
return json.dumps(generator.ids.counters), json.dumps(generator.ids.eid_counter), json.dumps(attachment_counters)


class DocumentPopupView(AbstractAuthedIndigoView, DetailView):
model = Document
Expand Down