From 880dfe9a16f719af1d0f8ae0e1c94d03dfc0c3ee Mon Sep 17 00:00:00 2001 From: Agnibho Mondal Date: Thu, 2 Nov 2023 21:19:58 +0530 Subject: [PATCH] Option to preserve template --- filehandler.py | 13 +++++++++++-- window.py | 22 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/filehandler.py b/filehandler.py index 5af5538..99e0a55 100644 --- a/filehandler.py +++ b/filehandler.py @@ -42,14 +42,20 @@ class FileHandler(): items.append(f) return(items) - def save(self, file=None): + def save(self, file=None, change_template=True): if file is not None: self.file=file with open(os.path.join(self.directory.name, "meta.json"), "w") as f: f.write(json.dumps(self.meta)) template=os.path.join(self.directory.name, "template") os.makedirs(template, exist_ok=True) - shutil.copytree(config["template"], template, dirs_exist_ok=True) + if(change_template): + shutil.copytree(config["template"], template, dirs_exist_ok=True) + else: + try: + os.remove(os.path.join(template, "output.html")) + except: + pass with ZipFile(self.file, "w", strict_timestamps=False) as target: for f in glob.glob(os.path.join(self.directory.name, "**" ,"*"), recursive=True): @@ -94,5 +100,8 @@ class FileHandler(): except Exception as e: print(e) + def has_template(self): + return(os.path.exists(os.path.join(self.directory.name, "template", "index.html"))) + def is_signed(self): return(os.path.exists(os.path.join(self.directory.name, "certificate.pem")) and (os.path.exists(os.path.join(self.directory.name, "signature.p7m")))) diff --git a/window.py b/window.py index d85d9ee..770a9a4 100644 --- a/window.py +++ b/window.py @@ -43,6 +43,7 @@ class MainWindow(QMainWindow): def cmd_new(self): if(self.confirm_close()): self.new_doc() + self.load_interface_from_instance() def cmd_open(self, file=None): if(self.confirm_close()): @@ -56,6 +57,7 @@ class MainWindow(QMainWindow): self.prescription.read_from(os.path.join(self.current_file.directory.name,"prescription.json")) self.plugin.open(self.prescription) self.load_interface_from_instance() + self.save_state=md5(self.prescription.get_json().encode()).hexdigest() self.load_attachment(self.current_file.list()) self.unchanged_state=True @@ -75,6 +77,11 @@ class MainWindow(QMainWindow): def cmd_save(self, save_as=False): self.update_instance() self.plugin.save(self.prescription) + if(self.input_template.currentText()!=""): + change_template=True + template=self.input_template.currentText() + else: + change_template=False self.load_interface_from_instance() suggest=self.prescription.id if(self.prescription.id) else self.prescription.name suggest=os.path.abspath(os.path.join(config["document_directory"], suggest)+".mpaz") @@ -88,8 +95,9 @@ class MainWindow(QMainWindow): for i in range(self.input_attachment.count()): self.current_file.copy(self.input_attachment.item(i).text()) self.prescription.write_to(os.path.join(self.current_file.directory.name, "prescription.json")) - config["template"]=os.path.join(config["template_directory"], self.input_template.currentText()) - self.current_file.save() + if change_template: + config["template"]=os.path.join(config["template_directory"], template) + self.current_file.save(change_template=change_template) self.unchanged_state=False self.load_interface_from_instance() self.save_state=md5(self.prescription.get_json().encode()).hexdigest() @@ -279,6 +287,7 @@ class MainWindow(QMainWindow): self.input_report_preset.setCurrentIndex(-1) if config["preset_newline"]: self.input_report.insertPlainText("\n") + def insert_preset_advice(self): try: self.input_advice.insertPlainText(self.preset_advice.data[self.input_advice_preset.currentText()]) @@ -289,7 +298,6 @@ class MainWindow(QMainWindow): if config["preset_newline"]: self.input_advice.insertPlainText("\n") - def insert_preset_investigation(self): try: self.input_investigation.insertPlainText(self.preset_investigation.data[self.input_investigation_preset.currentText()]) @@ -357,6 +365,12 @@ class MainWindow(QMainWindow): print(e) def load_interface_from_instance(self): + if(self.current_file.has_template()): + if(self.input_template.findText("")==-1): + self.input_template.addItem("") + self.input_template.setCurrentText("") + else: + self.input_template.removeItem(self.input_template.findText("")) self.load_interface( file=self.prescription.file, date=self.prescription.date, @@ -579,6 +593,8 @@ class MainWindow(QMainWindow): toolbar.addAction(action_refresh2) toolbar.addAction(action_render2) toolbar.addSeparator() + label_template=QLabel("Template:") + toolbar.addWidget(label_template) self.input_template=QComboBox(self) templates=os.listdir(config["template_directory"]) try: -- 2.39.5