From b5df54e5d680e5c4731c2f3f3a384dfe0f627004 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Thu, 20 Jul 2017 09:35:46 +0200
Subject: [PATCH] Fix items table display

---
 app/main/views.py        | 23 ++++++++++++-----------
 app/templates/index.html |  2 ++
 app/utils.py             | 10 ++++++++++
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/app/main/views.py b/app/main/views.py
index 5faf01d..e0b06e9 100644
--- a/app/main/views.py
+++ b/app/main/views.py
@@ -9,10 +9,10 @@ This module implements the main blueprint.
 :license: BSD 2-Clause, see LICENSE for more details.
 
 """
-from flask import (Blueprint, render_template, jsonify)
+from flask import Blueprint, render_template, jsonify
 from flask_login import login_required
 from ..extensions import db
-from ..models import Action, Vendor, Model, Location, Status
+from ..models import Action, Vendor, Model, Location, Status, Item
 from .. import utils
 
 bp = Blueprint('main', __name__)
@@ -39,15 +39,16 @@ def handle_inventory_error(error):
 @bp.route('/_retrieve_items')
 @login_required
 def retrieve_items():
-    sql = """SELECT item.id, item.name, vendor.name, model.name, location.name, status.name
-            FROM item
-            INNER JOIN vendor ON vendor.id = item.vendor_id
-            INNER JOIN model ON model.id = item.model_id
-            INNER JOIN location ON location.id = item.location_id
-            INNER JOIN status ON status.id = item.status_id
-            """
-    result = db.engine.execute(sql)
-    data = [[item for item in row] for row in result]
+    items = Item.query.order_by(Item._created)
+    data = [[item.id,
+             item.serial_number,
+             item.name,
+             utils.format_field(item.vendor),
+             utils.format_field(item.model),
+             utils.format_field(item.location),
+             utils.format_field(item.status),
+             utils.format_field(item._updated),
+             ] for item in items]
     return jsonify(data=data)
 
 
diff --git a/app/templates/index.html b/app/templates/index.html
index 155ad1f..89e97bb 100644
--- a/app/templates/index.html
+++ b/app/templates/index.html
@@ -9,11 +9,13 @@
     <thead>
       <tr>
         <th>Id</th>
+        <th>Serial number</th>
         <th>name</th>
         <th>Vendor</th>
         <th>Model</th>
         <th>Location</th>
         <th>Status</th>
+        <th>Last updated</th>
       </tr>
     </thead>
   </table>
diff --git a/app/utils.py b/app/utils.py
index 40d9f16..dddef7b 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -10,6 +10,7 @@ This module implements utility functions.
 
 """
 import base64
+import datetime
 import io
 import hashlib
 import uuid
@@ -55,3 +56,12 @@ def image_to_base64(img, format='PNG'):
     buf = io.BytesIO()
     img.save(buf, format=format)
     return base64.b64encode(buf.getvalue()).decode('ascii')
+
+
+def format_field(field):
+    """Format the given field to a string or None"""
+    if field is None:
+        return None
+    if isinstance(field, datetime.datetime):
+        return field.strftime('%Y-%m-%d %H:%M')
+    return str(field)
-- 
GitLab