from flask import url_for, redirect, render_template from flask_security import login_required, roles_required from sqlalchemy.exc import IntegrityError from ..entities import User, Consumable from ..forms import CreateConsumableForm, DeletConsumableForm, EditConsumableForm from ..tools import db_log_update, db_log_delete, db_log_add from .. import app, db @app.route('/admin', methods=['GET']) @roles_required('admin') def get_admin_page(): users = User.query.all() consumables = Consumable.query.all() for consumable in consumables: consumable.in_use = len(consumable.vehicles) > 0 return render_template('admin.html', users=users, consumables=consumables) @app.route('/admin/consumable/create', methods=['GET', 'POST']) @login_required def create_consumable(): form = CreateConsumableForm() choices = [(0, ''), (1, 'diesel'), (2, 'e5'), (3, 'e10')] form.ext_id.choices = choices # preinitialize the defaults with potentially existing values from a try before if form.name.data is not None: form.name.default = form.name.data if form.unit.data is not None: form.unit.default = form.unit.data if form.validate_on_submit(): new_consumable = Consumable(form.name.data, choices[form.ext_id.data][1], form.unit.data) db.session.add(new_consumable) try: db.session.commit() db_log_add(new_consumable) except IntegrityError: db.session.rollback() form.name.errors.append('"%s" is not unique.' % (form.name.data)) return render_template('createConsumableForm.html', form=form) return redirect(url_for('get_admin_page')) return render_template('createConsumableForm.html', form=form) @app.route('/admin/consumable/delete/', methods=['GET', 'POST']) @login_required def delete_consumable(cid): consumable = Consumable.query.filter(Consumable.id == cid).first() if consumable is None: return redirect(url_for('get_admin_page')) form = DeletConsumableForm() if form.validate_on_submit(): db.session.delete(consumable) db.session.commit() db_log_delete(consumable) return redirect(url_for('get_admin_page')) return render_template('deleteConsumableForm.html', form=form, consumable=consumable) @app.route('/admin/consumable/edit/', methods=['GET', 'POST']) @login_required def edit_consumable(cid): consumable = Consumable.query.filter(Consumable.id == cid).first() if consumable is None: return redirect(url_for('get_admin_page')) form = EditConsumableForm() choices = [(0, ''), (1, 'diesel'), (2, 'e5'), (3, 'e10')] form.ext_id.choices = choices form.name.default = consumable.name form.unit.default = consumable.unit form.ext_id.default = 3 for c in choices: if c[1] == consumable.ext_id: form.ext_id.default = c[0] # preinitialize the defaults with potentially existing values from a try before if form.name.data is not None: form.name.default = form.name.data if form.unit.data is not None: form.unit.default = form.unit.data if form.ext_id.data is not None: form.ext_id.default = form.ext_id.data if form.validate_on_submit(): consumable.name = form.name.data consumable.unit = form.unit.data print(form.ext_id.data) consumable.ext_id = choices[form.ext_id.data][1] print(consumable.ext_id) try: db.session.commit() db_log_update(consumable) except IntegrityError: db.session.rollback() form.name.errors.append('"%s" is not unique.' % (form.name.data)) return render_template('editConsumableForm.html', form=form) return redirect(url_for('get_admin_page')) return render_template('editConsumableForm.html', form=form)