Skip to content

Commit

Permalink
Merge pull request #934 from SuffolkLITLab/error-auto-drafting
Browse files Browse the repository at this point in the history
Don't try "auto drafting" with an empty PDF or DOCX file
  • Loading branch information
nonprofittechy authored Jun 5, 2024
2 parents 99e3316 + 2ad728d commit a825424
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
4 changes: 4 additions & 0 deletions docassemble/ALWeaver/data/questions/assembly_line.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,10 @@ code: |
interview.auto_assign_attributes()
interview_label_draft = varname(interview.title).lower()
# Check for an empty file
if not len(interview.all_fields):
force_ask('empty_pdf')
# TODO: refactor this at some point, this is a shim to create objects block but we
# shouldn't need it forever.
# person_candidates = interview.all_fields.get_person_candidates()
Expand Down
11 changes: 9 additions & 2 deletions docassemble/ALWeaver/data/questions/template_validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,18 @@ code: |
]
---
event: empty_pdf
question: You uploaded an empty PDF
question: |
% if interview.get_file_types() == "pdf":
You uploaded a PDF without any fields
% elif interview.get_file_types() == "docx":
You uploaded a DOCX file without any Jinja2 syntax
% endif
subquestion: |
The PDF you uploaded (${ interview.uploaded_templates[0].filename }) does not have any
The document you uploaded (${ interview.uploaded_templates[0].filename }) does not have any
fillable fields. To make a new interview with the Weaver, we need to have a
document that has fillable fields.
% if interview.get_file_types() == "pdf":
If you saved a DOCX file as a PDF, you will need to add fillable fields in a
tool like [Adobe Acrobat](https://acrobat.adobe.com).
For more info, see the [documentation](https://suffolklitlab.org/docassemble-AssemblyLine-documentation/docs/pdfs).
Expand All @@ -244,6 +250,7 @@ subquestion: |
or XFA form), you may need to flatten it first. You can flatten an XFA form
using [iText PDF's free
tool](https://itextpdf.com/en/demos/flatten-dynamic-xfa-pdf-free-online).
% endif
buttons:
- Restart: restart
---
Expand Down
22 changes: 15 additions & 7 deletions docassemble/ALWeaver/data/templates/output.mako
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ metadata:
description: |-
${ indent(interview.description, by=4) }
can_I_use_this_form: |
% if interview.can_I_use_this_form:
% if showifdef("interview.can_I_use_this_form"):
${ indent(interview.can_I_use_this_form, by=4) }
% endif
before_you_start: |
% if interview.getting_started:
% if showifdef("interview.getting_started"):
${ indent(interview.getting_started, by=4) }
% endif
maturity: production
estimated_completion_minutes: 60
estimated_completion_delta: 30
estimated_completion_minutes: ${ showifdef("interview.estimated_completion_minutes",'""')}
estimated_completion_delta: ${ showifdef("interview.estimated_completion_delta", '""')}
% if interview.categories.any_true():
LIST_topics:
% for category in sorted(set(interview.categories.true_values())):
Expand Down Expand Up @@ -58,7 +58,11 @@ ${ indent(interview.getting_started, by=4) }
% else:
original_form: []
% endif
% if defined("interview.original_form_published_on"):
original_form_published_on: ${ interview.original_form_published_on.format("yyyy-MM-dd") or '""'}
% else:
original_form_published_on: ""
% endif
% if interview.help_page_url:
help_page_url: >-
${ indent(interview.help_page_url, by=4) }
Expand All @@ -80,17 +84,17 @@ ${ indent(interview.help_page_title, by=4) }
generated_on: "${ today().format("yyyy-MM-dd") }"
languages:
- en
jurisdiction: ${ interview.jurisdiction }
jurisdiction: ${ showifdef("interview.jurisdiction", '""') }
review_date: ${ today().format("yyyy-MM-dd")}
form_titles:
- ${ interview.title }
% if interview.form_number:
% if showifdef("interview.form_number"):
form_numbers:
- ${ interview.form_number }
% else:
form_numbers: []
% endif
% if interview.filing_fee:
% if showifdef("interview.filing_fee"):
fees:
- Filing fee: ${ currency(interview.filing_fee) }
% endif
Expand Down Expand Up @@ -201,9 +205,13 @@ question: |
subquestion: |
${ indent(interview.getting_started, 2) }

% if defined("interview.can_I_use_this_form"):
${ indent(interview.can_I_use_this_form, by=2)}
% endif

% if defined("interview.estimated_completion_minutes"):
Most people take about ${ interview.estimated_completion_minutes or "_______________"} minutes to complete this interview.
% endif
<%doc>
Main question loop
</%doc>\
Expand Down
22 changes: 18 additions & 4 deletions docassemble/ALWeaver/interview_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import zipfile
import spacy
from dataclasses import dataclass
import pycountry

mako.runtime.UNDEFINED = DAEmpty()

Expand Down Expand Up @@ -1608,7 +1609,19 @@ def auto_assign_attributes(
)

if jurisdiction:
self.state = jurisdiction
try:
if jurisdiction.upper() in {
subdivision.code.split("-")[1]
for subdivision in pycountry.subdivisions.get(country_code="US")
}:
self.jurisdiction = "NAM-US-US+" + jurisdiction.upper()
self.state = jurisdiction.upper()
else:
self.jurisdiction = jurisdiction
self.state = jurisdiction
except:
self.jurisdiction = jurisdiction
self.state = jurisdiction
if categories:
nsmi_tmp = (
categories[1:-1].replace("'", "").strip()
Expand Down Expand Up @@ -1790,9 +1803,7 @@ def _guess_categories(self, title) -> List[str]:
full_text += docx_data.text
categories = formfyxer.spot(
title + ": " + full_text,
token=get_config("assembly line", {}).get(
"tools.suffolklitlab.org api key", None
),
token=get_config("assembly line", {}).get("spot api key", None),
)
if categories and not "401" in categories:
return categories
Expand Down Expand Up @@ -2025,11 +2036,14 @@ def auto_group_fields(self):
"tools.suffolklitlab.org api key", None
),
)
if not field_grouping:
field_grouping = self._null_group_fields()
except:
log(
f"Auto field grouping failed. Tried using tools.suffolklitlab.org api key {get_config('assembly line',{}).get('tools.suffolklitlab.org api key', None)}"
)
field_grouping = self._null_group_fields()
self.field_grouping = field_grouping
self.questions.auto_gather = False
for group in field_grouping:
new_screen = self.questions.appendObject()
Expand Down
3 changes: 1 addition & 2 deletions docassemble/ALWeaver/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
docassemble.base>=1.3
docassemble.webapp
docassemble.base
more_itertools
pyyaml
docx2python
Expand Down

0 comments on commit a825424

Please sign in to comment.