from flask import Flask, request, render_template, session, redirect, url_for import requests app = Flask(__name__) app.secret_key = 'Kill me already' @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': if not request.form.get('username') or not request.form.get('password'): return render_template('login.html', title='Авторизация', errors='Необходимо заполнить все поля', logged_in=False) response = requests.post("http://localhost:8080/api/login", json={ 'username': request.form.get('username'), 'password': request.form.get('password') }) if response.status_code != 200: return render_template('login.html', title='Авторизация', errors='Неверный логин или пароль', logged_in=False) session['token'] = response.json()['token'] session['administrator_id'] = response.json()['administrator_id'] session['car_station_id'] = response.json()['car_station_id'] return redirect(url_for('rents')) return render_template('login.html', title='Авторизация', logged_in=False) @app.route('/logout') def logout(): session.pop('token') session.pop('administrator_id') session.pop('car_station_id') requests.post('http://localhost:8080/api/logout') return redirect(url_for('login')) @app.route('/rents', methods=['GET', 'POST']) def rents(): if not session['token']: return redirect(url_for('login')) rents_data = requests.get('http://localhost:8080/api/rents/', headers={'Authorization': session['token']}).json() cars = list( filter(lambda c: c['car_station_id'] == session['car_station_id'], requests.get('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}).json() ) ) clients = requests.get('http://localhost:8080/api/clients/', headers={'Authorization': session['token']}).json() for rent in rents_data: from datetime import datetime date_format = '%Y-%m-%dT%H:%M:%S.%f' parsed_date = datetime.strptime(rent['start_time'], date_format) rent['start_time'] = parsed_date.strftime('%Y-%m-%d %H:%M:%S') for car in cars: if car['id'] == rent['car_id']: rent['car'] = car if not rent['time_amount']: cars.remove(car) break for client in clients: if client['id'] == rent['client_id']: rent['client'] = client break if request.method == 'POST': if not request.form.get('id') and (not request.form.get('client_id') or not request.form.get('car_id')): return render_template( 'rents.html', errors='Необходимо заполнить все поля', title='Аренды', rents=rents_data, cars=cars, clients=clients, logged_in=True, selected_client=int(request.args.get('client')) if request.args.get('client') else None, selected_car=int(request.args.get('car')) if request.args.get('car') else None ) response = None if request.form.get('id'): response = requests.get(f'http://localhost:8080/api/rents/{request.form.get("id")}/end', headers={'Authorization': session['token']}) else: response = requests.post('http://localhost:8080/api/rents/', headers={'Authorization': session['token']}, json={ "client_id": int(request.form.get('client_id')), "car_id": int(request.form.get('car_id')) }) if response.status_code != 200: return render_template( 'rents.html', title='Аренды', errors=response.json(), rents=rents_data, cars=cars, clients=clients, logged_in=True, selected_client=int(request.args.get('client')) if request.args.get('client') else None, selected_car=int(request.args.get('car')) if request.args.get('car') else None ) if request.form.get('id'): return redirect(url_for('rent', id=request.form.get('id'))) rents_data = requests.get('http://localhost:8080/api/rents/', headers={'Authorization': session['token']}).json() cars = list( filter(lambda c: c['car_station_id'] == session['car_station_id'], requests.get('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}).json() ) ) clients = requests.get('http://localhost:8080/api/clients/', headers={'Authorization': session['token']}).json() for rent in rents_data: from datetime import datetime date_format = '%Y-%m-%dT%H:%M:%S.%f' parsed_date = datetime.strptime(rent['start_time'], date_format) rent['start_time'] = parsed_date.strftime('%Y-%m-%d %H:%M:%S') for car in cars: if car['id'] == rent['car_id']: rent['car'] = car if not rent['time_amount']: cars.remove(car) break for client in clients: if client['id'] == rent['client_id']: rent['client'] = client break return render_template( 'rents.html', title='Аренды', rents=rents_data, cars=cars, clients=clients, logged_in=True, selected_client=int(request.args.get('client')) if request.args.get('client') else None, selected_car=int(request.args.get('car')) if request.args.get('car') else None ) @app.route('/rents/', methods=['GET', 'POST']) def rent(id): if not session['token']: return redirect(url_for('login')) rent = requests.get(f'http://localhost:8080/api/rents/{id}', headers={'Authorization': session['token']}).json() client = requests.get(f'http://localhost:8080/api/clients/{rent["client_id"]}', headers={'Authorization': session['token']}).json() car = requests.get(f'http://localhost:8080/api/cars/{rent["car_id"]}', headers={'Authorization': session['token']}).json() rent['client'] = client rent['car'] = car from datetime import datetime date_format = '%Y-%m-%dT%H:%M:%S.%f' parsed_date = datetime.strptime(rent['start_time'], date_format) rent['start_time'] = parsed_date.strftime('%Y-%m-%d %H:%M:%S') import math return render_template('rent.html', title='Просмотр аренды', rent=rent, logged_in=True, float=float, int=int, math=math) @app.route('/clients', defaults={'id': 0}, methods=['GET', 'POST']) @app.route('/clients/', methods=['GET', 'POST']) def clients(id): if not session['token']: return redirect(url_for('login')) clients = requests.get('http://localhost:8080/api/clients/', headers={'Authorization': session['token']}).json() if request.method == 'POST': if id != 0: if not request.form.get('name') or not request.form.get('surname') or not request.form.get('middlename') or not request.form.get('phone'): return render_template( 'clients.html', title='Изменение клиента', errors='Должны быть заполнены все поля!', current_client=list(filter(lambda c: c['id'] == id, clients))[0], clients=clients, logged_in=True ) response = requests.patch(f'http://localhost:8080/api/clients/{id}', headers={'Authorization': session['token']}, json={ 'name': request.form.get('name'), 'surname': request.form.get('surname'), 'middlename': request.form.get('middlename'), 'phone': request.form.get('phone') }) if response.status_code != 200: return render_template( 'clients.html', title='Изменение клиента', errors=response.json(), clients=clients, current_client=list(filter(lambda c: c['id'] == id, clients))[0], logged_in=True ) return redirect(url_for('clients')) else: if not request.form.get('name') or not request.form.get('surname') or not request.form.get('middlename') or not request.form.get('phone'): return render_template('clients.html', title='Клиенты', errors='Должны быть заполнены все поля!', clients=clients, logged_in=True) response = requests.post('http://localhost:8080/api/clients/', headers={'Authorization': session['token']}, json={ 'name': request.form.get('name'), 'surname': request.form.get('surname'), 'middlename': request.form.get('middlename'), 'phone': request.form.get('phone') }) if response.status_code != 200: return render_template('clients.html', title='Клиенты', errors=response.json(), clients=clients, logged_in=True) clients = requests.get('http://localhost:8080/api/clients/', headers={'Authorization': session['token']}).json() if id != 0: return render_template('clients.html', title='Изменение клиента', clients=clients, logged_in=True, current_client=list(filter(lambda c: c['id'] == id, clients))[0]) return render_template('clients.html', title='Клиенты', clients=clients, logged_in=True) @app.route('/owners', defaults={'id': 0}, methods=['GET', 'POST']) @app.route('/owners/', methods=['GET', 'POST']) def owners(id): if not session['token']: return redirect(url_for('login')) owners = requests.get('http://localhost:8080/api/owners/', headers={'Authorization': session['token']}).json() cars = list( filter(lambda c: c['car_station_id'] == session['car_station_id'], requests.get('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}).json() ) ) if request.method == 'POST': if id != 0: if not request.form.get('name') or not request.form.get('surname') or not request.form.get('middlename') or not request.form.get('phone'): return render_template( 'owners.html', title='Изменение владельца', errors='Должны быть заполнены все поля!', current_owner=list(filter(lambda c: c['id'] == id, owners))[0], owners=owners, cars=cars, logged_in=True ) response = requests.patch(f'http://localhost:8080/api/owners/{id}', headers={'Authorization': session['token']}, json={ 'name': request.form.get('name'), 'surname': request.form.get('surname'), 'middlename': request.form.get('middlename'), 'phone': request.form.get('phone') }) if response.status_code != 200: return render_template( 'owners.html', title='Изменение владельца', errors=response.json(), owners=owners, cars=cars, current_owner=list(filter(lambda c: c['id'] == id, owners))[0], logged_in=True ) return redirect(url_for('owners')) else: if not request.form.get('name') or not request.form.get('surname') or not request.form.get('middlename') or not request.form.get('phone'): return render_template('owners.html', title='Владельцы', errors='Должны быть заполнены все поля!', cars=cars, owners=owners, logged_in=True) response = requests.post('http://localhost:8080/api/owners/', headers={'Authorization': session['token']}, json={ 'name': request.form.get('name'), 'surname': request.form.get('surname'), 'middlename': request.form.get('middlename'), 'phone': request.form.get('phone') }) if response.status_code != 200: return render_template('owners.html', title='Владельцы', errors=response.json(), owners=owners, cars=cars, logged_in=True) owners = requests.get('http://localhost:8080/api/owners/', headers={'Authorization': session['token']}).json() if id != 0: return render_template('owners.html', title='Изменение владельца', owners=owners, cars=cars, logged_in=True, current_owner=list(filter(lambda c: c['id'] == id, owners))[0]) return render_template('owners.html', title='Владельца', owners=owners, cars=cars, logged_in=True) @app.route('/cars', defaults={'id': 0}, methods=['GET', 'POST']) @app.route('/cars/', methods=['GET', 'POST']) def cars(id): if not session['token']: return redirect(url_for('login')) owners = requests.get('http://localhost:8080/api/owners/', headers={'Authorization': session['token']}).json() cars = list( filter(lambda c: c['car_station_id'] == session['car_station_id'], requests.get('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}).json() ) ) if request.method == 'POST': if id != 0: if not request.form.get('brand') or not request.form.get('model') or not request.form.get('price') or not request.form.get('owner_id'): return render_template( 'cars.html', title='Изменение автомобиля', errors='Должны быть заполнены все поля!', current_car=list(filter(lambda c: c['id'] == id, cars))[0], cars=cars, owners=owners, logged_in=True, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None ) response = requests.patch(f'http://localhost:8080/api/cars/{id}', headers={'Authorization': session['token']}, json={ 'brand': request.form.get('brand'), 'model': request.form.get('model'), 'price': float(request.form.get('price')), 'owner_id': int(request.form.get('owner_id')), 'car_station_id': session['car_station_id'] }) if response.status_code != 200: return render_template( 'cars.html', title='Изменение автомобиля', errors=response.json(), cars=cars, owners=owners, current_car=list(filter(lambda c: c['id'] == id, cars))[0], logged_in=True, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None ) return redirect(url_for('cars')) else: if not request.form.get('brand') or not request.form.get('model') or not request.form.get('price') or not request.form.get('owner_id'): return render_template('cars.html', title='Автомобили', errors='Должны быть заполнены все поля!', owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None, cars=cars, logged_in=True) response = requests.post('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}, json={ 'brand': request.form.get('brand'), 'model': request.form.get('model'), 'price': float(request.form.get('price')), 'owner_id': int(request.form.get('owner_id')), 'car_station_id': session['car_station_id'] }) if response.status_code != 200: return render_template('cars.html', title='Автомобили', errors=response.json(), cars=cars, owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None, logged_in=True) cars = requests.get('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}).json() if id != 0: return render_template('cars.html', title='Изменение автомобиля', cars=cars, logged_in=True, owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None, current_car=list(filter(lambda c: c['id'] == id, cars))[0]) return render_template('cars.html', title='Автомобили', cars=cars, logged_in=True, owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None) @app.route('/report') def report(): if not session['token']: return redirect(url_for('login')) response = requests.get('http://localhost:8080/api/cars/report', headers={'Authorization': session['token']}) reports = list() for report in response.json(): report['income'] = float(report['income']) reports.append(report) return render_template('report.html', title='Отчёт', reports=reports, logged_in=True) @app.route('/admin', methods=['GET', 'POST']) def admin(): if not session['token']: return redirect(url_for('login')) if request.method == 'POST': response = requests.get('http://localhost:8080/benchmark', headers={'Authorization': session['token']}) result = response.json() return render_template('admin.html', title='Панель администратора', logged_in=True, result=result) return render_template('admin.html', title='Панель администратора', logged_in=True) if __name__ == '__main__': app.run(host='0.0.0.0', port='5000')