diff --git a/ynr/apps/elections/templates/elections/sopn_for_ballot.html b/ynr/apps/elections/templates/elections/sopn_for_ballot.html index d4a39d6d9..c7bf42806 100644 --- a/ynr/apps/elections/templates/elections/sopn_for_ballot.html +++ b/ynr/apps/elections/templates/elections/sopn_for_ballot.html @@ -67,6 +67,7 @@

{% include "elections/includes/_sopn_debug.html" %} {% endif %} +
{% else %} @@ -78,13 +79,14 @@

{% url 'admin:official_documents_ballotsopn_change' object.sopn.id as url %} You can edit this in the admin interface (e.g. to delete it) {% endif %} - {% if object.sopn.uploaded_file.url|slice:"-3:" == "pdf" %} {% else %} diff --git a/ynr/apps/official_documents/templates/official_documents/sopn_viewer.js b/ynr/apps/official_documents/templates/official_documents/sopn_viewer.js index c5e7994e3..12bed5f16 100644 --- a/ynr/apps/official_documents/templates/official_documents/sopn_viewer.js +++ b/ynr/apps/official_documents/templates/official_documents/sopn_viewer.js @@ -9,7 +9,17 @@ var SOPN_VIEWER = (function () { var module = {}; - function load_page(pdf, container, page_num) { + function drawRectangles(context, rectangles) { + rectangles.forEach(rect => { + context.beginPath(); + context.rect(rect.x, rect.y, rect.width, rect.height); + context.lineWidth = rect.lineWidth || 1; + context.strokeStyle = rect.color || 'red'; + context.stroke(); + }); + } + + function load_page(pdf, container, page_num, rectanglesPerPage) { return pdf.getPage(page_num).then(function (page) { var scale = 1.2; @@ -25,6 +35,8 @@ var SOPN_VIEWER = (function () { var context = canvas.getContext("2d"); canvas.height = viewport.height; canvas.width = viewport.width; + console.log(canvas.height) + console.log(canvas.width) var renderContext = { canvasContext: context, viewport: viewport @@ -33,6 +45,12 @@ var SOPN_VIEWER = (function () { var renderTask = page.render(renderContext); return renderTask.promise.then(function () { container.append(page_container); + + if (rectanglesPerPage && rectanglesPerPage[page_num]) { + drawRectangles(context, rectanglesPerPage[page_num]); + } + + return page.getTextContent({normalizeWhitespace: true}); }).then(function (textContent) { var pdf_canvas = $(canvas), @@ -57,6 +75,8 @@ var SOPN_VIEWER = (function () { viewport: viewport, textDivs: [] }); + + }); } @@ -64,7 +84,7 @@ var SOPN_VIEWER = (function () { }); } - function ShowSOPNInline(sopn_url, ballot_paper_id, options) { + function ShowSOPNInline(sopn_url, ballot_paper_id, rectanglesPerPage) { // The container element var this_pdf_container = document.getElementById("sopn-" + ballot_paper_id); @@ -73,7 +93,7 @@ var SOPN_VIEWER = (function () { loadingTask.promise.then(function (pdf) { var promise = Promise.resolve(); for (let page = 1; page <= pdf.numPages; page++) { - promise = promise.then(() => load_page(pdf, this_pdf_container, page)); + promise = promise.then(() => load_page(pdf, this_pdf_container, page, rectanglesPerPage)); } return promise; }).then(null, function (error) { diff --git a/ynr/apps/sopn_parsing/models.py b/ynr/apps/sopn_parsing/models.py index 1c10ae2cd..77381326a 100644 --- a/ynr/apps/sopn_parsing/models.py +++ b/ynr/apps/sopn_parsing/models.py @@ -140,3 +140,45 @@ def parse_raw_data(self): def as_textractor_document(self): return response_parser.parse(json.loads(self.raw_data)) + + def get_withdrawals_bboxes(self): + # headers = self.as_pandas.iloc[0].tolist() + # get colmun index from headers + column = "5" + column_values = self.as_pandas[column].tolist() + cells_with_value = [] + for i, row in enumerate(column_values): + if row: + cells_with_value.append(i) + cells_with_value.pop(0) + # Deal with more than one page + textract_cells = [] + for table in self.as_textractor_document().tables: + for cell in table.table_cells: + # if str(cell.col_index-1) != column: + # continue + if cell.row_index - 1 in cells_with_value: + textract_cells.append(cell) + print(textract_cells) + + doc_height = 1429 + doc_width = 1010 + + page = 1 + box_data = {page: []} + for cell in textract_cells: + absolute_x = cell.x * doc_width + absolute_y = cell.y * doc_height + absolute_width = cell.width * doc_width + absolute_height = cell.height * doc_height + box_data[page].append( + { + "x": absolute_x, + "y": absolute_y, + "width": absolute_width, + "height": absolute_height, + "color": "red", + "lineWidth": 2, + }, + ) + return json.dumps(box_data)