propagate error of connection with the tag server to the view

This commit is contained in:
Cayo Puigdefabregas 2022-03-04 13:32:37 +01:00
parent b737aaf50a
commit d98d09cc7d
2 changed files with 13 additions and 11 deletions

View File

@ -1,8 +1,6 @@
import copy import copy
import json import json
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from requests.exceptions import ConnectionError
from boltons.urlutils import URL from boltons.urlutils import URL
from flask import g, request from flask import g, request
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
@ -446,11 +444,7 @@ class TagUnnamedForm(FlaskForm):
def save(self): def save(self):
num = self.amount.data num = self.amount.data
try: tags_id, _ = g.tag_provider.post('/', {}, query=[('num', num)])
tags_id, _ = g.tag_provider.post('/', {}, query=[('num', num)])
except ConnectionError:
pass
return []
tags = [Tag(id=tag_id, provider=g.inventory.tag_provider) for tag_id in tags_id] tags = [Tag(id=tag_id, provider=g.inventory.tag_provider) for tag_id in tags_id]
db.session.add_all(tags) db.session.add_all(tags)
db.session.commit() db.session.commit()

View File

@ -1,13 +1,15 @@
import csv import csv
import logging
from io import StringIO from io import StringIO
import flask import flask
import flask_weasyprint import flask_weasyprint
from flask import Blueprint, g, make_response, request, url_for from flask import Blueprint, g, make_response, request, url_for, app
from flask.views import View from flask.views import View
from flask_login import current_user, login_required from flask_login import current_user, login_required
from werkzeug.exceptions import NotFound from werkzeug.exceptions import NotFound
from sqlalchemy import or_ from sqlalchemy import or_
from requests.exceptions import ConnectionError
from ereuse_devicehub import messages from ereuse_devicehub import messages
from ereuse_devicehub.inventory.forms import ( from ereuse_devicehub.inventory.forms import (
@ -34,6 +36,8 @@ from ereuse_devicehub.resources.tag.model import Tag
# TODO(@slamora): rename base 'inventory.devices' --> 'inventory' # TODO(@slamora): rename base 'inventory.devices' --> 'inventory'
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
logger = logging.getLogger(__name__)
class DeviceListMix(View): class DeviceListMix(View):
decorators = [login_required] decorators = [login_required]
@ -287,10 +291,14 @@ class TagAddUnnamedView(View):
context = {'page_title': 'New Unnamed Tag', 'lots': lots} context = {'page_title': 'New Unnamed Tag', 'lots': lots}
form = TagUnnamedForm() form = TagUnnamedForm()
if form.validate_on_submit(): if form.validate_on_submit():
tags = form.save() try:
if not tags: form.save()
msg = 'Sorry, the communication with the tag server is not possible now!' 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) messages.error(msg)
next_url = url_for('inventory.devices.taglist') next_url = url_for('inventory.devices.taglist')
return flask.redirect(next_url) return flask.redirect(next_url)