Skip to content
Snippets Groups Projects
Commit a47c2c0b authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Allow to retrieve QRCodes via API

Pass ?qrcode=true as query string
parent 75e1752a
No related branches found
No related tags found
No related merge requests found
...@@ -35,9 +35,16 @@ def get_item_by_id_or_ics_id(id_): ...@@ -35,9 +35,16 @@ def get_item_by_id_or_ics_id(id_):
return item 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) 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) return jsonify(data)
...@@ -170,7 +177,7 @@ def patch_item(id_): ...@@ -170,7 +177,7 @@ def patch_item(id_):
@bp.route('/manufacturers') @bp.route('/manufacturers')
@jwt_required @jwt_required
def get_manufacturers(): def get_manufacturers():
return get_generic_model(Manufacturer) return get_generic_model(Manufacturer, request.args)
@bp.route('/manufacturers', methods=['POST']) @bp.route('/manufacturers', methods=['POST'])
...@@ -183,7 +190,7 @@ def create_manufacturer(): ...@@ -183,7 +190,7 @@ def create_manufacturer():
@bp.route('/models') @bp.route('/models')
@jwt_required @jwt_required
def get_models(): def get_models():
return get_generic_model(Model) return get_generic_model(Model, request.args)
@bp.route('/models', methods=['POST']) @bp.route('/models', methods=['POST'])
...@@ -196,7 +203,7 @@ def create_model(): ...@@ -196,7 +203,7 @@ def create_model():
@bp.route('/locations') @bp.route('/locations')
@jwt_required @jwt_required
def get_locations(): def get_locations():
return get_generic_model(Location) return get_generic_model(Location, request.args)
@bp.route('/locations', methods=['POST']) @bp.route('/locations', methods=['POST'])
...@@ -209,7 +216,7 @@ def create_locations(): ...@@ -209,7 +216,7 @@ def create_locations():
@bp.route('/status') @bp.route('/status')
@jwt_required @jwt_required
def get_status(): def get_status():
return get_generic_model(Status) return get_generic_model(Status, request.args)
@bp.route('/status', methods=['POST']) @bp.route('/status', methods=['POST'])
......
...@@ -158,8 +158,11 @@ class QRCodeMixin: ...@@ -158,8 +158,11 @@ class QRCodeMixin:
def __str__(self): def __str__(self):
return self.name return self.name
def to_dict(self): def to_dict(self, qrcode=False):
return {'id': self.id, 'name': self.name} d = {'id': self.id, 'name': self.name}
if qrcode:
d['qrcode'] = utils.image_to_base64(self.image())
return d
class Action(QRCodeMixin, db.Model): class Action(QRCodeMixin, db.Model):
...@@ -174,8 +177,10 @@ class Model(QRCodeMixin, db.Model): ...@@ -174,8 +177,10 @@ class Model(QRCodeMixin, db.Model):
description = db.Column(db.Text) description = db.Column(db.Text)
items = db.relationship('Item', back_populates='model') items = db.relationship('Item', back_populates='model')
def to_dict(self): def to_dict(self, qrcode=False):
return {'id': self.id, 'name': self.name, 'description': self.description} d = super().to_dict(qrcode)
d['description'] = self.description
return d
class Location(QRCodeMixin, db.Model): class Location(QRCodeMixin, db.Model):
......
...@@ -136,6 +136,14 @@ def test_get_generic_model(endpoint, session, client, readonly_token): ...@@ -136,6 +136,14 @@ def test_get_generic_model(endpoint, session, client, readonly_token):
check_response_message(response, 'Not enough segments', 422) check_response_message(response, 'Not enough segments', 422)
response = get(client, f'/api/{endpoint}', readonly_token) response = get(client, f'/api/{endpoint}', readonly_token)
check_names(response, names) 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) @pytest.mark.parametrize('endpoint', ENDPOINTS)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment