This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/labels/views.py

145 lines
4.6 KiB
Python
Raw Normal View History

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
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')
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'
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.created.desc()
)
context = {
'lots': lots,
'tags': tags,
2022-04-29 09:38:47 +00:00
'page_title': 'Unique Identifiers 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'
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')
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'
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')
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'
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
2022-04-27 16:55:27 +00:00
context['devices'] = form._devices
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'
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(
'/unnamed/add/', view_func=TagAddUnnamedView.as_view('tag_unnamed_add')
)
2022-03-31 14:12:35 +00:00
labels.add_url_rule(
'/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'))