day of hard work with xkirsch #2
This commit is contained in:
153
web/app.py
153
web/app.py
@@ -1,8 +1,8 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for, flash, session
|
||||
from flask import Flask, render_template, request, redirect, url_for, flash, session, jsonify
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
from db import get_db_connection, fetch_users, fetch_orders, fetch_roles, fetch_repairs, fetch_employees
|
||||
from db import get_db_connection, fetch_users, fetch_orders, fetch_roles, fetch_repairs, fetch_employees, fetch_products, update_product, add_product_stock
|
||||
from auth import encrypt_password, check_password
|
||||
import random
|
||||
|
||||
@@ -72,6 +72,10 @@ def login():
|
||||
flash('Úspěšně přihlášen.', 'success')
|
||||
if user['Role_ID'] == 1:
|
||||
return redirect(url_for('administrator'))
|
||||
elif user['Role_ID'] == 2:
|
||||
return redirect(url_for('managers'))
|
||||
elif user['Role_ID'] == 3:
|
||||
return redirect(url_for('repairs'))
|
||||
else:
|
||||
return redirect(url_for('home'))
|
||||
|
||||
@@ -95,7 +99,18 @@ def administrator():
|
||||
users = fetch_users(session.get('role_id'))
|
||||
orders = fetch_orders()
|
||||
roles = fetch_roles()
|
||||
return render_template('administrator.html', users=users, orders=orders, roles=roles)
|
||||
products = fetch_products()
|
||||
return render_template('administrator.html', users=users, orders=orders, roles=roles, products=products)
|
||||
|
||||
@app.route('/managers')
|
||||
def managers():
|
||||
if not session.get('logged_in') or session.get('role_id') != 2:
|
||||
flash('Nemáte oprávnění k přístupu na tuto stránku.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
users = fetch_users(session.get('role_id'))
|
||||
orders = fetch_orders()
|
||||
roles = fetch_roles()
|
||||
return render_template('managers.html', users=users, orders=orders, roles=roles)
|
||||
|
||||
@app.route('/create_user', methods=['GET', 'POST'])
|
||||
def create_user():
|
||||
@@ -128,7 +143,8 @@ def create_user():
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return render_template('create_user.html')
|
||||
roles = fetch_roles()
|
||||
return render_template('create_user.html', roles=roles)
|
||||
|
||||
@app.route('/edit_user/<int:user_id>', methods=['GET', 'POST'])
|
||||
def edit_user(user_id):
|
||||
@@ -191,6 +207,7 @@ def edit_order(order_id):
|
||||
|
||||
conn = get_db_connection()
|
||||
order = conn.execute('SELECT * FROM Objednavky WHERE ID_Objednavky = ?', (order_id,)).fetchone()
|
||||
users = fetch_users(session.get('role_id'))
|
||||
|
||||
if request.method == 'POST':
|
||||
stav = request.form['stav']
|
||||
@@ -208,7 +225,21 @@ def edit_order(order_id):
|
||||
return redirect(url_for('administrator'))
|
||||
|
||||
conn.close()
|
||||
return render_template('edit_order.html', order=order)
|
||||
return render_template('edit_order.html', order=order, users=users)
|
||||
|
||||
@app.route('/delete_order/<int:order_id>', methods=['POST'])
|
||||
def delete_order(order_id):
|
||||
if not session.get('logged_in') or session.get('role_id') != 1:
|
||||
flash('Nemáte oprávnění k přístupu na tuto stránku.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
conn = get_db_connection()
|
||||
conn.execute('DELETE FROM Objednavky WHERE ID_Objednavky = ?', (order_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
flash('Objednávka byla úspěšně smazána.')
|
||||
return redirect(url_for('administrator'))
|
||||
|
||||
@app.route('/repairs')
|
||||
def repairs():
|
||||
@@ -216,7 +247,9 @@ def repairs():
|
||||
flash('Nemáte oprávnění k přístupu na tuto stránku.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
repairs = fetch_repairs()
|
||||
return render_template('repairs.html', repairs=repairs)
|
||||
orders = fetch_orders()
|
||||
users = fetch_users(session.get('role_id'))
|
||||
return render_template('repairs.html', repairs=repairs, orders=orders, users=users)
|
||||
|
||||
@app.route('/create_repair', methods=['GET', 'POST'])
|
||||
def create_repair():
|
||||
@@ -288,12 +321,9 @@ def delete_repair(repair_id):
|
||||
def create_reservation():
|
||||
full_name = request.form['fullName']
|
||||
email = request.form['email']
|
||||
date = request.form['date']
|
||||
datum_konce = request.form['datum_konce']
|
||||
description = request.form['description']
|
||||
|
||||
# Convert date to DD.MM.YYYY format
|
||||
formatted_date = datetime.strptime(date, '%Y-%m-%d').strftime('%d.%m.%Y')
|
||||
|
||||
conn = get_db_connection()
|
||||
try:
|
||||
# Fetch a random user with role_id 2
|
||||
@@ -303,8 +333,8 @@ def create_reservation():
|
||||
else:
|
||||
user_id = 1 # Fallback to a default user ID if no user with role_id 2 is found
|
||||
|
||||
conn.execute('INSERT INTO Objednavky (Stav, ID_Zamestnance, Popis, ID_Vozidla, Datum_Zacatku, Cena) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
('Nová', user_id, description, 1, formatted_date, 0.0)) # Example values for ID_Vozidla
|
||||
conn.execute('INSERT INTO Objednavky (Stav, ID_Zamestnance, Popis, ID_Vozidla, Datum_Zacatku, Datum_Konce, Cena) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
('Nová', user_id, description, 1, datetime.now().strftime('%Y-%m-%d'), datum_konce, 0.0)) # Use the current date for Datum_Zacatku
|
||||
conn.commit()
|
||||
flash('Rezervace byla úspěšně vytvořena.', 'success')
|
||||
except sqlite3.Error as e:
|
||||
@@ -314,6 +344,105 @@ def create_reservation():
|
||||
|
||||
return redirect(url_for('home'))
|
||||
|
||||
@app.route('/edit_product/<int:product_id>', methods=['GET', 'POST'])
|
||||
def edit_product(product_id):
|
||||
if not session.get('logged_in') or session.get('role_id') != 1:
|
||||
flash('Nemáte oprávnění k přístupu na tuto stránku.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
conn = get_db_connection()
|
||||
product = conn.execute('SELECT * FROM Produkty WHERE ID_Produktu = ?', (product_id,)).fetchone()
|
||||
|
||||
if request.method == 'POST':
|
||||
nazev = request.form['nazev']
|
||||
popis = request.form['popis']
|
||||
momentalni_zasoba = request.form['momentalni_zasoba']
|
||||
minimalni_zasoba = request.form['minimalni_zasoba']
|
||||
|
||||
update_product(product_id, nazev, popis, momentalni_zasoba, minimalni_zasoba)
|
||||
|
||||
flash('Produkt byl úspěšně aktualizován.')
|
||||
return redirect(url_for('administrator'))
|
||||
|
||||
conn.close()
|
||||
return render_template('edit_product.html', product=product)
|
||||
|
||||
@app.route('/add_product_stock/<int:product_id>', methods=['GET', 'POST'])
|
||||
def add_product_stock(product_id):
|
||||
if not session.get('logged_in') or session.get('role_id') != 1:
|
||||
flash('Nemáte oprávnění k přístupu na tuto stránku.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
conn = get_db_connection()
|
||||
product = conn.execute('SELECT * FROM Produkty WHERE ID_Produktu = ?', (product_id,)).fetchone()
|
||||
|
||||
if request.method == 'POST':
|
||||
quantity = request.form['quantity']
|
||||
|
||||
add_product_stock(product_id, quantity)
|
||||
|
||||
flash('Zásoba byla úspěšně přidána.')
|
||||
return redirect(url_for('administrator'))
|
||||
|
||||
conn.close()
|
||||
return render_template('add_product_stock.html', product=product)
|
||||
|
||||
@app.route('/create_product', methods=['GET', 'POST'])
|
||||
def create_product():
|
||||
if not session.get('logged_in') or session.get('role_id') != 1:
|
||||
flash('Nemáte oprávnění k přístupu na tuto stránku.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
if request.method == 'POST':
|
||||
nazev = request.form['nazev']
|
||||
popis = request.form['popis']
|
||||
momentalni_zasoba = request.form['momentalni_zasoba']
|
||||
minimalni_zasoba = request.form['minimalni_zasoba']
|
||||
|
||||
conn = get_db_connection()
|
||||
try:
|
||||
conn.execute('INSERT INTO Produkty (Nazev, Popis, Momentalni_Zasoba, Minimalni_Zasoba) VALUES (?, ?, ?, ?)',
|
||||
(nazev, popis, momentalni_zasoba, minimalni_zasoba))
|
||||
conn.commit()
|
||||
flash('Nový produkt byl úspěšně přidán.', 'success')
|
||||
return redirect(url_for('administrator'))
|
||||
except sqlite3.Error as e:
|
||||
flash(f'Chyba při přidávání produktu: {e}', 'error')
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return render_template('create_product.html')
|
||||
|
||||
@app.route('/statistics')
|
||||
def statistics():
|
||||
conn = get_db_connection()
|
||||
repairs_data = conn.execute('''
|
||||
SELECT Zamestnanci.Jmeno || ' ' || Zamestnanci.Prijmeni AS employee, COUNT(Opravy.ID_Opravy) AS count
|
||||
FROM Opravy
|
||||
JOIN Zamestnanci ON Opravy.ID_Zamestnance = Zamestnanci.ID_Uzivatele
|
||||
GROUP BY Zamestnanci.ID_Uzivatele
|
||||
''').fetchall()
|
||||
repairs_data = [dict(employee=row['employee'], count=row['count']) for row in repairs_data]
|
||||
conn.close()
|
||||
return render_template('statistics.html', repairs_data=repairs_data)
|
||||
|
||||
@app.route('/repairs_by_date')
|
||||
def repairs_by_date():
|
||||
start_date = request.args.get('start')
|
||||
end_date = request.args.get('end')
|
||||
|
||||
conn = get_db_connection()
|
||||
repairs_data = conn.execute('''
|
||||
SELECT DATE(Datum_Zacatku) AS date, COUNT(*) AS count
|
||||
FROM Objednavky
|
||||
WHERE Datum_Zacatku BETWEEN ? AND ?
|
||||
GROUP BY DATE(Datum_Zacatku)
|
||||
''', (start_date, end_date)).fetchall()
|
||||
repairs_data = [dict(date=row['date'], count=row['count']) for row in repairs_data]
|
||||
conn.close()
|
||||
|
||||
return jsonify(repairs_data)
|
||||
|
||||
# Always redirect back home
|
||||
@app.errorhandler(404)
|
||||
def default_page(e):
|
||||
|
||||
Reference in New Issue
Block a user