From 77a646525403d07511b3be008bcf2525b3e1017b Mon Sep 17 00:00:00 2001 From: StochasticMouse Date: Sun, 21 Jan 2024 10:54:26 +0100 Subject: [PATCH] can now modify entries in animelist.csv --- templates/index.html | 15 ++++++++++++ webserver.py | 57 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/templates/index.html b/templates/index.html index 94e6bca..05055ef 100644 --- a/templates/index.html +++ b/templates/index.html @@ -26,3 +26,18 @@ + +
+

Vuoi modificare un anime?

+ + + + + + + + + + +
Index:Url:Cartella:Nome Ep:Abilitato:JellyNaming:
+
diff --git a/webserver.py b/webserver.py index 9306ea2..e6241b7 100644 --- a/webserver.py +++ b/webserver.py @@ -22,8 +22,8 @@ def new_animu(): if request.method == 'POST': form_data = request.form #print(form_data) - if add_animu(form_data): - return f'Anime aggiunto con successo' + if add_animu_fun(form_data): + return redirect('/') return f'Errore aggiungendo anime, hai inserito correttamente i valori?' @app.route("/del_animu", methods = ['POST', 'GET']) @@ -33,11 +33,23 @@ def del_animu(): if request.method == 'POST': form_data = request.form print(form_data) - if del_animu(form_data): - return f'Anime rimosso con successo' + if del_animu_fun(form_data): + return redirect('/') return f'Errore rimuovendo anime, hai inserito correttamente i valori?' -def add_animu(form_data): +@app.route("/mod_animu", methods = ['POST', 'GET']) +def mod_animu(): + if request.method == 'GET': + return redirect('/') + if request.method == 'POST': + form_data = request.form + print(form_data) + if mod_animu_fun(form_data): + return redirect('/') + return f'Errore modificando anime, hai inserito correttamente i valori?' + + +def add_animu_fun(form_data): try: if not (form_data['field_season'] and form_data['field_url'] and form_data['field_folder']): return False @@ -55,7 +67,7 @@ def add_animu(form_data): print(e) return False -def del_animu(form_data): +def del_animu_fun(form_data): try: if not form_data['del_field']: return False @@ -74,5 +86,38 @@ def del_animu(form_data): print(e) return False +def mod_animu_fun(form_data): + try: + index = int(form_data['field_1_index']) + url = form_data['field_1_url'] + folder = form_data['field_1_folder'] + name = form_data['field_1_name'] + check = form_data['field_1_check'] + naming = form_data['field_1_naming'] + if not ((check in '01') and (naming in '01')): + return False + newrow = [url,folder,name,check[-1:],naming[-1:]] + if not index: #cosi non si può modificare la riga 0 + return False + data = [] + with open("animelist.csv", 'r', newline='') as file: + csv_reader = csv.reader(file) + for ind, row in enumerate(csv_reader): + if ind != index: + data.append(row) + else: #cosi se lindex dato è fuori dal range non si fanno modifiche + for i in range(len(newrow)): + if newrow[i] == '': + newrow[i]=row[i] + data.append(newrow) + # Write the remaining data back to the CSV file + with open("animelist.csv", 'w', newline='') as file: + csv_writer = csv.writer(file) + csv_writer.writerows(data) + return True + except Exception as e: + print(e) + return False + if __name__ == "__main__": app.run(HOST_NAME, HOST_PORT)