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
|
2022-03-31 12:04:16 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
|
|
|
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)
|
|
|
|
tags = Tag.query.filter(Tag.owner_id == current_user.id).order_by(Tag.id)
|
|
|
|
context = {
|
|
|
|
'lots': lots,
|
|
|
|
'tags': tags,
|
|
|
|
'page_title': 'Tags Management',
|
|
|
|
'version': __version__,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
context = {'page_title': 'New Tag', 'lots': lots, 'version': __version__}
|
|
|
|
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)
|
|
|
|
context = {
|
|
|
|
'page_title': 'New Unnamed Tag',
|
|
|
|
'lots': lots,
|
|
|
|
'version': __version__,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
context = {
|
|
|
|
'lots': lots,
|
|
|
|
'page_title': self.title,
|
|
|
|
'version': __version__,
|
|
|
|
'referrer': request.referrer,
|
|
|
|
}
|
|
|
|
|
|
|
|
form = PrintLabelsForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
context['form'] = form
|
|
|
|
context['tags'] = form._tags
|
|
|
|
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):
|
|
|
|
decorators = [login_required]
|
2022-03-31 14:12:35 +00:00
|
|
|
template_name = 'labels/label_detail.html'
|
2022-03-31 12:04:16 +00:00
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
tag = (
|
|
|
|
Tag.query.filter(Tag.owner_id == current_user.id).filter(Tag.id == id).one()
|
|
|
|
)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'lots': lots,
|
|
|
|
'tag': tag,
|
|
|
|
'page_title': '{} Tag'.format(tag.code),
|
|
|
|
'version': __version__,
|
|
|
|
}
|
|
|
|
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'))
|