From 06693cea53f337a5f0486954c1f6d054868cb1f3 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Fri, 1 Dec 2017 23:52:33 +0100 Subject: [PATCH] Move attributes creation to its own page --- app/main/forms.py | 2 +- app/main/views.py | 22 +++++++++---------- app/static/js/{qrcodes.js => attributes.js} | 6 ++--- .../{create_qrcodes.html => attributes.html} | 14 ++++++++---- app/templates/base.html | 11 ++-------- 5 files changed, 27 insertions(+), 28 deletions(-) rename app/static/js/{qrcodes.js => attributes.js} (63%) rename app/templates/{create_qrcodes.html => attributes.html} (63%) diff --git a/app/main/forms.py b/app/main/forms.py index 20549c2..c020d56 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 3dfd313..5c3d708 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 0102a00..ea7ff61 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 9b535d8..dd6f972 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 4b1e84d..df8eea2 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 %} -- GitLab