2022-03-31 12:04:16 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import flask
|
|
|
|
from flask import Blueprint, request, url_for
|
|
|
|
from flask.views import View
|
|
|
|
from flask_login import current_user, login_required
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
|
|
|
from ereuse_devicehub import __version__, messages
|
2022-03-31 14:12:35 +00:00
|
|
|
from ereuse_devicehub.labels.forms import PrintLabelsForm, TagForm, TagUnnamedForm
|
2023-04-28 12:45:22 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, ShareLot
|
2022-03-31 12:04:16 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
|
|
|
|
2022-03-31 14:12:35 +00:00
|
|
|
labels = Blueprint('labels', __name__, url_prefix='/labels')
|
2022-03-31 12:04:16 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TagListView(View):
|
|
|
|
methods = ['GET']
|
|
|
|
decorators = [login_required]
|
2022-03-31 14:12:35 +00:00
|
|
|
template_name = 'labels/label_list.html'
|
2022-03-31 12:04:16 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2023-04-28 12:45:22 +00:00
|
|
|
share_lots = ShareLot.query.filter_by(user_to_id=current_user.id)
|
2022-05-12 09:03:48 +00:00
|
|
|
tags = Tag.query.filter(Tag.owner_id == current_user.id).order_by(
|
|
|
|
Tag.created.desc()
|
|
|
|
)
|
2022-03-31 12:04:16 +00:00
|
|
|
context = {
|
|
|
|
'lots': lots,
|
|
|
|
'tags': tags,
|
2022-04-29 09:38:47 +00:00
|
|
|
'page_title': 'Unique Identifiers Management',
|
2022-03-31 12:04:16 +00:00
|
|
|
'version': __version__,
|
2023-04-28 12:45:22 +00:00
|
|
|
'share_lots': share_lots,
|
2022-03-31 12:04:16 +00:00
|
|
|
}
|
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
|
|
|
|
|
|
|
|
class TagAddView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
2022-03-31 14:12:35 +00:00
|
|
|
template_name = 'labels/tag_create.html'
|
2022-03-31 12:04:16 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2023-04-28 12:45:22 +00:00
|
|
|
share_lots = ShareLot.query.filter_by(user_to_id=current_user.id)
|
|
|
|
context = {
|
|
|
|
'page_title': 'New Tag',
|
|
|
|
'lots': lots,
|
|
|
|
'version': __version__,
|
|
|
|
'share_lots': share_lots,
|
|
|
|
}
|
2022-03-31 12:04:16 +00:00
|
|
|
form = TagForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
2022-03-31 14:12:35 +00:00
|
|
|
next_url = url_for('labels.label_list')
|
2022-03-31 12:04:16 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
return flask.render_template(self.template_name, form=form, **context)
|
|
|
|
|
|
|
|
|
|
|
|
class TagAddUnnamedView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
2022-03-31 14:12:35 +00:00
|
|
|
template_name = 'labels/tag_create_unnamed.html'
|
2022-03-31 12:04:16 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2023-04-28 12:45:22 +00:00
|
|
|
share_lots = ShareLot.query.filter_by(user_to_id=current_user.id)
|
2022-03-31 12:04:16 +00:00
|
|
|
context = {
|
|
|
|
'page_title': 'New Unnamed Tag',
|
|
|
|
'lots': lots,
|
|
|
|
'version': __version__,
|
2023-04-28 12:45:22 +00:00
|
|
|
'share_lots': share_lots,
|
2022-03-31 12:04:16 +00:00
|
|
|
}
|
|
|
|
form = TagUnnamedForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
try:
|
|
|
|
form.save()
|
|
|
|
except ConnectionError as e:
|
|
|
|
logger.error(
|
|
|
|
"Error while trying to connect to tag server: {}".format(e)
|
|
|
|
)
|
|
|
|
msg = (
|
|
|
|
"Sorry, we cannot create the unnamed tags requested because "
|
|
|
|
"some error happens while connecting to the tag server!"
|
|
|
|
)
|
|
|
|
messages.error(msg)
|
|
|
|
|
2022-03-31 14:12:35 +00:00
|
|
|
next_url = url_for('labels.label_list')
|
2022-03-31 12:04:16 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
return flask.render_template(self.template_name, form=form, **context)
|
|
|
|
|
|
|
|
|
|
|
|
class PrintLabelsView(View):
|
|
|
|
"""This View is used to print labels from multiple devices"""
|
|
|
|
|
|
|
|
methods = ['POST', 'GET']
|
|
|
|
decorators = [login_required]
|
2022-03-31 14:12:35 +00:00
|
|
|
template_name = 'labels/print_labels.html'
|
2022-03-31 12:04:16 +00:00
|
|
|
title = 'Design and implementation of labels'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2023-04-28 12:45:22 +00:00
|
|
|
share_lots = ShareLot.query.filter_by(user_to_id=current_user.id)
|
2022-03-31 12:04:16 +00:00
|
|
|
context = {
|
|
|
|
'lots': lots,
|
|
|
|
'page_title': self.title,
|
|
|
|
'version': __version__,
|
|
|
|
'referrer': request.referrer,
|
2023-04-28 12:45:22 +00:00
|
|
|
'share_lots': share_lots,
|
2022-03-31 12:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
form = PrintLabelsForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
context['form'] = form
|
2022-04-27 16:55:27 +00:00
|
|
|
context['devices'] = form._devices
|
2022-03-31 12:04:16 +00:00
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
else:
|
|
|
|
messages.error('Error you need select one or more devices')
|
|
|
|
|
|
|
|
next_url = request.referrer or url_for('inventory.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
|
|
|
|
class LabelDetailView(View):
|
2022-09-29 10:20:06 +00:00
|
|
|
"""This View is used to print labels from multiple devices"""
|
|
|
|
|
|
|
|
methods = ['POST', 'GET']
|
2022-03-31 12:04:16 +00:00
|
|
|
decorators = [login_required]
|
2022-09-29 10:20:06 +00:00
|
|
|
template_name = 'labels/print_labels.html'
|
|
|
|
title = 'Design and implementation of labels'
|
2022-03-31 12:04:16 +00:00
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2023-04-28 12:45:22 +00:00
|
|
|
share_lots = ShareLot.query.filter_by(user_to_id=current_user.id)
|
2022-03-31 12:04:16 +00:00
|
|
|
tag = (
|
|
|
|
Tag.query.filter(Tag.owner_id == current_user.id).filter(Tag.id == id).one()
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
'lots': lots,
|
2022-09-29 10:20:06 +00:00
|
|
|
'page_title': self.title,
|
2022-03-31 12:04:16 +00:00
|
|
|
'version': __version__,
|
2022-09-29 10:20:06 +00:00
|
|
|
'referrer': request.referrer,
|
2023-04-28 12:45:22 +00:00
|
|
|
'share_lots': share_lots,
|
2022-03-31 12:04:16 +00:00
|
|
|
}
|
2022-09-29 10:20:06 +00:00
|
|
|
|
|
|
|
devices = []
|
|
|
|
if tag.device:
|
|
|
|
form = PrintLabelsForm(devices=str(tag.device.id))
|
|
|
|
devices = [tag.device]
|
|
|
|
else:
|
|
|
|
form = PrintLabelsForm()
|
|
|
|
|
|
|
|
form._devices = devices
|
|
|
|
context['form'] = form
|
|
|
|
context['devices'] = devices
|
2022-03-31 12:04:16 +00:00
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
|
|
|
|
|
2022-03-31 14:12:35 +00:00
|
|
|
labels.add_url_rule('/', view_func=TagListView.as_view('label_list'))
|
|
|
|
labels.add_url_rule('/add/', view_func=TagAddView.as_view('tag_add'))
|
|
|
|
labels.add_url_rule(
|
2022-03-31 12:04:16 +00:00
|
|
|
'/unnamed/add/', view_func=TagAddUnnamedView.as_view('tag_unnamed_add')
|
|
|
|
)
|
2022-03-31 14:12:35 +00:00
|
|
|
labels.add_url_rule(
|
2022-03-31 12:04:16 +00:00
|
|
|
'/print',
|
|
|
|
view_func=PrintLabelsView.as_view('print_labels'),
|
|
|
|
)
|
2022-03-31 14:12:35 +00:00
|
|
|
labels.add_url_rule('/<string:id>/', view_func=LabelDetailView.as_view('label_details'))
|