Compare commits
22 Commits
alpha6
...
b61c52972d
| Author | SHA1 | Date | |
|---|---|---|---|
| b61c52972d | |||
| c80a587cbd | |||
| 0d114a325d | |||
| 9532eb7247 | |||
| 7e9d5bec9b | |||
| 6407cf4229 | |||
| 9ad45ed63f | |||
| 3224c1c2aa | |||
| ca88c4d229 | |||
| 12d0d7034f | |||
| 2d840d6ad4 | |||
| 0ae2c7e559 | |||
| 96995f1e80 | |||
| ec130fcee9 | |||
| 95ed06df45 | |||
| 9ea44933a6 | |||
| dc27197129 | |||
| bbdbaadb92 | |||
| ad66988e09 | |||
| 3fd0147158 | |||
| aec7770e74 | |||
| d16aefe20f |
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
if [ $VIRTUAL_ENV=="" ]
|
if [ "$VIRTUAL_ENV" == "" ]
|
||||||
then
|
then
|
||||||
source venv/bin/activate
|
source venv/bin/activate
|
||||||
fi
|
fi
|
||||||
@@ -11,8 +11,8 @@ if [ "$new_version" != "" ]
|
|||||||
then
|
then
|
||||||
echo "$new_version" | tee version.txt
|
echo "$new_version" | tee version.txt
|
||||||
fi
|
fi
|
||||||
sed -i "s/VERSION = '[0-9]\.[0-9]\w'/VERSION = '$(cat version.txt)'/g" src/brovski-adress-etiketten-verwaltung.py
|
sed -i "s/VERSION = '[0-9]\.[0-9].[0-9]\w'/VERSION = '$(cat version.txt)'/g" src/brovski-adress-etiketten-verwaltung.py
|
||||||
sed -i "s/Version: [0-9]\.[0-9]\w/Version: $(cat version.txt)/g" deb-package/brovski-adressetiketten/DEBIAN/control
|
sed -i "s/Version: [0-9]\.[0-9].[0-9]\w/Version: $(cat version.txt)/g" deb-package/brovski-adressetiketten/DEBIAN/control
|
||||||
|
|
||||||
pyinstaller --clean --onefile src/brovski-adress-etiketten-verwaltung.py
|
pyinstaller --clean --onefile src/brovski-adress-etiketten-verwaltung.py
|
||||||
cp dist/brovski-adress-etiketten-verwaltung deb-package/brovski-adressetiketten/usr/local/bin/brovski-adressetiketten
|
cp dist/brovski-adress-etiketten-verwaltung deb-package/brovski-adressetiketten/usr/local/bin/brovski-adressetiketten
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
Package: brovski-adressetiketten
|
Package: brovski-adressetiketten
|
||||||
Version: 0.6a
|
Version: 0.1.1b
|
||||||
Maintainer: Ovski
|
Maintainer: Ovski
|
||||||
Architecture: all
|
Architecture: all
|
||||||
Description: manage csv files for glables address labels
|
Description: manage csv files for glables address labels
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ import tkinter as tk
|
|||||||
from configparser import NoSectionError, NoOptionError
|
from configparser import NoSectionError, NoOptionError
|
||||||
from tkinter import messagebox
|
from tkinter import messagebox
|
||||||
from tkinter import ttk
|
from tkinter import ttk
|
||||||
import json
|
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
from windows import SettingsWindow, EditRecord, Window, show_error
|
from model import Model
|
||||||
|
from src.connector import JSONConnector
|
||||||
|
from windows import SettingsWindow, EditRecord, show_error
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
@@ -18,7 +19,7 @@ class Application:
|
|||||||
y_offset = 200
|
y_offset = 200
|
||||||
width = 1050
|
width = 1050
|
||||||
height = 700
|
height = 700
|
||||||
VERSION = '0.6a'
|
VERSION = '0.1.1b'
|
||||||
title = f"Brovski Adress-Etiketten Verwaltung {VERSION}"
|
title = f"Brovski Adress-Etiketten Verwaltung {VERSION}"
|
||||||
|
|
||||||
self.root = tk.Tk(className="BrovskiAdressEtiketten")
|
self.root = tk.Tk(className="BrovskiAdressEtiketten")
|
||||||
@@ -32,16 +33,24 @@ class Application:
|
|||||||
self.address_list = []
|
self.address_list = []
|
||||||
self.current_record: int | None = None
|
self.current_record: int | None = None
|
||||||
self.sort_order = False
|
self.sort_order = False
|
||||||
|
self.last_sort_field = "#3"
|
||||||
|
self.filter_active = tk.BooleanVar(value=False)
|
||||||
|
|
||||||
|
# model connector
|
||||||
|
self.model = Model(JSONConnector(self.config))
|
||||||
|
|
||||||
# init paths to json and csv file
|
# init paths to json and csv file
|
||||||
self.json_file_name = "brovski-adress-etiketten-verwaltung.json"
|
self.json_file_name = "brovski-adress-etiketten-verwaltung.json"
|
||||||
self.csv_file_name = "brovski-adress-etiketten.csv"
|
self.csv_file_name = "brovski-adress-etiketten.csv"
|
||||||
self.json_path = ""
|
|
||||||
self.csv_path = ""
|
self.csv_path = ""
|
||||||
self.load_config()
|
self.load_config()
|
||||||
self.json_file = os.path.join(self.json_path, self.json_file_name)
|
|
||||||
self.csv_file = os.path.join(self.csv_path, self.csv_file_name)
|
self.csv_file = os.path.join(self.csv_path, self.csv_file_name)
|
||||||
|
|
||||||
|
# status bar content
|
||||||
|
self.statusbar = tk.StringVar()
|
||||||
|
self.length_address_list = None
|
||||||
|
self.length_address_list_active = None
|
||||||
|
|
||||||
# leave application if settings are bad
|
# leave application if settings are bad
|
||||||
if not self.config_good:
|
if not self.config_good:
|
||||||
show_error(message_title="Fehler Konfiguration",
|
show_error(message_title="Fehler Konfiguration",
|
||||||
@@ -50,21 +59,28 @@ class Application:
|
|||||||
)
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
# frames
|
||||||
top_frame = tk.Frame(self.root)
|
top_frame = tk.Frame(self.root)
|
||||||
top_frame.pack(side=tk.TOP, fill=tk.X)
|
top_frame.pack(side=tk.TOP, fill=tk.X)
|
||||||
|
data_frame = tk.Frame(self.root, bg="teal")
|
||||||
|
data_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
||||||
|
bottom_frame = tk.Frame(self.root)
|
||||||
|
bottom_frame.pack(side=tk.BOTTOM, fill=tk.X)
|
||||||
|
|
||||||
|
# top buttons
|
||||||
button_width = 8
|
button_width = 8
|
||||||
tk.Button(top_frame, text="Insert", command=self.insert_record, width=button_width).pack(side=tk.LEFT)
|
tk.Button(top_frame, text="Insert", command=self.insert_record, width=button_width).pack(side=tk.LEFT)
|
||||||
tk.Button(top_frame, text="Delete", command=self.delete_record, width=button_width).pack(side=tk.LEFT)
|
tk.Button(top_frame, text="Delete", command=self.delete_record, width=button_width).pack(side=tk.LEFT)
|
||||||
tk.Button(top_frame, text="Export CSV", command=self.export_csv, width=button_width).pack(side=tk.LEFT)
|
tk.Button(top_frame, text="Export CSV", command=self.export_csv, width=button_width).pack(side=tk.LEFT)
|
||||||
tk.Button(top_frame, text="Toggle Aktiv", command=self.toggle_active, width=button_width).pack(side=tk.LEFT)
|
tk.Button(top_frame, text="Toggle Aktiv", command=self.toggle_active, width=button_width).pack(side=tk.LEFT)
|
||||||
|
tk.Checkbutton(top_frame, text="Filter aktiv", variable=self.filter_active, command=self.populate_table).pack(
|
||||||
|
side=tk.LEFT)
|
||||||
tk.Button(top_frame, text="Quit", command=self.on_close, width=button_width).pack(side=tk.RIGHT)
|
tk.Button(top_frame, text="Quit", command=self.on_close, width=button_width).pack(side=tk.RIGHT)
|
||||||
tk.Button(top_frame, text="Settings", command=self.show_settings, width=button_width).pack(side=tk.RIGHT)
|
tk.Button(top_frame, text="Settings", command=self.show_settings, width=button_width).pack(side=tk.RIGHT)
|
||||||
|
|
||||||
data_frame = tk.Frame(self.root, bg="teal")
|
# table content
|
||||||
data_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
|
||||||
|
|
||||||
scrollbar = ttk.Scrollbar(data_frame, orient=tk.VERTICAL)
|
scrollbar = ttk.Scrollbar(data_frame, orient=tk.VERTICAL)
|
||||||
self.table = ttk.Treeview(data_frame, yscrollcommand=scrollbar.set, columns=("0", "1", "2", "3", "4"),
|
self.table = ttk.Treeview(data_frame, yscrollcommand=scrollbar.set, columns=("0", "1", "2", "3", "4", "5"),
|
||||||
show="headings")
|
show="headings")
|
||||||
scrollbar.config(command=self.table.yview)
|
scrollbar.config(command=self.table.yview)
|
||||||
self.table.heading('0', text="Aktiv")
|
self.table.heading('0', text="Aktiv")
|
||||||
@@ -73,22 +89,27 @@ class Application:
|
|||||||
self.table.heading('2', text="Name")
|
self.table.heading('2', text="Name")
|
||||||
self.table.heading('3', text="Strasse")
|
self.table.heading('3', text="Strasse")
|
||||||
self.table.heading('4', text="Plz/Ort")
|
self.table.heading('4', text="Plz/Ort")
|
||||||
|
self.table.heading('5', text="Anzahl")
|
||||||
self.table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
|
self.table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
|
||||||
scrollbar.pack(side=tk.LEFT, fill=tk.Y)
|
scrollbar.pack(side=tk.LEFT, fill=tk.Y)
|
||||||
|
|
||||||
self.table.bind("<ButtonRelease-1>", self.mouse_click)
|
self.table.bind("<ButtonRelease-1>", self.mouse_click)
|
||||||
self.table.bind("<Return>", self.mouse_click_double)
|
self.table.bind("<Return>", self.enter_button)
|
||||||
self.table.bind("<Double-1>", self.mouse_click_double)
|
self.table.bind("<Double-1>", self.mouse_click_double)
|
||||||
|
self.root.bind("<Up>", self.focus_table)
|
||||||
|
self.root.bind("<Down>", self.focus_table)
|
||||||
|
|
||||||
self._load_json_file()
|
# bottom status bar
|
||||||
|
tk.Label(bottom_frame, textvariable=self.statusbar).pack(side=tk.LEFT)
|
||||||
|
|
||||||
|
self.first_sort_after_start()
|
||||||
|
|
||||||
self.root.mainloop()
|
self.root.mainloop()
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self):
|
||||||
try:
|
try:
|
||||||
self.json_path = self.config.get("json", "path")
|
|
||||||
self.csv_path = self.config.get("csv", "path")
|
self.csv_path = self.config.get("csv", "path")
|
||||||
if self.json_path == "" or self.csv_path == "":
|
if self.csv_path == "":
|
||||||
raise ValueError("Empty JSON or CSV path")
|
raise ValueError("Empty JSON or CSV path")
|
||||||
self.config_good = True
|
self.config_good = True
|
||||||
except NoSectionError:
|
except NoSectionError:
|
||||||
@@ -114,55 +135,56 @@ class Application:
|
|||||||
settings.wait_window()
|
settings.wait_window()
|
||||||
|
|
||||||
def insert_record(self):
|
def insert_record(self):
|
||||||
values = [
|
values = {
|
||||||
"x",
|
"aktiv": "x",
|
||||||
"Firma",
|
"firma": "Firma",
|
||||||
"Name",
|
"name": "Name",
|
||||||
"Strasse",
|
"strasse": "Strasse",
|
||||||
"Plz/Ort",
|
"plzort": "Plz/Ort",
|
||||||
]
|
"anzahl": "Anzahl"
|
||||||
last_iid = self.table.get_children()[-1]
|
}
|
||||||
next_iid = int(last_iid) + 1
|
self.model.create_new(values)
|
||||||
self.table.insert('', 'end', iid=next_iid, values=values)
|
self.populate_table()
|
||||||
self._save_json_file()
|
|
||||||
self.deselect_tree()
|
self.deselect_tree()
|
||||||
|
|
||||||
def delete_record(self):
|
def delete_record(self):
|
||||||
if self.current_record is None:
|
if self.current_record is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if len(self.table.selection()) > 1:
|
||||||
|
show_error(message_title="Mehrere Adressen ausgewählt",
|
||||||
|
message="Es können nur einzelne Adressen gelöscht werden",
|
||||||
|
parent=self.root)
|
||||||
|
return
|
||||||
|
|
||||||
if messagebox.askyesno(
|
if messagebox.askyesno(
|
||||||
"Eintrag löschen?",
|
"Eintrag löschen?",
|
||||||
"Willst du diesen Eintrag wirklich löschen?\nDies kann nicht rückgängig gemacht werden"):
|
"Willst du diesen Eintrag wirklich löschen?\nDies kann nicht rückgängig gemacht werden"):
|
||||||
self.table.delete(self.current_record)
|
self.model.delete_by_id(self.current_record)
|
||||||
|
|
||||||
self.deselect_tree()
|
self.deselect_tree()
|
||||||
self._save_json_file()
|
self.populate_table()
|
||||||
|
|
||||||
def update_record(self, data: list):
|
def update_record(self, record: dict):
|
||||||
if self.current_record is None:
|
if self.current_record is None:
|
||||||
return
|
return
|
||||||
values = {}
|
self.model.update_record(record)
|
||||||
for idx, value in enumerate(data):
|
self.populate_table()
|
||||||
values[str(idx)] = value.get()
|
|
||||||
|
|
||||||
for key, value in values.items():
|
|
||||||
self.table.set(self.current_record, key, value)
|
|
||||||
|
|
||||||
self._save_json_file()
|
|
||||||
|
|
||||||
def toggle_active(self):
|
def toggle_active(self):
|
||||||
items = self.table.selection()
|
selection = self.table.selection()
|
||||||
if len(items) == 0:
|
if len(selection) == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
for record_id in items:
|
item_list = [int(x) for x in selection]
|
||||||
values = self.table.item(record_id, "values")
|
for address in self.address_list:
|
||||||
active = values[0]
|
record_id = address.get("record_id")
|
||||||
new_active = "x" if active == "" else ""
|
if record_id in item_list:
|
||||||
self.table.set(record_id, "0", new_active)
|
address["aktiv"] = "x" if address["aktiv"] == "" else ""
|
||||||
|
self.model.update_record(address)
|
||||||
|
self.table.set(record_id, "0", address["aktiv"])
|
||||||
|
|
||||||
self._save_json_file()
|
self.update_status_bar()
|
||||||
|
|
||||||
def deselect_tree(self):
|
def deselect_tree(self):
|
||||||
while len(self.table.selection()) > 0:
|
while len(self.table.selection()) > 0:
|
||||||
@@ -170,7 +192,11 @@ class Application:
|
|||||||
self.current_record = None
|
self.current_record = None
|
||||||
|
|
||||||
def mouse_click(self, event):
|
def mouse_click(self, event):
|
||||||
self.current_record = self.table.focus()
|
id_string = self.table.focus()
|
||||||
|
if id_string is not None and id_string != "":
|
||||||
|
self.current_record = int(self.table.focus())
|
||||||
|
else:
|
||||||
|
self.current_record = None
|
||||||
region = self.table.identify("region", event.x, event.y)
|
region = self.table.identify("region", event.x, event.y)
|
||||||
match region:
|
match region:
|
||||||
case "heading":
|
case "heading":
|
||||||
@@ -180,84 +206,89 @@ class Application:
|
|||||||
region = self.table.identify("region", event.x, event.y)
|
region = self.table.identify("region", event.x, event.y)
|
||||||
match region:
|
match region:
|
||||||
case "cell":
|
case "cell":
|
||||||
self.click_on_cell()
|
self.edit_selected_record()
|
||||||
|
|
||||||
|
def enter_button(self, event):
|
||||||
|
self.edit_selected_record()
|
||||||
|
|
||||||
def click_on_header(self, event):
|
def click_on_header(self, event):
|
||||||
column = self.table.identify_column(event.x)
|
column = self.table.identify_column(event.x)
|
||||||
|
if self.last_sort_field == column:
|
||||||
|
self.sort_order = False if self.sort_order else True
|
||||||
|
else:
|
||||||
|
self.sort_order = False
|
||||||
|
self.last_sort_field = column
|
||||||
|
|
||||||
match column:
|
match column:
|
||||||
case "#1":
|
case "#1":
|
||||||
self.address_list.sort(key=lambda x: x[int(column[-1]) - 1], reverse=self.sort_order)
|
field = "aktiv"
|
||||||
self.populate_table()
|
|
||||||
case "#2":
|
case "#2":
|
||||||
self.address_list.sort(key=lambda x: x[int(column[-1]) - 1], reverse=self.sort_order)
|
field = "firma"
|
||||||
self.populate_table()
|
|
||||||
case "#3":
|
case "#3":
|
||||||
self.address_list.sort(key=lambda x: x[int(column[-1]) - 1], reverse=self.sort_order)
|
field = "name"
|
||||||
self.populate_table()
|
|
||||||
case "#4":
|
case "#4":
|
||||||
self.address_list.sort(key=lambda x: x[int(column[-1]) - 1], reverse=self.sort_order)
|
field = "strasse"
|
||||||
self.populate_table()
|
|
||||||
case "#5":
|
case "#5":
|
||||||
self.address_list.sort(key=lambda x: x[int(column[-1]) - 1], reverse=self.sort_order)
|
field = "plzort"
|
||||||
self.populate_table()
|
case "#6":
|
||||||
|
field = "anzahl"
|
||||||
case _:
|
case _:
|
||||||
print(column)
|
field = "name"
|
||||||
self.sort_order = not self.sort_order
|
|
||||||
|
|
||||||
def click_on_cell(self):
|
self.address_list = self.model.get_all_sorted_by(field, self.sort_order)
|
||||||
self.current_record = self.table.focus()
|
self.populate_table(reload=False)
|
||||||
values = self.table.item(self.current_record, "values")
|
|
||||||
self.open_window_edit_records(values)
|
|
||||||
|
|
||||||
def _load_json_file(self):
|
def edit_selected_record(self):
|
||||||
try:
|
if self.table.focus() is None or self.table.focus() == "":
|
||||||
with open(self.json_file, "r", encoding="utf-8") as f:
|
return
|
||||||
self.address_list = json.load(f)
|
self.current_record = int(self.table.focus())
|
||||||
self.address_list.sort(key=lambda x: (x[0], x[1]))
|
self.open_window_edit_records(self.current_record)
|
||||||
except FileNotFoundError:
|
|
||||||
show_error(
|
|
||||||
message_title="Datei nicht gefunden",
|
|
||||||
message=f"{self.json_file_name} nicht gefunden, erstelle leere Datei unter {self.json_path}",
|
|
||||||
parent=self.root
|
|
||||||
)
|
|
||||||
self.address_list = [["", "firma", "name", "adresse", "plz/ort"]]
|
|
||||||
with open(self.json_file, "w", encoding="utf-8") as f:
|
|
||||||
json.dump(self.address_list, f)
|
|
||||||
self.populate_table()
|
|
||||||
|
|
||||||
def _save_json_file(self):
|
|
||||||
self.export_table_to_address_list()
|
|
||||||
try:
|
|
||||||
with open(self.json_file, "w", encoding="utf-8") as f:
|
|
||||||
json.dump(self.address_list, f, indent=4, sort_keys=True)
|
|
||||||
except FileNotFoundError:
|
|
||||||
show_error(
|
|
||||||
message_title="Unexpected Error: File not found?!",
|
|
||||||
message=f"{self.json_file_name} not found",
|
|
||||||
parent=self.root
|
|
||||||
)
|
|
||||||
|
|
||||||
def export_csv(self):
|
def export_csv(self):
|
||||||
try:
|
try:
|
||||||
with open(self.csv_file, "w", encoding="utf-8") as f:
|
with open(self.csv_file, "w", encoding="utf-8") as f:
|
||||||
writer = csv.writer(f, delimiter=",")
|
writer = csv.writer(f, delimiter=",")
|
||||||
for address in self.address_list:
|
for address in self.address_list:
|
||||||
if address[0] != "x":
|
if address["aktiv"] != "x":
|
||||||
continue
|
continue
|
||||||
if address[1] == "":
|
for index in range(int(address["anzahl"])):
|
||||||
writer.writerow(address[2:])
|
line = []
|
||||||
else:
|
if address["firma"] != "":
|
||||||
writer.writerow(address[1:])
|
line.append(address["firma"])
|
||||||
|
line.append(address["name"])
|
||||||
|
line.append(address["strasse"])
|
||||||
|
line.append(address["plzort"])
|
||||||
|
writer.writerow(line)
|
||||||
|
# todo: add "absender" to config parameters
|
||||||
|
line = []
|
||||||
|
line.append("Absender:")
|
||||||
|
line.append("Matthias Braun")
|
||||||
|
line.append("Wolfacherweg 1")
|
||||||
|
line.append("5724 Dürrenäsch")
|
||||||
|
writer.writerow(line)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
show_error(message_title="Unexpected error",
|
show_error(message_title="Unexpected error",
|
||||||
message=f"Could not write file {self.csv_file}",
|
message=f"Could not write file {self.csv_file}",
|
||||||
parent=self.root
|
parent=self.root
|
||||||
)
|
)
|
||||||
|
|
||||||
def populate_table(self):
|
def populate_table(self, reload=True):
|
||||||
|
if reload:
|
||||||
|
self.address_list = self.model.get_all()
|
||||||
|
if len(self.address_list) == 0:
|
||||||
|
self.delete_all_table_items()
|
||||||
|
return
|
||||||
self.delete_all_table_items()
|
self.delete_all_table_items()
|
||||||
for index, item in enumerate(self.address_list):
|
for address in self.address_list:
|
||||||
self.table.insert('', 'end', iid=index, values=item)
|
# skip inactive records if filter is true
|
||||||
|
if self.filter_active.get() and address["aktiv"] != "x":
|
||||||
|
continue
|
||||||
|
self.table.insert('', 'end', iid=address["record_id"],
|
||||||
|
values=(address["aktiv"], address["firma"], address["name"], address["strasse"],
|
||||||
|
address["plzort"], address["anzahl"])
|
||||||
|
)
|
||||||
|
self.update_status_bar()
|
||||||
|
|
||||||
def export_table_to_address_list(self):
|
def export_table_to_address_list(self):
|
||||||
self.address_list.clear()
|
self.address_list.clear()
|
||||||
@@ -265,25 +296,50 @@ class Application:
|
|||||||
self.address_list.append([])
|
self.address_list.append([])
|
||||||
for value in self.table.item(child)['values']:
|
for value in self.table.item(child)['values']:
|
||||||
self.address_list[-1].append(value)
|
self.address_list[-1].append(value)
|
||||||
|
self.update_status_bar()
|
||||||
|
|
||||||
def delete_all_table_items(self):
|
def delete_all_table_items(self):
|
||||||
for item in self.table.get_children():
|
for item in self.table.get_children():
|
||||||
self.table.delete(item)
|
self.table.delete(item)
|
||||||
|
|
||||||
def open_window_settings(self):
|
def open_window_settings(self):
|
||||||
window = SettingsWindow(self, self.root)
|
window = SettingsWindow(self, self.root, self.config)
|
||||||
window.wm_transient(self.root)
|
window.wm_transient(self.root)
|
||||||
window.wait_visibility()
|
window.wait_visibility()
|
||||||
window.grab_set()
|
window.grab_set()
|
||||||
return window
|
return window
|
||||||
|
|
||||||
def open_window_edit_records(self, data):
|
def open_window_edit_records(self, record_id: int):
|
||||||
window = EditRecord(self, self.root, data)
|
window = EditRecord(self, self.root, record_id, self.config)
|
||||||
window.wm_transient(self.root)
|
window.wm_transient(self.root)
|
||||||
window.wait_visibility()
|
window.wait_visibility()
|
||||||
window.grab_set()
|
window.grab_set()
|
||||||
|
|
||||||
|
def update_status_bar(self):
|
||||||
|
self._count_address_records()
|
||||||
|
self.statusbar.set(f"Adressen: {self.length_address_list} | Aktive Adressen: {self.length_address_list_active}")
|
||||||
|
|
||||||
|
def _count_address_records(self):
|
||||||
|
self.length_address_list = len(self.address_list)
|
||||||
|
count = 0
|
||||||
|
for address in self.address_list:
|
||||||
|
if address["aktiv"] == "x":
|
||||||
|
count += 1
|
||||||
|
self.length_address_list_active = count
|
||||||
|
|
||||||
|
def first_sort_after_start(self):
|
||||||
|
self.address_list.sort(key=lambda x: (x["firma"], x["name"]))
|
||||||
|
self.populate_table()
|
||||||
|
|
||||||
|
def focus_table(self, event):
|
||||||
|
first = self.table.get_children()[0]
|
||||||
|
last = self.table.get_children()[-1]
|
||||||
|
goto = last if event.keysym == "Up" else first
|
||||||
|
if self.table.focus() == "":
|
||||||
|
self.table.selection_set(goto)
|
||||||
|
self.table.focus(goto)
|
||||||
|
self.table.see(goto)
|
||||||
|
self.table.focus_force()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import os
|
import os
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser, NoOptionError, NoSectionError, DuplicateSectionError
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
parser: ConfigParser
|
parser: ConfigParser
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, filename: str = "config.ini"):
|
||||||
"""
|
"""
|
||||||
Config parser reading config.ini
|
Config parser reading config.ini
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ class Config:
|
|||||||
full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" )
|
full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" )
|
||||||
if not os.path.exists(full_path):
|
if not os.path.exists(full_path):
|
||||||
os.makedirs(full_path)
|
os.makedirs(full_path)
|
||||||
self.config_file = os.path.join(full_path, "config.ini")
|
self.config_file = os.path.join(full_path, filename)
|
||||||
|
|
||||||
self._load()
|
self._load()
|
||||||
|
|
||||||
@@ -32,14 +32,25 @@ class Config:
|
|||||||
|
|
||||||
def add_section(self, section):
|
def add_section(self, section):
|
||||||
self._load()
|
self._load()
|
||||||
self.parser.add_section(section)
|
try:
|
||||||
|
self.parser.add_section(section)
|
||||||
|
except DuplicateSectionError:
|
||||||
|
return
|
||||||
|
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
def set(self, section: str, option: str, value: str):
|
def set(self, section: str, option: str, value: str):
|
||||||
self._load()
|
self._load()
|
||||||
|
self.add_section(section)
|
||||||
self.parser.set(section, option, value)
|
self.parser.set(section, option, value)
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
def get(self, section: str, option: str):
|
def get(self, section: str, option: str):
|
||||||
self._load()
|
self._load()
|
||||||
return self.parser.get(section, option)
|
try:
|
||||||
|
option = self.parser.get(section, option)
|
||||||
|
except NoOptionError:
|
||||||
|
option = ""
|
||||||
|
except NoSectionError:
|
||||||
|
option = ""
|
||||||
|
return option
|
||||||
|
|||||||
89
src/connector.py
Normal file
89
src/connector.py
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
import config
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
|
||||||
|
class Connector(ABC):
|
||||||
|
def __init__(self, config: Config):
|
||||||
|
self.config = config
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_all(self) -> list:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_by_id(self, record_id: int) -> dict:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def delete_by_id(self, record_id: int):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_record(self, updated_record: dict):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def create_new(self, values: dict) -> int:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_all_sorted_by(self, field: str, reverse: bool = False) -> list:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class JSONConnector(Connector):
|
||||||
|
def __init__(self, config: Config):
|
||||||
|
super().__init__(config)
|
||||||
|
self.json_path = self.config.get("json", "path")
|
||||||
|
self.json_file = os.path.join(self.json_path, "brovski-adress-etiketten-verwaltung-v7.json")
|
||||||
|
|
||||||
|
def get_all(self) -> list:
|
||||||
|
try:
|
||||||
|
with open(self.json_file, "r") as f:
|
||||||
|
return json.load(f)
|
||||||
|
except FileNotFoundError:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def get_all_sorted_by(self, field: str, reverse=False) -> list:
|
||||||
|
with open(self.json_file, "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
return sorted(data, key=lambda k: k[field], reverse=reverse)
|
||||||
|
|
||||||
|
def get_by_id(self, record_id: int) -> dict:
|
||||||
|
data = self.get_all()
|
||||||
|
for record in data:
|
||||||
|
if record["record_id"] == record_id:
|
||||||
|
return record
|
||||||
|
|
||||||
|
def delete_by_id(self, record_id: int):
|
||||||
|
data = self.get_all()
|
||||||
|
for idx, record in enumerate(data):
|
||||||
|
if record["record_id"] == record_id:
|
||||||
|
del data[idx]
|
||||||
|
self._write_to_file(data)
|
||||||
|
|
||||||
|
def update_record(self, new_record: dict):
|
||||||
|
data = self.get_all()
|
||||||
|
for idx, record in enumerate(data):
|
||||||
|
if record["record_id"] == new_record["record_id"]:
|
||||||
|
data[idx] = new_record
|
||||||
|
self._write_to_file(data)
|
||||||
|
|
||||||
|
def create_new(self, record: dict) -> int:
|
||||||
|
data = self.get_all()
|
||||||
|
if len(data) == 0:
|
||||||
|
next_id = 0
|
||||||
|
else:
|
||||||
|
next_id = max(data, key=lambda x: x["record_id"])["record_id"] + 1
|
||||||
|
record["record_id"] = next_id
|
||||||
|
data.append(record)
|
||||||
|
self._write_to_file(data)
|
||||||
|
return next_id
|
||||||
|
|
||||||
|
def _write_to_file(self, data):
|
||||||
|
with open(self.json_file, "w", encoding="UTF-8") as f:
|
||||||
|
json.dump(data, f, indent=4, ensure_ascii=False)
|
||||||
24
src/model.py
Normal file
24
src/model.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
from src.connector import Connector
|
||||||
|
|
||||||
|
|
||||||
|
class Model:
|
||||||
|
def __init__(self, connector: Connector):
|
||||||
|
self.connector = connector
|
||||||
|
|
||||||
|
def get_all(self):
|
||||||
|
return self.connector.get_all()
|
||||||
|
|
||||||
|
def get_all_sorted_by(self, field: str, reverse: bool = False):
|
||||||
|
return self.connector.get_all_sorted_by(field=field, reverse=reverse)
|
||||||
|
|
||||||
|
def get_by_id(self, record_id: int):
|
||||||
|
return self.connector.get_by_id(record_id)
|
||||||
|
|
||||||
|
def delete_by_id(self, record_id: int):
|
||||||
|
self.connector.delete_by_id(record_id)
|
||||||
|
|
||||||
|
def update_record(self, new_record: dict):
|
||||||
|
self.connector.update_record(new_record)
|
||||||
|
|
||||||
|
def create_new(self, record: dict):
|
||||||
|
self.connector.create_new(record)
|
||||||
@@ -3,6 +3,7 @@ from configparser import NoSectionError, NoOptionError
|
|||||||
from tkinter import font, filedialog, messagebox
|
from tkinter import font, filedialog, messagebox
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
|
from connector import JSONConnector
|
||||||
|
|
||||||
|
|
||||||
def show_error(message_title: str, message: str, parent: tk.Tk | tk.Toplevel):
|
def show_error(message_title: str, message: str, parent: tk.Tk | tk.Toplevel):
|
||||||
@@ -25,27 +26,29 @@ class Window(tk.Toplevel):
|
|||||||
self._destroy_window()
|
self._destroy_window()
|
||||||
|
|
||||||
def _destroy_window(self):
|
def _destroy_window(self):
|
||||||
|
self.parent.populate_table()
|
||||||
self.root.update()
|
self.root.update()
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
|
|
||||||
class EditRecord(Window):
|
class EditRecord(Window):
|
||||||
def __init__(self, parent, root: tk.Tk, data: tuple):
|
def __init__(self, parent, root: tk.Tk, record_id: int, config: Config):
|
||||||
super().__init__(parent, root)
|
super().__init__(parent, root)
|
||||||
self.bind("<Return>", self._update)
|
self.bind("<Return>", self._update)
|
||||||
|
|
||||||
self.data = data
|
self.model = JSONConnector(config)
|
||||||
|
|
||||||
|
record = self.model.get_by_id(record_id)
|
||||||
|
|
||||||
button_width = 8
|
button_width = 8
|
||||||
self.title("Adresse bearbeiten")
|
self.title("Adresse bearbeiten")
|
||||||
self.aktiv = tk.StringVar()
|
self.record_id = tk.IntVar(value=record_id)
|
||||||
self.firma = tk.StringVar()
|
self.aktiv = tk.StringVar(value=record.get("aktiv"))
|
||||||
self.name = tk.StringVar()
|
self.firma = tk.StringVar(value=record.get("firma"))
|
||||||
self.strasse = tk.StringVar()
|
self.name = tk.StringVar(value=record.get("name"))
|
||||||
self.plz_ort = tk.StringVar()
|
self.strasse = tk.StringVar(value=record.get("strasse"))
|
||||||
|
self.plz_ort = tk.StringVar(value=record.get("plzort"))
|
||||||
self.field_list = [self.aktiv, self.firma, self.name, self.strasse, self.plz_ort]
|
self.anzahl = tk.StringVar(value=record.get("anzahl"))
|
||||||
for argument, field in zip(self.data, self.field_list):
|
|
||||||
field.set(argument)
|
|
||||||
|
|
||||||
edit_frame = tk.Frame(self)
|
edit_frame = tk.Frame(self)
|
||||||
edit_frame.pack(side=tk.TOP, fill=tk.X, pady=20, padx=20)
|
edit_frame.pack(side=tk.TOP, fill=tk.X, pady=20, padx=20)
|
||||||
@@ -54,6 +57,7 @@ class EditRecord(Window):
|
|||||||
tk.Label(edit_frame, text="Name").grid(row=0, column=2)
|
tk.Label(edit_frame, text="Name").grid(row=0, column=2)
|
||||||
tk.Label(edit_frame, text="Strasse").grid(row=0, column=3)
|
tk.Label(edit_frame, text="Strasse").grid(row=0, column=3)
|
||||||
tk.Label(edit_frame, text="Plz/Ort").grid(row=0, column=4)
|
tk.Label(edit_frame, text="Plz/Ort").grid(row=0, column=4)
|
||||||
|
tk.Label(edit_frame, text="Anzahl").grid(row=0, column=5)
|
||||||
edit_aktiv = tk.Checkbutton(edit_frame, variable=self.aktiv, onvalue="x", offvalue="")
|
edit_aktiv = tk.Checkbutton(edit_frame, variable=self.aktiv, onvalue="x", offvalue="")
|
||||||
edit_aktiv.grid(row=1, column=0)
|
edit_aktiv.grid(row=1, column=0)
|
||||||
edit_firma = tk.Entry(edit_frame, textvariable=self.firma)
|
edit_firma = tk.Entry(edit_frame, textvariable=self.firma)
|
||||||
@@ -64,6 +68,8 @@ class EditRecord(Window):
|
|||||||
edit_strasse.grid(row=1, column=3)
|
edit_strasse.grid(row=1, column=3)
|
||||||
edit_plz_ort = tk.Entry(edit_frame, textvariable=self.plz_ort)
|
edit_plz_ort = tk.Entry(edit_frame, textvariable=self.plz_ort)
|
||||||
edit_plz_ort.grid(row=1, column=4)
|
edit_plz_ort.grid(row=1, column=4)
|
||||||
|
edit_anzahl = tk.Entry(edit_frame, textvariable=self.anzahl)
|
||||||
|
edit_anzahl.grid(row=1, column=5)
|
||||||
|
|
||||||
button_frame = tk.Frame(self)
|
button_frame = tk.Frame(self)
|
||||||
button_frame.pack(side=tk.TOP, pady=10)
|
button_frame.pack(side=tk.TOP, pady=10)
|
||||||
@@ -71,15 +77,24 @@ class EditRecord(Window):
|
|||||||
tk.Button(button_frame, text="Abbrechen", command=self.close_window, width=button_width).pack(side=tk.LEFT)
|
tk.Button(button_frame, text="Abbrechen", command=self.close_window, width=button_width).pack(side=tk.LEFT)
|
||||||
|
|
||||||
def _update(self, event = None):
|
def _update(self, event = None):
|
||||||
self.parent.update_record(self.field_list)
|
new_record = {
|
||||||
|
"record_id": self.record_id.get(),
|
||||||
|
"aktiv": self.aktiv.get(),
|
||||||
|
"firma": self.firma.get(),
|
||||||
|
"name": self.name.get(),
|
||||||
|
"strasse": self.strasse.get(),
|
||||||
|
"plzort": self.plz_ort.get(),
|
||||||
|
"anzahl": self.anzahl.get(),
|
||||||
|
}
|
||||||
|
self.model.update_record(new_record)
|
||||||
self.close_window()
|
self.close_window()
|
||||||
|
|
||||||
|
|
||||||
class SettingsWindow(Window):
|
class SettingsWindow(Window):
|
||||||
def __init__(self, parent, root: tk.Tk):
|
def __init__(self, parent, root: tk.Tk, config: Config):
|
||||||
super().__init__(parent, root)
|
super().__init__(parent, root)
|
||||||
self.geometry(f"500x330+{self.root.winfo_x() + 20}+{self.root.winfo_y() + 20}")
|
self.geometry(f"500x330+{self.root.winfo_x() + 20}+{self.root.winfo_y() + 20}")
|
||||||
self.config = Config()
|
self.config = config
|
||||||
self.json_file = tk.StringVar()
|
self.json_file = tk.StringVar()
|
||||||
self.csv_file = tk.StringVar()
|
self.csv_file = tk.StringVar()
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0.6a
|
0.1.1b
|
||||||
|
|||||||
Reference in New Issue
Block a user