From d0dfa712fdebe5b2cf5c476e72b06454ebdea3a8 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Sun, 11 Feb 2018 16:36:48 +0100
Subject: [PATCH] Move QR codes to attributes table

- Remove QRcodes page
- Add Status to attributes page (only allow admin to add new statuses)

Actions QR Codes not visible anymore. Required ones shall be put on the
page where they are used.
---
 app/inventory/views.py                  | 20 +++---------------
 app/static/js/attributes.js             | 14 ++++++++++++-
 app/templates/base-fluid.html           |  2 --
 app/templates/inventory/attributes.html | 25 ++++++++++++----------
 app/templates/inventory/qrcodes.html    | 28 -------------------------
 5 files changed, 30 insertions(+), 59 deletions(-)
 delete mode 100644 app/templates/inventory/qrcodes.html

diff --git a/app/inventory/views.py b/app/inventory/views.py
index 77ec12e..9a4692e 100644
--- a/app/inventory/views.py
+++ b/app/inventory/views.py
@@ -201,20 +201,6 @@ def edit_item(ics_id):
     return render_template('inventory/edit_item.html', form=form)
 
 
-@bp.route('/qrcodes/<kind>')
-@login_required
-def qrcodes(kind='Action'):
-    try:
-        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)
-    images = [{'name': item.name,
-               'data': item.base64_image()}
-              for item in items]
-    return render_template('inventory/qrcodes.html', kind=kind, images=images)
-
-
 @bp.route('/attributes/<kind>', methods=('GET', 'POST'))
 @login_groups_accepted('admin', 'create')
 def attributes(kind):
@@ -235,15 +221,15 @@ def attributes(kind):
     return render_template('inventory/attributes.html', kind=kind, form=form)
 
 
-@bp.route('/_retrieve_attributes_name/<kind>')
+@bp.route('/_retrieve_attributes/<kind>')
 @login_required
-def retrieve_attributes_name(kind):
+def retrieve_attributes(kind):
     try:
         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, item.description) for item in items]
+    data = [(item.base64_image(), item.name, item.description) for item in items]
     return jsonify(data=data)
 
 
diff --git a/app/static/js/attributes.js b/app/static/js/attributes.js
index d2aadc0..bfbf354 100644
--- a/app/static/js/attributes.js
+++ b/app/static/js/attributes.js
@@ -4,11 +4,23 @@ $(document).ready(function() {
     "ajax": function(data, callback, settings) {
       var kind = $('li a.nav-link.active').text();
       $.getJSON(
-        $SCRIPT_ROOT + "/inventory/_retrieve_attributes_name/" + kind,
+        $SCRIPT_ROOT + "/inventory/_retrieve_attributes/" + kind,
         function(json) {
           callback(json);
         });
     },
+    "order": [[1, 'asc']],
+    "columnDefs": [
+      {
+        "targets": [0],
+        "orderable": false,
+        "render": function(data, type, row) {
+          // render QR code from base64 string
+          return '<img class="img-fluid" src="data:image/png;base64,' + data + '">';
+        },
+        "width": "6%",
+      }
+    ],
     "paging": false
   });
 
diff --git a/app/templates/base-fluid.html b/app/templates/base-fluid.html
index 509133e..b425f06 100644
--- a/app/templates/base-fluid.html
+++ b/app/templates/base-fluid.html
@@ -14,8 +14,6 @@
           href="{{ url_for('inventory.list_items') }}">Items</a>
         <a class="list-group-item list-group-item-action {{ is_active(path.startswith("/inventory/attributes")) }}"
           href="{{ url_for('inventory.attributes', kind='Manufacturer') }}">Attributes</a>
-        <a class="list-group-item list-group-item-action {{ is_active(path.startswith("/inventory/qrcodes")) }}"
-          href="{{ url_for('inventory.qrcodes', kind='Action') }}">QR Codes</a>
         <a class="list-group-item list-group-item-action {{ is_active(path.startswith("/inventory/scanner")) }}"
           href="{{ url_for('inventory.scanner') }}">Scanner setup</a>
         {% elif path.startswith("/network") %}
diff --git a/app/templates/inventory/attributes.html b/app/templates/inventory/attributes.html
index 03501ee..6a9d453 100644
--- a/app/templates/inventory/attributes.html
+++ b/app/templates/inventory/attributes.html
@@ -5,7 +5,7 @@
 
 {% block main %}
   <ul class="nav nav-tabs">
-    {% for attribute in ('Manufacturer', 'Model', 'Location') %}
+    {% for attribute in ('Manufacturer', 'Model', 'Location', 'Status') %}
     <li class="nav-item">
       <a class="nav-link {% if attribute == kind %}active{% endif %}" href="{{ url_for('inventory.attributes', kind=attribute) }}">{{ attribute }}</a>
     </li>
@@ -14,22 +14,25 @@
 
   <br>
 
-  <form id="attributeForm" method="POST">
-    {{ form.hidden_tag() }}
-    {{ render_field(form.name) }}
-    {{ render_field(form.description) }}
-    <div class="form-group row">
-      <div class="col-sm-10">
-        <button type="submit" class="btn btn-primary">Create</button>
+  {% if kind in ('Manufacturer', 'Model', 'Location') or (current_user.is_authenticated and current_user.is_admin) %}
+    <form id="attributeForm" method="POST">
+      {{ form.hidden_tag() }}
+      {{ render_field(form.name) }}
+      {{ render_field(form.description) }}
+      <div class="form-group row">
+        <div class="col-sm-10">
+          <button type="submit" class="btn btn-primary">Create</button>
+        </div>
       </div>
-    </div>
-  </form>
+    </form>
 
-  <hr class="separator">
+    <hr class="separator">
+  {%- endif %}
 
   <table id="attributes_table" class="table table-bordered table-hover table-sm" cellspacing="0" width="100%">
     <thead>
       <tr>
+        <th></th>
         <th>Name</th>
         <th>Description</th>
       </tr>
diff --git a/app/templates/inventory/qrcodes.html b/app/templates/inventory/qrcodes.html
deleted file mode 100644
index 606f135..0000000
--- a/app/templates/inventory/qrcodes.html
+++ /dev/null
@@ -1,28 +0,0 @@
-{% extends "base-fluid.html" %}
-
-{% block title %}QR Codes - CSEntry{% endblock %}
-
-{% block main %}
-  <ul class="nav nav-tabs">
-    {% for item in ('Action', 'Manufacturer', 'Model', 'Location', 'Status') %}
-    <li class="nav-item">
-      <a class="nav-link {% if kind == item %}active{% endif %}" href="{{ url_for('inventory.qrcodes', kind=item) }}">{{ item }}</a>
-    </li>
-    {% endfor %}
-  </ul>
-
-  <br>
-
-  <div class="row">
-    {% for image in images %}
-    <div class="col-md-3">
-      <div class="card">
-        <img class="card-img-top" src="data:image/png;base64,{{ image.data }}">
-        <div class="card-body">
-          <h4 class="card-title">{{ image.name }}</h4>
-        </div>
-      </div>
-    </div>
-    {% endfor %}
-  </div>
-{%- endblock %}
-- 
GitLab