SyncTk/classes/frames/devices.py

115 lines
4.1 KiB
Python
Raw Normal View History

2023-01-23 01:13:30 +02:00
from datetime import datetime
from functools import partial
from tkinter import LEFT, NSEW, E, W, ttk
from tkinter.messagebox import askyesno
2023-01-22 18:53:55 +02:00
2023-01-23 01:13:30 +02:00
import requests
2023-01-22 18:53:55 +02:00
from ttkthemes import ThemedTk
2023-01-23 01:13:30 +02:00
from classes.custom.scrollable_frame import ScrollableFrame
2023-01-22 18:53:55 +02:00
from classes.custom.themed_frame import ThemedFrame
2023-01-23 01:13:30 +02:00
from modules.utils import configGet
2023-01-22 18:53:55 +02:00
2023-01-23 01:13:30 +02:00
class FrameDevices(ScrollableFrame):
def __init__(self, master: ThemedTk, devices: list, **kwargs) -> None:
super().__init__(master, **kwargs)
master.title("Devices - Stardew Sync")
# self["borderwidth"] = 1
# self["relief"] = "solid"
self.devices = devices
# self.grid_columnconfigure(0, weight=1)
# self.grid_rowconfigure(0, weight=2)
master.columnconfigure(1, weight=1)
self.render_devices()
def render_devices(self):
i = 0
for device in self.devices:
device_frame = ThemedFrame(self, style="Card.TFrame")
device_frame.grid(column=0, row=i, pady=9, padx=9, sticky=NSEW)
# self.device_frame["borderwidth"] = 1
# self.device_frame["relief"] = "solid"
device_frame.grid_columnconfigure(0, weight=1)
device_frame.grid_columnconfigure(1, weight=3)
device_frame.grid_columnconfigure(2, weight=3)
device_title = ttk.Label(device_frame, text=device["name"], font=("SunValleyBodyFont", 12), justify=LEFT, width=46)
device_title.grid(column=0, row=0, padx=9, pady=9, sticky=W)
last_upload = "N/A" if device["last_save"] == 0 else datetime.utcfromtimestamp(device["last_save"]).strftime("%d.%m.%Y %H:%M")
device_description = ttk.Label(device_frame, text=f'OS: {device["os"]}\nClient: {device["client"]}\nLast upload: {last_upload}', width=46)
device_description.grid(column=0, row=1, padx=9, pady=9, sticky=W)
buttons_frame = ThemedFrame(device_frame)
buttons_frame.grid(column=0, columnspan=2, row=2, sticky=NSEW, padx=9, pady=9)
buttons_frame.grid_columnconfigure(0, weight=1)
button_device_rename = ttk.Button(buttons_frame, text="Rename", width=11)
button_device_rename.grid(column=0, row=0, padx=9, sticky=E)
button_device_delete_action = partial(self.device_delete, device["name"])
button_device_delete = ttk.Button(buttons_frame, text="Delete", style="Accent.TButton", width=11, command=button_device_delete_action)
button_device_delete.grid(column=1, row=0, sticky=W)
if device["name"] == configGet("name"):
2023-01-23 17:38:47 +02:00
# button_device_rename.state(["disabled"])
2023-01-23 01:13:30 +02:00
button_device_delete.state(["disabled"])
i += 1
if i+1 != len(self.devices):
divider = ttk.Separator(self, orient="horizontal")
divider.grid(column=0, row=i+1, pady=9)
i += 1
def device_delete(self, name: str):
decision = askyesno(title="Device removal", message=f"You are about to remove the device '{name}' and this will also remove all the save files uploaded by this device. Are you sure you want to continue?")
if decision is False:
return
requests.delete(f'{configGet("address")}/devices/{name}', headers={"apikey": configGet("apikey")}, verify=not configGet("allow_self_signed"))
for k in range(len(self.devices)):
print(k)
if self.devices[k]["name"] == name:
del self.devices[k]
break
for widget in self.winfo_children():
widget.destroy()
self.render_devices()
class FrameDevicesEmpty(ThemedFrame):
2023-01-22 18:53:55 +02:00
def __init__(self, master: ThemedTk, **kwargs) -> None:
super().__init__(master, **kwargs)
master.title("Devices - Stardew Sync")
2023-01-23 01:13:30 +02:00
# self["borderwidth"] = 1
# self["relief"] = "solid"
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=2)
2023-01-22 18:53:55 +02:00
2023-01-23 01:13:30 +02:00
master.columnconfigure(1, weight=1)
2023-01-22 18:53:55 +02:00
2023-01-23 01:13:30 +02:00
self.label = ttk.Label(self, text="No devices found")
self.label.grid(column=0, row=0, padx=9, pady=9)