diff --git a/.github/workflows/flask.yml b/.github/workflows/flask.yml index ccc4a798..151cc26d 100644 --- a/.github/workflows/flask.yml +++ b/.github/workflows/flask.yml @@ -37,9 +37,10 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 + uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} + cache: 'pip' - name: Install dependencies run: | diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index c9b69091..01bbe1db 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1,10 +1,9 @@ -import datetime - import flask from flask import Blueprint, request, url_for from flask.views import View from flask_login import current_user, login_required +from ereuse_devicehub import messages from ereuse_devicehub.inventory.forms import (AllocateForm, LotDeviceForm, LotForm, NewActionForm, NewDeviceForm, TagDeviceForm, @@ -294,31 +293,42 @@ class TagUnlinkDeviceView(View): class NewActionView(View): methods = ['POST'] decorators = [login_required] - _form = NewActionForm + form_class = NewActionForm def dispatch_request(self): - self.form = self._form() - - next_url = url_for('inventory.devices.devicelist') - lot_id = self.form.lot.data - if lot_id: - next_url = url_for('inventory.devices.lotdevicelist', lot_id=lot_id) + self.form = self.form_class() if self.form.validate_on_submit(): - self.form.save() + instance = self.form.save() + messages.success('Action "{}" created successfully!'.format(instance.type)) + + next_url = self.get_next_url() return flask.redirect(next_url) + def get_next_url(self): + lot_id = self.form.lot.data + + if lot_id: + return url_for('inventory.devices.lotdevicelist', lot_id=lot_id) + + return url_for('inventory.devices.devicelist') + class NewAllocateView(NewActionView, DeviceListMix): methods = ['POST'] - _form = AllocateForm + form_class = AllocateForm def dispatch_request(self): - lot_id = self.form.lot.data - dispatch = super().dispatch_request() - if dispatch: - return dispatch + self.form = self.form_class() + if self.form.validate_on_submit(): + instance = self.form.save() + messages.success('Action "{}" created successfully!'.format(instance.type)) + + next_url = self.get_next_url() + return flask.redirect(next_url) + + lot_id = self.form.lot.data self.get_context(lot_id) self.context['form_new_allocate'] = self.form return flask.render_template(self.template_name, **self.context) diff --git a/ereuse_devicehub/messages.py b/ereuse_devicehub/messages.py new file mode 100644 index 00000000..ba3bf089 --- /dev/null +++ b/ereuse_devicehub/messages.py @@ -0,0 +1,64 @@ +from flask import flash, session + +DEBUG = 10 +INFO = 20 +SUCCESS = 25 +WARNING = 30 +ERROR = 40 + +DEFAULT_LEVELS = { + 'DEBUG': DEBUG, + 'INFO': INFO, + 'SUCCESS': SUCCESS, + 'WARNING': WARNING, + 'ERROR': ERROR, +} + +DEFAULT_TAGS = { + DEBUG: 'light', + INFO: 'info', + SUCCESS: 'success', + WARNING: 'warning', + ERROR: 'danger', +} + +DEFAULT_ICONS = { + DEFAULT_TAGS[DEBUG]: 'tools', + DEFAULT_TAGS[INFO]: 'info-circle', + DEFAULT_TAGS[SUCCESS]: 'check-circle', + DEFAULT_TAGS[WARNING]: 'exclamation-triangle', + DEFAULT_TAGS[ERROR]: 'exclamation-octagon', +} + + +def add_message(level, message): + level_tag = DEFAULT_TAGS[level] + if '_message_icon' not in session: + session['_message_icon'] = DEFAULT_ICONS + + flash(message, level_tag) + + +def debug(message): + """Add a message with the ``DEBUG`` level.""" + add_message(DEBUG, message) + + +def info(message): + """Add a message with the ``INFO`` level.""" + add_message(INFO, message) + + +def success(message): + """Add a message with the ``SUCCESS`` level.""" + add_message(SUCCESS, message) + + +def warning(message): + """Add a message with the ``WARNING`` level.""" + add_message(WARNING, message) + + +def error(message): + """Add a message with the ``ERROR`` level.""" + add_message(ERROR, message) diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index 99cf0cf5..5bd1994c 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html @@ -175,6 +175,15 @@
+ {% block messages %} + {% for level, message in get_flashed_messages(with_categories=true) %} + + {% endfor %} + {% endblock %} {% block main %} {% endblock main %} diff --git a/requirements.txt b/requirements.txt index e5b35869..cacf8ae3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ colorama==0.3.9 colour==0.1.5 ereuse-utils[naming,test,session,cli]==0.4.0b49 Flask==1.0.2 -Flask-Cors==3.0.6 +Flask-Cors==3.0.10 Flask-Login==0.5.0 Flask-SQLAlchemy==2.3.2 Flask-WTF==1.0.0 @@ -23,13 +23,13 @@ pytest-runner==4.2 python-dateutil==2.7.3 python-stdnum==1.9 PyYAML==3.13 -requests[security]==2.19.1 +requests[security]==2.27.1 requests-mock==1.5.2 SQLAlchemy==1.2.17 SQLAlchemy-Utils==0.33.11 teal==0.2.0a38 -webargs==4.0.0 -Werkzeug==0.14.1 +webargs==5.5.3 +Werkzeug==0.15.3 sqlalchemy-citext==1.3.post0 flask-weasyprint==0.5 weasyprint==44