From b191b155fd8ecf099bd07cefc7bbccd662c4b13c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 10 Jan 2022 13:07:05 +0100 Subject: [PATCH] action form standart --- ereuse_devicehub/inventory/forms.py | 39 ++++++++++++++----- ereuse_devicehub/inventory/views.py | 10 +++-- ereuse_devicehub/static/js/main_inventory.js | 1 - .../templates/inventory/actions.html | 2 + 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 00c6afdc..ed0e3fc5 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -4,7 +4,9 @@ from flask import g from ereuse_devicehub.db import db from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.action.models import Action from ereuse_devicehub.resources.lot.models import Lot +from ereuse_devicehub.resources.enums import Severity class LotDeviceForm(FlaskForm): @@ -80,20 +82,39 @@ class LotForm(FlaskForm): class NewActionForm(FlaskForm): name = StringField(u'Name', [validators.length(max=50)]) devices = HiddenField() - date = DateField(u'Date') - severity = SelectField(u'Severity', choices=[('Info', 'Ok'), - ('Notice', 'Notice'), - ('Warning', 'Warning'), - ('Error', 'Error')]) + date = DateField(u'Date', validators=(validators.Optional(),)) + severity = SelectField(u'Severity', choices=[(v.name, v.name) for v in Severity]) description = TextAreaField(u'Description') lot = HiddenField() type = HiddenField() + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + + if not is_valid: + return False + + if self.lot.data: + self._lot = Lot.query.filter(Lot.id == self.lot.data).filter( + Lot.owner_id == g.user.id).one() + + devices = set(self.devices.data.split(",")) + self._devices = set(Device.query.filter(Device.id.in_(devices)).filter( + Device.owner_id == g.user.id).all()) + + if not self._devices: + return False + + return True + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.instance = None - if self.lot.data: - self.lot.data = self.lot.data.id + self.instance = Action() + self.populate_obj(self.instance) def save(self): - pass + self.instance.devices = self._devices + self.instance.severity = Severity[self.severity.data] + db.session.add(self.instance) + db.session.commit() + return self.instance diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index e35f3777..dec3544b 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -22,15 +22,17 @@ class DeviceListView(View): if id: lot = lots.filter(Lot.id == id).one() devices = [dev for dev in lot.devices if dev.type in filter_types] + form_new_action = NewActionForm(lot=lot.id) else: devices = Device.query.filter( Device.owner_id == current_user.id).filter( Device.type.in_(filter_types)) + form_new_action = NewActionForm() context = {'devices': devices, 'lots': lots, 'form_lot_device': LotDeviceForm(), - 'form_new_action': NewActionForm(lot=lot), + 'form_new_action': form_new_action, 'lot': lot} return flask.render_template(self.template_name, **context) @@ -102,12 +104,12 @@ class NewActionView(View): def dispatch_request(self): form = NewActionForm() - # import pdb; pdb.set_trace() - # from flask import request + next_url = url_for('inventory.devices.devicelist') if form.validate_on_submit(): form.save() + if form.lot.data: + next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data) - next_url = url_for('inventory.devices.devicelist') return flask.redirect(next_url) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 7cfa0de6..186bdd17 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -34,7 +34,6 @@ function deviceSelect() { } function newAction(action) { - console.log(action); $("#actionModal #type").val(action); $("#activeActionModal").click(); } diff --git a/ereuse_devicehub/templates/inventory/actions.html b/ereuse_devicehub/templates/inventory/actions.html index 1a2d60f9..45776ef5 100644 --- a/ereuse_devicehub/templates/inventory/actions.html +++ b/ereuse_devicehub/templates/inventory/actions.html @@ -19,6 +19,8 @@ {{ field(class_="devicesList") }} {% elif field == form_new_action.lot %} {{ field(class_="form-control") }} + {% elif field == form_new_action.type %} + {{ field(class_="form-control") }} {% else %}
{{ field.label(class_="form-label") }}