enricorampazzo commited on
Commit
5d399c2
·
1 Parent(s): d92c06c

now it should work

Browse files
Files changed (2) hide show
  1. app.py +7 -5
  2. form/form.py +10 -6
app.py CHANGED
@@ -69,17 +69,19 @@ def use_streamlit():
69
 
70
  form_filename = f"{st.session_state['session_id']}_form.pdf"
71
  st.session_state["form_filename"] = form_filename
72
- form_data = build_form_data_from_answers(st.session_state["answers"], categories,
73
  st.session_state.get("signature"))
74
- write_pdf_form(form_data, Path(form_filename))
 
75
  st.session_state["step"] = "form_created"
76
  if st.session_state.get("step") == "form_created":
77
- with open(Path(st.session_state["form_filename"]), "rb") as form:
78
- st.download_button("download form", form.read(), mime="application/pdf")
79
  start_over_button = st.button("Start over")
80
  if start_over_button:
81
  del st.session_state["step"]
82
- os.unlink(st.session_state["form_filename"])
 
83
 
84
 
85
  use_streamlit()
 
69
 
70
  form_filename = f"{st.session_state['session_id']}_form.pdf"
71
  st.session_state["form_filename"] = form_filename
72
+ form_data, filename = build_form_data_from_answers(st.session_state["answers"], categories,
73
  st.session_state.get("signature"))
74
+ st.session_state["pdf_form"] = write_pdf_form(form_data)
75
+ st.session_state["pdf_form_filename"] = filename
76
  st.session_state["step"] = "form_created"
77
  if st.session_state.get("step") == "form_created":
78
+ st.download_button("download form", st.session_state["pdf_form"],
79
+ file_name=st.session_state["pdf_form_filename"], mime="application/pdf")
80
  start_over_button = st.button("Start over")
81
  if start_over_button:
82
  del st.session_state["step"]
83
+ del st.session_state["pdf_form"]
84
+ del st.session_state["pdf_form_filename"]
85
 
86
 
87
  use_streamlit()
form/form.py CHANGED
@@ -1,12 +1,14 @@
 
1
  from pathlib import Path
2
- from typing import TypedDict
3
  from PyPDFForm import PdfWrapper
4
 
5
  from utils.date_utils import get_today_date_as_dd_mm_yyyy
6
  from utils.parsing_utils import find_and_parse_date, find_and_parse_phone_number
7
 
8
 
9
- def build_form_data_from_answers(answers: dict[int, str], categories: list[str], signature: str | None = None):
 
10
  form_data = {}
11
  for category in categories:
12
  form_data[form_fields_map[category]] = True
@@ -27,15 +29,17 @@ def build_form_data_from_answers(answers: dict[int, str], categories: list[str],
27
  form_data[form_fields_map["signature_date"]] = get_today_date_as_dd_mm_yyyy()
28
  if signature:
29
  form_data[form_fields_map["signature"]] = signature
30
- return form_data
 
 
31
 
32
- def write_pdf_form(form_data: dict[str, str | bool | int], output_path:Path):
 
33
  base_form_path = Path(__file__).parent
34
  base_form_path = base_form_path.joinpath("form.pdf")
35
  with open(base_form_path, "rb") as blank_form:
36
  filled_form = PdfWrapper(blank_form).fill(form_data)
37
- with open(output_path, "wb+") as filled_form_file:
38
- filled_form_file.write(filled_form.read())
39
 
40
  class FormFields(TypedDict, total=True):
41
  start_date: str
 
1
+ from io import BytesIO
2
  from pathlib import Path
3
+ from typing import TypedDict, Tuple
4
  from PyPDFForm import PdfWrapper
5
 
6
  from utils.date_utils import get_today_date_as_dd_mm_yyyy
7
  from utils.parsing_utils import find_and_parse_date, find_and_parse_phone_number
8
 
9
 
10
+ def build_form_data_from_answers(answers: dict[int, str], categories: list[str], signature: str | None = None) -> (
11
+ Tuple)[dict[str, str], str]:
12
  form_data = {}
13
  for category in categories:
14
  form_data[form_fields_map[category]] = True
 
29
  form_data[form_fields_map["signature_date"]] = get_today_date_as_dd_mm_yyyy()
30
  if signature:
31
  form_data[form_fields_map["signature"]] = signature
32
+ return form_data, (f"work_permit_request"
33
+ f"_{'-'.join(categories)}_{answers[9]}_"
34
+ f"{form_data[form_fields_map['start_date']].replace('/','-')}.pdf")
35
 
36
+
37
+ def write_pdf_form(form_data: dict[str, str | bool | int]) -> bytes:
38
  base_form_path = Path(__file__).parent
39
  base_form_path = base_form_path.joinpath("form.pdf")
40
  with open(base_form_path, "rb") as blank_form:
41
  filled_form = PdfWrapper(blank_form).fill(form_data)
42
+ return filled_form.read()
 
43
 
44
  class FormFields(TypedDict, total=True):
45
  start_date: str