dashboard/app/routes.py

92 lines
3.1 KiB
Python

from flask import render_template, redirect, url_for, current_app, render_template_string, request
import os
import csv
from app import app, htmx
def read_invoices_from_csv(csv_file_path):
try:
with open(csv_file_path, mode='r', encoding='utf-8') as file:
return list(csv.DictReader(file, delimiter=';'))
except FileNotFoundError:
current_app.logger.error(f"CSV file not found at {csv_file_path}")
except csv.Error as e:
current_app.logger.error(f"Error reading CSV file: {e}")
return []
@app.route('/load-invoices')
def load_invoices():
csv_file_path = os.path.join(current_app.root_path, 'data', 'invoices.csv')
invoices = read_invoices_from_csv(csv_file_path)
if not invoices:
return "No invoice data available", 500
return render_template('partials/invoice_table.html', invoices=invoices)
def render_htmx_or_full(template_partial, template_full):
try:
if htmx:
return render_template(template_partial)
return render_template(template_full)
except Exception as e:
current_app.logger.error(f"Error rendering template: {e}")
return "An error occurred", 500
@app.route('/')
def index():
return render_htmx_or_full('partials/index.html', 'index.html')
@app.route('/buchhaltung')
def buchhaltung():
return render_htmx_or_full('partials/buchhaltung.html', 'index.html')
@app.route('/add-invoice', methods=['POST'])
def add_invoice():
csv_file_path = os.path.join(current_app.root_path, 'data', 'invoices.csv')
new_invoice = {
'Rechnungsnummer': request.form['rechnungsnummer'],
'Email': request.form['email'],
'Anrede': request.form['anrede']
}
try:
# Read the existing data
with open(csv_file_path, 'r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
fieldnames = reader.fieldnames
existing_data = list(reader)
# Add the new invoice at the beginning of the data
existing_data.insert(0, new_invoice)
# Write the updated data back to the file
with open(csv_file_path, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=';')
writer.writeheader()
writer.writerows(existing_data)
return redirect(url_for('buchhaltung'))
except Exception as e:
current_app.logger.error(f"Error updating CSV file: {e}")
return "An error occurred while adding the invoice", 500
@app.route('/flottenmanagement')
def flottenmanagement():
return render_htmx_or_full('partials/flottenmanagement.html', 'index.html')
@app.route('/debug/templates')
def debug_templates():
template_dir = os.path.join(current_app.root_path, 'templates')
all_files = []
for path, subdirs, files in os.walk(template_dir):
for name in files:
all_files.append(os.path.join(path, name))
return '\n'.join(all_files)