From 2327fa5a89bebca37c9b521655a16eab9be7406a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 26 Jan 2022 13:16:18 +0100 Subject: [PATCH] add an unnamed tag --- ereuse_devicehub/inventory/forms.py | 12 ++++++++++++ ereuse_devicehub/inventory/views.py | 18 +++++++++++++++++- .../templates/inventory/tag_list.html | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 870083a6..5cf11870 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -401,6 +401,18 @@ class TagForm(FlaskForm): return self.instance +class TagUnnamedForm(FlaskForm): + amount = IntegerField(u'amount') + + def save(self): + num = self.amount.data + tags_id, _ = g.tag_provider.post('/', {}, query=[('num', num)]) + tags = [Tag(id=tag_id, provider=g.inventory.tag_provider) for tag_id in tags_id] + db.session.add_all(tags) + db.session.commit() + return tags + + class TagDeviceForm(FlaskForm): tag = SelectField(u'Tag', choices=[]) device = StringField(u'Device', [validators.Optional()]) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 57bd7aa2..ba2e7fa0 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -7,7 +7,7 @@ from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.tag.model import Tag from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm, UploadSnapshotForm, \ - NewDeviceForm, TagForm, TagDeviceForm + NewDeviceForm, TagForm, TagUnnamedForm, TagDeviceForm devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') @@ -189,6 +189,21 @@ class TagAddView(View): return flask.render_template(self.template_name, form=form) +class TagAddUnnamedView(View): + methods = ['GET', 'POST'] + decorators = [login_required] + template_name = 'inventory/tagUnnamed.html' + + def dispatch_request(self): + form = TagUnnamedForm() + if form.validate_on_submit(): + form.save() + next_url = url_for('inventory.devices.taglist') + return flask.redirect(next_url) + + return flask.render_template(self.template_name, form=form) + + class TagDeviceAddView(View): methods = ['POST'] decorators = [login_required] @@ -230,5 +245,6 @@ devices.add_url_rule('/upload-snapshot/', view_func=UploadSnapshotView.as_view(' devices.add_url_rule('/device/add/', view_func=CreateDeviceView.as_view('device_add')) devices.add_url_rule('/tag/', view_func=TagListView.as_view('taglist')) devices.add_url_rule('/tag/add/', view_func=TagAddView.as_view('tag_add')) +devices.add_url_rule('/tag//unnamed/add/', view_func=TagAddUnnamedView.as_view('tag_unnamed_add')) devices.add_url_rule('/tag/devices/add/', view_func=TagDeviceAddView.as_view('tag_devices_add')) devices.add_url_rule('/tag/devices//del/', view_func=TagDeviceDeleteView.as_view('tag_devices_del')) diff --git a/ereuse_devicehub/templates/inventory/tag_list.html b/ereuse_devicehub/templates/inventory/tag_list.html index f18631fc..1e8ee6e7 100644 --- a/ereuse_devicehub/templates/inventory/tag_list.html +++ b/ereuse_devicehub/templates/inventory/tag_list.html @@ -30,7 +30,7 @@