Skip to content

Commit

Permalink
views
Browse files Browse the repository at this point in the history
  • Loading branch information
goose-life committed Nov 12, 2024
1 parent 35fce68 commit 31c629c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
var deferred = this.pendingTextSave = $.Deferred();
deferred
.then(function(response) {
// TODO: response.output should be the updated (by the server) version of the edited provision
var newFragment = $.parseXML(response.output);

if (fragmentRule === 'akomaNtoso') {
Expand Down
84 changes: 78 additions & 6 deletions indigo_app/views/documents.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import json

from django.views.generic import DetailView
from django.http import Http404
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import DetailView, FormView
from lxml import etree

from indigo.plugins import plugins
from indigo_api.models import Document, Country, Subtype, Work
from indigo_api.serializers import DocumentSerializer, WorkSerializer, WorkAmendmentSerializer
from indigo_api.views.documents import DocumentViewSet

from indigo_app.forms import DocumentForm
from indigo_app.forms import DocumentForm, DocumentProvisionForm
from .base import AbstractAuthedIndigoView


Expand All @@ -19,15 +20,26 @@ class DocumentDetailView(AbstractAuthedIndigoView, DetailView):
pk_url_kwarg = 'doc_id'
template_name = 'indigo_api/document/show.html'
permission_required = ('indigo_api.view_document',)
provision_eid = None

def get(self, request, *args, **kwargs):
self.object = self.get_object()
# TODO: decide on cut-off size
if len(self.object.document_xml) >= 5000000:
# for large documents, redirect to the provision chooser to edit just a portion
return redirect('choose_document_provision', doc_id=self.object.id)
# otherwise, follow the normal get() path
context = self.get_context_data(object=self.object)
return self.render_to_response(context)

def get_object(self, queryset=None):
doc = super(DocumentDetailView, self).get_object(queryset)
doc = super().get_object(queryset)
if doc.deleted:
raise Http404()
return doc

def get_context_data(self, **kwargs):
context = super(DocumentDetailView, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)

doc = self.object

Expand All @@ -48,7 +60,7 @@ def get_context_data(self, **kwargs):
# TODO do this in a better place
context['countries'] = Country.objects.select_related('country').prefetch_related('localities', 'publication_set', 'country').all()

context['document_content_json'] = json.dumps(doc.document_xml)
context['document_content_json'] = self.get_document_content_json(doc)

# add 'numbered_title_localised' to each amendment
amendments = WorkAmendmentSerializer(context={'request': self.request}, many=True)\
Expand Down Expand Up @@ -76,6 +88,66 @@ def get_context_data(self, **kwargs):

return context

def get_document_content_json(self, document):
return json.dumps(document.document_xml)


class ChooseDocumentProvisionView(AbstractAuthedIndigoView, DetailView, FormView):
form_class = DocumentProvisionForm
model = Document
context_object_name = 'document'
pk_url_kwarg = 'doc_id'
template_name = 'indigo_api/document/_provisions.html'
provision = None

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['provisions'] = self.get_shallow_toc(self.object.table_of_contents())
context['country'] = self.object.work.country
context['place'] = self.object.work.place
context['work'] = self.object.work
return context

def get_shallow_toc(self, toc):
# TODO: choose depth somehow; loading the whole thing can take ages
# -- or use htmx to only load children when selected?
for e in toc:
for c in e.children:
for gc in c.children:
gc.children = []
return toc

def form_valid(self, form):
self.provision = form.cleaned_data['provision']
return super().form_valid(form)

def get_success_url(self):
return reverse('document_provision', kwargs={'doc_id': int(self.kwargs['doc_id']), 'eid': self.provision})


class DocumentProvisionDetailView(DocumentDetailView):
eid = None

def get(self, request, *args, **kwargs):
# don't call super(), we'll end up in a loop choosing a provision forever
self.object = self.get_object()
self.eid = self.kwargs.get('eid')
context = self.get_context_data(object=self.object)
return self.render_to_response(context)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['provision_mode'] = self.eid is not None
return context

def get_document_content_json(self, document):
component = document.doc.main
elems = component.xpath(f".//a:*[@eId='{self.eid}']", namespaces={'a': self.object.doc.namespace})
# TODO: throw a useful error including the eId if we don't have exactly one match
assert len(elems) == 1

return json.dumps(etree.tostring(elems[0], encoding='unicode'))


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

0 comments on commit 31c629c

Please sign in to comment.