From 34843baaec0d321bb5435d5ba8934a208e2b8fe6 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Wed, 6 Sep 2017 09:49:36 +0200
Subject: [PATCH] Add view item page

---
 app/main/views.py            |  7 +++++++
 app/static/js/inventory.js   | 11 +++++++++++
 app/templates/_helpers.html  | 14 ++++++++++++++
 app/templates/view_item.html | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 app/templates/view_item.html

diff --git a/app/main/views.py b/app/main/views.py
index f5f77c6..9348ba4 100644
--- a/app/main/views.py
+++ b/app/main/views.py
@@ -63,6 +63,13 @@ def index():
     return render_template('index.html')
 
 
+@bp.route('/view/<ics_id>')
+@login_required
+def view_item(ics_id):
+    item = Item.query.filter_by(ics_id=ics_id).first_or_404()
+    return render_template('view_item.html', item=item)
+
+
 @bp.route('/qrcodes')
 @login_required
 def qrcodes():
diff --git a/app/static/js/inventory.js b/app/static/js/inventory.js
index de9f8d4..dbd50eb 100644
--- a/app/static/js/inventory.js
+++ b/app/static/js/inventory.js
@@ -17,6 +17,17 @@ $(document).ready(function() {
         "targets": [0],
         "visible": false,
         "searchable": false
+      },
+      {
+        "targets": [1],
+        "render": function(data, type, row) {
+          // render funtion to create link to Item view page
+          if ( data === null ) {
+            return data;
+          }
+          var url = $SCRIPT_ROOT + "/view/" + data;
+          return '<a href="'+ url + '">' + data + '</a>';
+        }
       }
     ]
   });
diff --git a/app/templates/_helpers.html b/app/templates/_helpers.html
index 9580474..ac555ca 100644
--- a/app/templates/_helpers.html
+++ b/app/templates/_helpers.html
@@ -1,3 +1,17 @@
 {% macro is_active(active) -%}
 {% if active %}active{% endif %}
 {%- endmacro %}
+
+{% macro link_to_item(item) -%}
+  {% if item %}
+    <a href="{{ url_for('main.view_item', ics_id=item.ics_id) }}">{{ item.ics_id }}</a>
+  {% else %}
+    {{ item }}
+  {% endif %}
+{%- endmacro %}
+
+{% macro link_to_children(item) -%}
+  {% for child in item.children %}
+    {{ link_to_item(child) }}
+  {% endfor %}
+{%- endmacro %}
diff --git a/app/templates/view_item.html b/app/templates/view_item.html
new file mode 100644
index 0000000..3892656
--- /dev/null
+++ b/app/templates/view_item.html
@@ -0,0 +1,32 @@
+{%- extends "base.html" %}
+{% from "_helpers.html" import link_to_item, link_to_children %}
+
+{% block title %}View Item{% endblock %}
+
+{% block main %}
+  <h2>Item {{ item.ics_id }}</h2>
+
+  <dl class="row">
+    <dt class="col-sm-3">ICS id</dt>
+    <dd class="col-sm-9">{{ item.ics_id }}</dt>
+    <dt class="col-sm-3">Created</dt>
+    <dd class="col-sm-9">{{ item._created.strftime("%Y-%m-%d %H:%M") }}</dt>
+    <dt class="col-sm-3">Updated</dt>
+    <dd class="col-sm-9">{{ item._updated.strftime("%Y-%m-%d %H:%M") }}</dt>
+    <dt class="col-sm-3">Serial number</dt>
+    <dd class="col-sm-9">{{ item.serial_number }}</dt>
+    <dt class="col-sm-3">Manufacturer</dt>
+    <dd class="col-sm-9">{{ item.manufacturer }}</dt>
+    <dt class="col-sm-3">Model</dt>
+    <dd class="col-sm-9">{{ item.model }}</dt>
+    <dt class="col-sm-3">Location</dt>
+    <dd class="col-sm-9">{{ item.location }}</dt>
+    <dt class="col-sm-3">Status</dt>
+    <dd class="col-sm-9">{{ item.status }}</dt>
+    <dt class="col-sm-3">Parent</dt>
+    <dd class="col-sm-9">{{ link_to_item(item.parent) }}</dt>
+    <dt class="col-sm-3">Children</dt>
+    <dd class="col-sm-9">{{ link_to_children(item) }}</dt>
+  </dl>
+
+{%- endblock %}
-- 
GitLab