32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
from flask import Blueprint, request, render_template, redirect
|
|
from models import Asset, db
|
|
from config import item_attributes
|
|
|
|
addasset_bp = Blueprint('addasset', __name__)
|
|
|
|
@addasset_bp.route('/create/', methods=['GET', 'POST'])
|
|
def create():
|
|
if request.method == 'GET':
|
|
# Render the "add item" form
|
|
return render_template('create.html', item_attributes=item_attributes)
|
|
|
|
# Process submitted form
|
|
if request.method == 'POST':
|
|
# Get data from form
|
|
form_data = {attrib: request.form[attrib] for attrib in item_attributes}
|
|
try:
|
|
primary_attr = next((attrib_name for attrib_name, attrib in item_attributes.items() if attrib.primary), None)
|
|
except ValueError:
|
|
return render_template('create.html', exc=primary_attr)
|
|
|
|
item = Asset(**form_data)
|
|
|
|
try:
|
|
db.session.add(item)
|
|
db.session.commit()
|
|
except exc.IntegrityError:
|
|
return render_template('create.html', exc='integrity')
|
|
except exc.StatementError:
|
|
return render_template('create.html', exc='status')
|
|
|
|
return redirect('/viewall') |