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

Add QR Codes page

parent 5f41066b
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ This module implements the main blueprint. ...@@ -12,6 +12,7 @@ This module implements the main blueprint.
from flask import (Blueprint, render_template, jsonify) from flask import (Blueprint, render_template, jsonify)
from flask_login import login_required from flask_login import login_required
from ..extensions import db from ..extensions import db
from ..models import Action, Vendor, Model, Location, Status
from .. import utils from .. import utils
bp = Blueprint('main', __name__) bp = Blueprint('main', __name__)
...@@ -54,3 +55,16 @@ def retrieve_items(): ...@@ -54,3 +55,16 @@ def retrieve_items():
@login_required @login_required
def index(): def index():
return render_template('index.html') return render_template('index.html')
@bp.route('/qrcodes')
@login_required
def qrcodes():
codes = {}
for model in (Action, Vendor, Model, Location, Status):
items = db.session.query(model).order_by(model.name)
images = [{'name': item.name,
'data': utils.image_to_base64(item.image())}
for item in items]
codes[model.__name__] = images
return render_template('qrcodes.html', codes=codes)
{% macro nav_li(active) -%}
<li {% if active %}class="active"{% endif %}>
{%- endmacro %}
{%- extends "bootstrap/base.html" %} {%- extends "bootstrap/base.html" %}
{% import "bootstrap/utils.html" as utils %} {% import "bootstrap/utils.html" as utils %}
{% from "_helpers.html" import nav_li %}
{% block head %} {% block head %}
<meta charset="utf-8"> <meta charset="utf-8">
...@@ -32,6 +33,8 @@ ...@@ -32,6 +33,8 @@
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
{% set path = request.path %} {% set path = request.path %}
{{ nav_li(path in ("/", "/index", "/index/")) }}<a href="{{ url_for('main.index') }}">Index</a></li>
{{ nav_li(path == "/qrcodes") }}<a href="{{ url_for('main.qrcodes') }}">QR Codes</a></li>
{% if current_user.is_authenticated and current_user.is_admin %} {% if current_user.is_authenticated and current_user.is_admin %}
<li><a href="{{ url_for('admin.index') }}">Admin</a></li> <li><a href="{{ url_for('admin.index') }}">Admin</a></li>
{% endif %} {% endif %}
......
{%- extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}QR Codes{% endblock %}
{% block main %}
{% for name, images in codes.items() %}
<div class="row">
<h2>{{ name }} codes</h2>
{% for image in images %}
<div class="col-md-3">
<div class="thumbnail">
<img src="data:image/png;base64,{{ image.data }}"/>
<div class="caption">
<h3>{{ image.name }}</h3>
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
{%- endblock %}
...@@ -9,6 +9,8 @@ This module implements utility functions. ...@@ -9,6 +9,8 @@ This module implements utility functions.
:license: BSD 2-Clause, see LICENSE for more details. :license: BSD 2-Clause, see LICENSE for more details.
""" """
import base64
import io
import hashlib import hashlib
import uuid import uuid
...@@ -39,3 +41,15 @@ class InventoryError(Exception): ...@@ -39,3 +41,15 @@ class InventoryError(Exception):
def compute_hash(string): def compute_hash(string):
md5 = hashlib.md5(string.encode()).hexdigest() md5 = hashlib.md5(string.encode()).hexdigest()
return uuid.UUID(md5) return uuid.UUID(md5)
def image_to_base64(img, format='PNG'):
"""Convert a Pillow image to a base64 string
:param img: Pillow image
:param format: format of the image to use
:returns str: image as base64 string
"""
buf = io.BytesIO()
img.save(buf, format=format)
return base64.b64encode(buf.getvalue()).decode('ascii')
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