diff --git a/app/main/forms.py b/app/main/forms.py index 20549c20e1f0b860339454c058731178c9b585fb..c020d56593a59a820f6bfbd13229d767befbb3ff 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -26,7 +26,7 @@ class NoValidateSelectField(SelectField): pass -class QRCodeForm(FlaskForm): +class AttributeForm(FlaskForm): kind = SelectField('Kind') name = StringField('name', validators=[validators.DataRequired()]) diff --git a/app/main/views.py b/app/main/views.py index 3dfd3132611c6cfb59ce60cf0e0dd8d4f6976846..5c3d708fe2ee8ccd37d0122ff1ba9475dcb96452 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -13,7 +13,7 @@ import sqlalchemy as sa from flask import (Blueprint, render_template, jsonify, redirect, url_for, request, flash, current_app) from flask_login import login_required -from .forms import QRCodeForm, HostForm, ItemForm +from .forms import AttributeForm, HostForm, ItemForm from ..extensions import db from ..decorators import login_groups_accepted from .. import utils, models @@ -115,11 +115,11 @@ def qrcodes(): return render_template('qrcodes.html', codes=codes) -@bp.route('/qrcodes/create', methods=('GET', 'POST')) +@bp.route('/attributes', methods=('GET', 'POST')) @login_groups_accepted('admin', 'create') -def create_qrcodes(): +def attributes(): kind = request.args.get('kind', 'Manufacturer') - form = QRCodeForm(request.form, kind=kind) + form = AttributeForm(request.form, kind=kind) if form.validate_on_submit(): model = globals()[form.kind.data] new_model = model(name=form.name.data) @@ -128,17 +128,17 @@ def create_qrcodes(): db.session.commit() except sa.exc.IntegrityError as e: db.session.rollback() - flash(f'{form.name.data} already exists! Item not created.', 'error') - return redirect(url_for('main.create_qrcodes', kind=form.kind.data)) - return render_template('create_qrcodes.html', form=form) + flash(f'{form.name.data} already exists! Attribute not created.', 'error') + return redirect(url_for('main.attributes', kind=form.kind.data)) + return render_template('attributes.html', form=form) -@bp.route('/_retrieve_qrcodes_name/<kind>') +@bp.route('/_retrieve_attributes_name/<kind>') @login_required -def retrieve_qrcodes_name(kind): +def retrieve_attributes_name(kind): try: - model = globals()[kind] - except KeyError: + model = getattr(models, kind) + except AttributeError: raise utils.CSEntryError(f"Unknown model '{kind}'", status_code=422) items = db.session.query(model).order_by(model.name) data = [[item.name] for item in items] diff --git a/app/static/js/qrcodes.js b/app/static/js/attributes.js similarity index 63% rename from app/static/js/qrcodes.js rename to app/static/js/attributes.js index 0102a00c83e739993659de4d2d21aeb59a645241..ea7ff616b7e354b0071cdcad7daeec0e210f2b35 100644 --- a/app/static/js/qrcodes.js +++ b/app/static/js/attributes.js @@ -1,10 +1,10 @@ $(document).ready(function() { - var qrcodes_table = $("#qrcodes_table").DataTable({ + var attributes_table = $("#attributes_table").DataTable({ "ajax": function(data, callback, settings) { var kind = $("#kind").val(); $.getJSON( - $SCRIPT_ROOT + "/_retrieve_qrcodes_name/" + kind, + $SCRIPT_ROOT + "/_retrieve_attributes_name/" + kind, function(json) { callback(json); }); @@ -13,7 +13,7 @@ $(document).ready(function() { }); $("#kind").on('change', function() { - qrcodes_table.ajax.reload(); + attributes_table.ajax.reload(); }); }); diff --git a/app/templates/create_qrcodes.html b/app/templates/attributes.html similarity index 63% rename from app/templates/create_qrcodes.html rename to app/templates/attributes.html index 9b535d8ca596fc38221ff231ebff4413eefb0fe1..dd6f9724486df2dfd632e2e95334ca710ef62b6b 100644 --- a/app/templates/create_qrcodes.html +++ b/app/templates/attributes.html @@ -1,10 +1,16 @@ {%- extends "base.html" %} {% import "bootstrap/wtf.html" as wtf %} -{% block title %}Create QR Codes - CSEntry{% endblock %} +{% block title %}Attributes - CSEntry{% endblock %} {% block main %} - <h2>Create QR Codes</h2> + <ul class="nav nav-tabs"> + <li class="nav-item"> + <a class="nav-link active" href="{{ url_for('main.attributes') }}">Attributes</a> + </li> + </ul> + + <br> <form method="POST" class="form-inline"> {{ form.csrf_token }} @@ -20,7 +26,7 @@ <hr class="separator"> - <table id="qrcodes_table" class="table table-bordered table-hover table-sm"> + <table id="attributes_table" class="table table-bordered table-hover table-sm"> <thead> <tr> <th>Name</th> @@ -30,5 +36,5 @@ {%- endblock %} {% block csentry_scripts %} - <script src="{{ url_for('static', filename='js/qrcodes.js') }}"></script> + <script src="{{ url_for('static', filename='js/attributes.js') }}"></script> {% endblock %} diff --git a/app/templates/base.html b/app/templates/base.html index 4b1e84d7526cb01723b0fedee8ee3fa5c337dec1..df8eea2d0b3147f2d3e888a16cc36d940c8e031e 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -25,15 +25,8 @@ {% set path = request.path %} <a class="nav-item nav-link {{ is_active(path.startswith("/items")) }}" href="{{ url_for('main.list_items') }}">Items</a> <a class="nav-item nav-link {{ is_active(path.startswith("/hosts")) }}" href="{{ url_for('main.list_hosts') }}">Hosts</a> - <div class="dropdown {{ is_active(path.startswith("/qrcodes")) }}"> - <a class="nav-link dropdown-toggle" href="#" id="qrcodesDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> - QR Codes - </a> - <div class="dropdown-menu" aria-labelledby="qrcodesDropdownMenuLink"> - <a class="dropdown-item" href="{{ url_for('main.qrcodes') }}">View</a> - <a class="dropdown-item" href="{{ url_for('main.create_qrcodes') }}">Create</a> - </div> - </div> + <a class="nav-item nav-link {{ is_active(path.startswith("/attributes")) }}" href="{{ url_for('main.attributes') }}">Attributes</a> + <a class="nav-item nav-link {{ is_active(path.startswith("/qrcodes")) }}" href="{{ url_for('main.qrcodes') }}">QR Codes</a> {% if current_user.is_authenticated and current_user.is_admin %} <a class="nav-item nav-link" href="{{ url_for('admin.index') }}">Admin</a> {% endif %}