From a47c2c0b4edf2f0905d459dd33c295558569f8e6 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Sun, 17 Sep 2017 22:12:39 +0200
Subject: [PATCH] Allow to retrieve QRCodes via API

Pass ?qrcode=true as query string
---
 app/api/main.py              | 19 +++++++++++++------
 app/models.py                | 13 +++++++++----
 tests/functional/test_api.py |  8 ++++++++
 3 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/app/api/main.py b/app/api/main.py
index e2c3f31..42b2fb1 100644
--- a/app/api/main.py
+++ b/app/api/main.py
@@ -35,9 +35,16 @@ def get_item_by_id_or_ics_id(id_):
     return item
 
 
-def get_generic_model(model):
+def get_generic_model(model, args):
+    """Return data from model as json
+
+    :param model: model class
+    :param MultiDict args: args from the request
+    :returns: data from model as json
+    """
     items = model.query.order_by(model.name)
-    data = [item.to_dict() for item in items]
+    qrcode = args.get('qrcode', 'false').lower() == 'true'
+    data = [item.to_dict(qrcode=qrcode) for item in items]
     return jsonify(data)
 
 
@@ -170,7 +177,7 @@ def patch_item(id_):
 @bp.route('/manufacturers')
 @jwt_required
 def get_manufacturers():
-    return get_generic_model(Manufacturer)
+    return get_generic_model(Manufacturer, request.args)
 
 
 @bp.route('/manufacturers', methods=['POST'])
@@ -183,7 +190,7 @@ def create_manufacturer():
 @bp.route('/models')
 @jwt_required
 def get_models():
-    return get_generic_model(Model)
+    return get_generic_model(Model, request.args)
 
 
 @bp.route('/models', methods=['POST'])
@@ -196,7 +203,7 @@ def create_model():
 @bp.route('/locations')
 @jwt_required
 def get_locations():
-    return get_generic_model(Location)
+    return get_generic_model(Location, request.args)
 
 
 @bp.route('/locations', methods=['POST'])
@@ -209,7 +216,7 @@ def create_locations():
 @bp.route('/status')
 @jwt_required
 def get_status():
-    return get_generic_model(Status)
+    return get_generic_model(Status, request.args)
 
 
 @bp.route('/status', methods=['POST'])
diff --git a/app/models.py b/app/models.py
index f704763..8466c0c 100644
--- a/app/models.py
+++ b/app/models.py
@@ -158,8 +158,11 @@ class QRCodeMixin:
     def __str__(self):
         return self.name
 
-    def to_dict(self):
-        return {'id': self.id, 'name': self.name}
+    def to_dict(self, qrcode=False):
+        d = {'id': self.id, 'name': self.name}
+        if qrcode:
+            d['qrcode'] = utils.image_to_base64(self.image())
+        return d
 
 
 class Action(QRCodeMixin, db.Model):
@@ -174,8 +177,10 @@ class Model(QRCodeMixin, db.Model):
     description = db.Column(db.Text)
     items = db.relationship('Item', back_populates='model')
 
-    def to_dict(self):
-        return {'id': self.id, 'name': self.name, 'description': self.description}
+    def to_dict(self, qrcode=False):
+        d = super().to_dict(qrcode)
+        d['description'] = self.description
+        return d
 
 
 class Location(QRCodeMixin, db.Model):
diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py
index 396dc55..c1e828b 100644
--- a/tests/functional/test_api.py
+++ b/tests/functional/test_api.py
@@ -136,6 +136,14 @@ def test_get_generic_model(endpoint, session, client, readonly_token):
     check_response_message(response, 'Not enough segments', 422)
     response = get(client, f'/api/{endpoint}', readonly_token)
     check_names(response, names)
+    response = get(client, f'/api/{endpoint}?qrcode=true', readonly_token)
+    check_names(response, names)
+    for item in response.json:
+        assert 'qrcode' in item
+    response = get(client, f'/api/{endpoint}?qrcode=false', readonly_token)
+    check_names(response, names)
+    for item in response.json:
+        assert 'qrcode' not in item
 
 
 @pytest.mark.parametrize('endpoint', ENDPOINTS)
-- 
GitLab