action form standart

This commit is contained in:
Cayo Puigdefabregas 2022-01-10 13:07:05 +01:00
parent a2c3741002
commit b191b155fd
4 changed files with 38 additions and 14 deletions

View file

@ -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

View file

@ -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)

View file

@ -34,7 +34,6 @@ function deviceSelect() {
}
function newAction(action) {
console.log(action);
$("#actionModal #type").val(action);
$("#activeActionModal").click();
}

View file

@ -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 %}
<div class="col-12">
{{ field.label(class_="form-label") }}