# Zeiterfassung # User bezogene Funktionen import os import datetime import time import json import shutil from definitions import userfolder, scriptpath, usersettingsfilename, photofilename, status_in, status_out # Benutzerklasse class user: def __init__(self, name): self.userfolder = f"{scriptpath}/{userfolder}/{name}" self.settingsfile = f"{self.userfolder}/{usersettingsfilename}" self.photofile = f"{self.userfolder}/{photofilename}" # Stammdaten einlesen try: with open(self.settingsfile) as json_file: data = json.load(json_file) except: print("Fehler beim Erstellen des Datenarrays.") #Hier muss noch Fehlerbehandlungcode hin self.password = data["password"] self.workhours = data["workhours"] self.username = data["username"] self.fullname = data["fullname"] def get_stamp_file(self): year = str(datetime.datetime.now().year) month = str(datetime.datetime.now().month) completepath = f"{self.userfolder}/{year}-{month}.txt" return completepath def timestamp(self, stamptime=-1): filename = self.get_stamp_file() if stamptime == -1: stamptime = time.time() print(stamptime) timestamp = int(stamptime) try: # Öffne die Datei im Anhang-Modus ('a') with open(filename, 'a') as file: # Schreibe den Timestamp in die Datei und füge einen Zeilenumbruch hinzu file.write(f"{timestamp}\n") file.close() except FileNotFoundError: # Fehlende Verzeichnisse anlegen folder_path = os.path.dirname(filename) os.makedirs(folder_path, exist_ok=True) self.timestamp() def stamp_status(self): try: # Öffne die Datei im Lese-Modus ('r') with open(self.get_stamp_file(), 'r') as file: # Zähle die Zeilen lines = file.readlines() file.close() except FileNotFoundError: print(f"Die Datei {filename} wurde nicht gefunden.") if len(lines)== 0: print(f"Keine Einträge") elif len(lines) % 2 == 0: return (status_out) else: return (status_in) def last_2_timestmaps(self): with open(self.get_stamp_file(), 'r') as file: lines = file.readlines() file.close() if len(lines) > 2: second_last_line = int(lines[-2]) last_line = int(lines[-1]) last_2_timestmaps = [second_last_line, last_line] return last_2_timestmaps elif len(lines) == 1: return int(lines[0]) else: return -1 def write_settings(self): dict = { } dict["username"] = (self.username) dict["fullname"] = (self.fullname) dict["password"] = (self.password) dict["workhours"] = (self.workhours) json_dict = json.dumps(dict, indent=4) with open(self.settingsfile, "w") as outputfile: outputfile.write(json_dict) pathcheck = self.userfolder pathcheck = pathcheck.removeprefix(f"{scriptpath}/{userfolder}/") if pathcheck != self.username: os.rename(self.userfolder, f"{scriptpath}/{userfolder}/{self.username}") def del_user(self): shutil.rmtree(self.userfolder) def get_years(self): txtfiles = [ ] for file in os.listdir(self.userfolder): if file.endswith(".txt"): txtfiles.append(file) for i in range(len(txtfiles)): txtfiles[i] = txtfiles[i][:4] txtfiles = list(set(txtfiles)) txtfiles.sort() return txtfiles def get_months(self, year): txtfiles = [ ] for file in os.listdir(self.userfolder): if file.endswith(".txt"): txtfiles.append(file) txtfiles.sort() available_months = [ ] for entry in txtfiles: if entry[:4] == str(year): available_months.append(entry[5:-4]) return available_months def get_timestamps(self, year, month): with open(f"{self.userfolder}/{year}-{month}.txt", "r") as file: timestamps = file.readlines() timestamps.sort() return timestamps def write_edited_timestamps(self, timestamps, year, month): with open(f"{self.userfolder}/{year}-{month}.txt", "w") as file: file.write(''.join(timestamps)) # Benutzer auflisten def list_users(): users = [d for d in os.listdir(userfolder) if os.path.isdir(os.path.join(userfolder, d))] users.sort() return users