move generating logic to dialog

This commit is contained in:
WorldTeacher
2024-06-27 13:18:56 +02:00
parent a547b6aa21
commit 616843c906

View File

@@ -2,16 +2,21 @@ from .dialog_sources.Ui_elsa_add_table_entry import Ui_Dialog
from src.logic.webrequest import WebRequest, BibTextTransformer from src.logic.webrequest import WebRequest, BibTextTransformer
from src import Icon from src import Icon
from PyQt6 import QtWidgets from PyQt6 import QtWidgets
from src.transformers.transformers import DictToTable
from src.logic.zotero import ZoteroController
from icecream import ic
zot = ZoteroController()
dtt = DictToTable()
class ElsaAddEntry(QtWidgets.QDialog, Ui_Dialog): class ElsaAddEntry(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None, data=None): def __init__(self, parent=None, data=None):
super(ElsaAddEntry, self).__init__(parent) super(ElsaAddEntry, self).__init__(parent)
self.setupUi(self) self.setupUi(self)
self.setWindowTitle("Eintrag hinzufügen") self.setWindowTitle("Eintrag zitieren")
self.buttonBox.button( self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Ok QtWidgets.QDialogButtonBox.StandardButton.Ok
).clicked.connect(self.send) ).clicked.connect(self.accept)
self.buttonBox.button( self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Cancel QtWidgets.QDialogButtonBox.StandardButton.Cancel
).clicked.connect(self.close) ).clicked.connect(self.close)
@@ -23,14 +28,30 @@ class ElsaAddEntry(QtWidgets.QDialog, Ui_Dialog):
self.btn_mono.clicked.connect(self.stack) self.btn_mono.clicked.connect(self.stack)
self.btn_zs.clicked.connect(self.stack) self.btn_zs.clicked.connect(self.stack)
self.btn_hg.clicked.connect(self.stack) self.btn_hg.clicked.connect(self.stack)
self.make_quote.clicked.connect(self.display_data)
self.copy_filename.clicked.connect(
lambda: self.copy_to_clipboard(self.filename_edit)
)
self.copy_ilias_filename.clicked.connect(
lambda: self.copy_to_clipboard(self.ilias_filename)
)
self.copy_qoute.clicked.connect(
lambda: self.copy_to_clipboard(self.file_desc_edit)
)
self.setWindowIcon(Icon("edit").icon) self.setWindowIcon(Icon("edit").icon)
self.stackedWidget.setEnabled(False) self.stackedWidget.setEnabled(False)
self.btn_hg.setChecked(False) self.btn_hg.setChecked(False)
self.data = None self.data = None
def copy_to_clipboard(self, field):
clipboard = QtWidgets.QApplication.clipboard()
# fields are textedits
clipboard.setText(field.toPlainText())
def discard(self): def discard(self):
for line in self.findChildren(QtWidgets.QLineEdit): for line in self.findChildren(QtWidgets.QLineEdit):
line.clear() line.clear()
for line in self.findChildren(QtWidgets.QTextEdit):
line.clear()
def stack(self): def stack(self):
self.stackedWidget.setEnabled(True) self.stackedWidget.setEnabled(True)
@@ -44,8 +65,7 @@ class ElsaAddEntry(QtWidgets.QDialog, Ui_Dialog):
self.stackedWidget.setCurrentIndex(2) self.stackedWidget.setCurrentIndex(2)
self.mode = "zs" self.mode = "zs"
def send(self): def display_data(self):
# get all fields from the dialog
fields = self.findChildren(QtWidgets.QLineEdit) fields = self.findChildren(QtWidgets.QLineEdit)
# remove all fields from the list if they do not start with the mode and _ # remove all fields from the list if they do not start with the mode and _
fields = [field for field in fields if field.objectName().startswith(self.mode)] fields = [field for field in fields if field.objectName().startswith(self.mode)]
@@ -57,8 +77,70 @@ class ElsaAddEntry(QtWidgets.QDialog, Ui_Dialog):
fdata = field.text() fdata = field.text()
data[fname] = fdata data[fname] = fdata
data["mode"] = self.mode data["mode"] = self.mode
self.data = data table = dtt.transform(data)
self.accept()
if data is None:
return
res_key = None
filename = None
ilias_name = None
if table["type"] == "zs":
book = zot.createBook(table["isbn"])
res_key = zot.createJournalArticle(book, table)
ic(book)
a_lastname = table["section_author"].split(";")[0].strip().split(",")[0]
a_firstname = table["section_author"].split(";")[0].strip().split(",")[1]
author = f"{a_lastname}, {a_firstname[0]}"
year = book.date.strip() if book.date != "" else table["year"]
chapter = table["chapter_title"].strip()
page = table["pages"].strip()
if len(book.creators) > 1:
a_lastname = f"{a_lastname}_etal"
filename = f"{a_lastname}_{year}_{chapter}_{page}_z.pdf"
ilias_name = f"{author}. ({year}). {chapter}. S. {page}."
elif table["type"] == "book":
book = zot.createBook(table["signature"])
res_key = zot.createBookSection(book, table)
a_lastname = book.creators[0]["lastName"].strip()
a_firstname = book.creators[0]["firstName"].strip()
author = f"{a_lastname}, {a_firstname[0]}"
title = book.title.strip()
year = book.date.strip()
page = table["pages"].strip()
if len(book.creators) > 1:
a_lastname = f"{a_lastname}_etal"
filename = f"{a_lastname}_{year}_{title}_{page}.pdf"
ilias_name = f"{author}. ({year}). {title}. S. {page}."
elif table["type"] == "hg":
book = zot.createBook(table["signature"])
res_key = zot.createHGSection(book, table)
a_lastname = table["section_author"].split(";")[0].strip().split(",")[0]
a_firstname = (
table["section_author"].split(";")[0].strip().split(",")[1].strip()[0]
)
author = f"{a_lastname}, {a_firstname[0]}"
editor = table["work_author"].split(";")[0].strip().split(",")[0]
title = table["chapter_title"].strip()
year = book.date.strip()
page = table["pages"].strip()
s_authors = table["section_author"].split(";")
s_editors = table["work_author"].split(";")
if len(s_authors) > 1:
a_lastname = f"{a_lastname}_etal"
author = f"{author} et al."
if len(s_editors) > 1:
editor = f"{editor}_etal"
filename = f"{a_lastname}_in_{editor}_{year}_{title}_{page}.pdf"
ilias_name = f"{author}. ({year}). {title}. S. {page}."
citation = None
if res_key:
citation = zot.get_citation(res_key)
zot.deleteItem(res_key)
self.file_desc_edit.setText(citation)
self.filename_edit.setText(filename.lower())
self.ilias_filename.setText(ilias_name)
self.stackedWidget.setCurrentIndex(3)
def search(self): def search(self):
param = self.searchIdent.text() param = self.searchIdent.text()