Skip to content

Commit

Permalink
➕ API: page break system
Browse files Browse the repository at this point in the history
  • Loading branch information
mathis committed Mar 31, 2023
1 parent 4b8f452 commit 3f7a8f4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions API/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,29 @@ def fill_file(directory: str, file: str, json, error_caught=False) -> Union[tupl

for elem in json:

if (type(elem) != dict or not elem.get("name") or not elem["name"] or type(elem["name"]) != str or
elem.get("variables") is None or len(elem) > 2):
length = len(elem)
is_name_present = type(elem.get("name")) is str
is_variables_present = type(elem.get("variables")) is dict
is_page_break_present = type(elem.get("page_break")) is bool

if (
not is_name_present
or not is_variables_present
or ((length > 2 and not is_page_break_present) or (length > 3 and is_page_break_present))
):
return error_sim(
"JsonSyntaxError",
'api_invalid_instance_syntax',
"Each instance of the array in the json should be an object containing only 'name' - "
"a non-empty string, and 'variables' - a non-empty object"), 415
"a non-empty string, 'variables' - a non-empty object, and, optionally, 'page_break' - "
"a boolean."), 415

try:
json_variables = ot.convert_to_datas_template(elem["variables"])
temp.search_error(json_variables)
temp.fill(elem["variables"])
if elem.get('page_break', False):
temp.page_break()
exports.append(temp.export("exports/" + elem["name"], should_replace=(
True if len(json) == 1 else False)))
except Exception as e:
Expand Down
18 changes: 18 additions & 0 deletions lotemplate/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from com.sun.star.connection import NoConnectException
from com.sun.star.uno import RuntimeException
from com.sun.star.awt import Size
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.style.BreakType import PAGE_AFTER

from . import errors
from .utils import *
Expand Down Expand Up @@ -523,3 +525,19 @@ def close(self) -> None:
os.remove(self.file_dir + "/.~lock." + self.file_name + "#")
except FileNotFoundError:
pass

def page_break(self) -> None:
"""
Add a page break to the document
:return: None
"""

if not self.new:
return

cursor = self.new.Text.createTextCursor()
cursor.gotoEnd(False)
cursor.collapseToEnd()
cursor.BreakType = PAGE_AFTER
self.new.Text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)

0 comments on commit 3f7a8f4

Please # to comment.