action form standart
This commit is contained in:
parent
a2c3741002
commit
b191b155fd
|
@ -4,7 +4,9 @@ from flask import g
|
||||||
|
|
||||||
from ereuse_devicehub.db import db
|
from ereuse_devicehub.db import db
|
||||||
from ereuse_devicehub.resources.device.models import Device
|
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.lot.models import Lot
|
||||||
|
from ereuse_devicehub.resources.enums import Severity
|
||||||
|
|
||||||
|
|
||||||
class LotDeviceForm(FlaskForm):
|
class LotDeviceForm(FlaskForm):
|
||||||
|
@ -80,20 +82,39 @@ class LotForm(FlaskForm):
|
||||||
class NewActionForm(FlaskForm):
|
class NewActionForm(FlaskForm):
|
||||||
name = StringField(u'Name', [validators.length(max=50)])
|
name = StringField(u'Name', [validators.length(max=50)])
|
||||||
devices = HiddenField()
|
devices = HiddenField()
|
||||||
date = DateField(u'Date')
|
date = DateField(u'Date', validators=(validators.Optional(),))
|
||||||
severity = SelectField(u'Severity', choices=[('Info', 'Ok'),
|
severity = SelectField(u'Severity', choices=[(v.name, v.name) for v in Severity])
|
||||||
('Notice', 'Notice'),
|
|
||||||
('Warning', 'Warning'),
|
|
||||||
('Error', 'Error')])
|
|
||||||
description = TextAreaField(u'Description')
|
description = TextAreaField(u'Description')
|
||||||
lot = HiddenField()
|
lot = HiddenField()
|
||||||
type = 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):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.instance = None
|
self.instance = Action()
|
||||||
if self.lot.data:
|
self.populate_obj(self.instance)
|
||||||
self.lot.data = self.lot.data.id
|
|
||||||
|
|
||||||
def save(self):
|
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
|
||||||
|
|
|
@ -22,15 +22,17 @@ class DeviceListView(View):
|
||||||
if id:
|
if id:
|
||||||
lot = lots.filter(Lot.id == id).one()
|
lot = lots.filter(Lot.id == id).one()
|
||||||
devices = [dev for dev in lot.devices if dev.type in filter_types]
|
devices = [dev for dev in lot.devices if dev.type in filter_types]
|
||||||
|
form_new_action = NewActionForm(lot=lot.id)
|
||||||
else:
|
else:
|
||||||
devices = Device.query.filter(
|
devices = Device.query.filter(
|
||||||
Device.owner_id == current_user.id).filter(
|
Device.owner_id == current_user.id).filter(
|
||||||
Device.type.in_(filter_types))
|
Device.type.in_(filter_types))
|
||||||
|
form_new_action = NewActionForm()
|
||||||
|
|
||||||
context = {'devices': devices,
|
context = {'devices': devices,
|
||||||
'lots': lots,
|
'lots': lots,
|
||||||
'form_lot_device': LotDeviceForm(),
|
'form_lot_device': LotDeviceForm(),
|
||||||
'form_new_action': NewActionForm(lot=lot),
|
'form_new_action': form_new_action,
|
||||||
'lot': lot}
|
'lot': lot}
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **context)
|
||||||
|
|
||||||
|
@ -102,12 +104,12 @@ class NewActionView(View):
|
||||||
|
|
||||||
def dispatch_request(self):
|
def dispatch_request(self):
|
||||||
form = NewActionForm()
|
form = NewActionForm()
|
||||||
# import pdb; pdb.set_trace()
|
next_url = url_for('inventory.devices.devicelist')
|
||||||
# from flask import request
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
form.save()
|
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)
|
return flask.redirect(next_url)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,6 @@ function deviceSelect() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAction(action) {
|
function newAction(action) {
|
||||||
console.log(action);
|
|
||||||
$("#actionModal #type").val(action);
|
$("#actionModal #type").val(action);
|
||||||
$("#activeActionModal").click();
|
$("#activeActionModal").click();
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
{{ field(class_="devicesList") }}
|
{{ field(class_="devicesList") }}
|
||||||
{% elif field == form_new_action.lot %}
|
{% elif field == form_new_action.lot %}
|
||||||
{{ field(class_="form-control") }}
|
{{ field(class_="form-control") }}
|
||||||
|
{% elif field == form_new_action.type %}
|
||||||
|
{{ field(class_="form-control") }}
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
{{ field.label(class_="form-label") }}
|
{{ field.label(class_="form-label") }}
|
||||||
|
|
Reference in New Issue