Skip to content

Commit 3f7a8f4

Browse files
author
mathis
committed
➕ API: page break system
1 parent 4b8f452 commit 3f7a8f4

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

API/utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,29 @@ def fill_file(directory: str, file: str, json, error_caught=False) -> Union[tupl
213213

214214
for elem in json:
215215

216-
if (type(elem) != dict or not elem.get("name") or not elem["name"] or type(elem["name"]) != str or
217-
elem.get("variables") is None or len(elem) > 2):
216+
length = len(elem)
217+
is_name_present = type(elem.get("name")) is str
218+
is_variables_present = type(elem.get("variables")) is dict
219+
is_page_break_present = type(elem.get("page_break")) is bool
220+
221+
if (
222+
not is_name_present
223+
or not is_variables_present
224+
or ((length > 2 and not is_page_break_present) or (length > 3 and is_page_break_present))
225+
):
218226
return error_sim(
219227
"JsonSyntaxError",
220228
'api_invalid_instance_syntax',
221229
"Each instance of the array in the json should be an object containing only 'name' - "
222-
"a non-empty string, and 'variables' - a non-empty object"), 415
230+
"a non-empty string, 'variables' - a non-empty object, and, optionally, 'page_break' - "
231+
"a boolean."), 415
223232

224233
try:
225234
json_variables = ot.convert_to_datas_template(elem["variables"])
226235
temp.search_error(json_variables)
227236
temp.fill(elem["variables"])
237+
if elem.get('page_break', False):
238+
temp.page_break()
228239
exports.append(temp.export("exports/" + elem["name"], should_replace=(
229240
True if len(json) == 1 else False)))
230241
except Exception as e:

lotemplate/classes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from com.sun.star.connection import NoConnectException
2525
from com.sun.star.uno import RuntimeException
2626
from com.sun.star.awt import Size
27+
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
28+
from com.sun.star.style.BreakType import PAGE_AFTER
2729

2830
from . import errors
2931
from .utils import *
@@ -523,3 +525,19 @@ def close(self) -> None:
523525
os.remove(self.file_dir + "/.~lock." + self.file_name + "#")
524526
except FileNotFoundError:
525527
pass
528+
529+
def page_break(self) -> None:
530+
"""
531+
Add a page break to the document
532+
533+
:return: None
534+
"""
535+
536+
if not self.new:
537+
return
538+
539+
cursor = self.new.Text.createTextCursor()
540+
cursor.gotoEnd(False)
541+
cursor.collapseToEnd()
542+
cursor.BreakType = PAGE_AFTER
543+
self.new.Text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)

0 commit comments

Comments
 (0)