2021-12-30 12:52:06 +00:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators
|
2021-12-30 20:35:54 +00:00
|
|
|
from flask import g
|
2021-12-30 12:52:06 +00:00
|
|
|
|
2021-12-30 20:35:54 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2021-12-30 12:52:06 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2022-01-11 12:42:01 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
2021-12-30 12:52:06 +00:00
|
|
|
|
|
|
|
|
2022-01-03 10:32:12 +00:00
|
|
|
class LotDeviceForm(FlaskForm):
|
2021-12-30 12:52:06 +00:00
|
|
|
lot = StringField(u'Lot', [validators.UUID()])
|
|
|
|
devices = StringField(u'Devices', [validators.length(min=1)])
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
2022-01-03 10:32:12 +00:00
|
|
|
self._lot = Lot.query.filter(Lot.id == self.lot.data).filter(
|
|
|
|
Lot.owner_id == g.user.id).one()
|
2021-12-30 12:52:06 +00:00
|
|
|
|
|
|
|
devices = set(self.devices.data.split(","))
|
2022-01-03 10:32:12 +00:00
|
|
|
self._devices = set(Device.query.filter(Device.id.in_(devices)).filter(
|
|
|
|
Device.owner_id == g.user.id).all())
|
2021-12-30 12:52:06 +00:00
|
|
|
|
2022-01-03 10:32:12 +00:00
|
|
|
if not self._devices:
|
2021-12-30 12:52:06 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2021-12-30 20:35:54 +00:00
|
|
|
|
|
|
|
def save(self):
|
2022-01-03 10:32:12 +00:00
|
|
|
self._lot.devices.update(self._devices)
|
|
|
|
db.session.add(self._lot)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
self._lot.devices.difference_update(self._devices)
|
|
|
|
db.session.add(self._lot)
|
2021-12-30 20:35:54 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
class LotForm(FlaskForm):
|
|
|
|
name = StringField(u'Name', [validators.length(min=1)])
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-01-05 10:10:32 +00:00
|
|
|
self.id = kwargs.pop('id', None)
|
2022-01-04 11:45:13 +00:00
|
|
|
self.instance = None
|
2022-01-05 10:10:32 +00:00
|
|
|
if self.id:
|
|
|
|
self.instance = Lot.query.filter(Lot.id == self.id).filter(
|
2022-01-03 10:32:12 +00:00
|
|
|
Lot.owner_id == g.user.id).one()
|
2021-12-30 20:35:54 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2022-01-04 11:45:13 +00:00
|
|
|
if self.instance and not self.name.data:
|
|
|
|
self.name.data = self.instance.name
|
2021-12-30 20:35:54 +00:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
name = self.name.data.strip()
|
2022-01-04 11:45:13 +00:00
|
|
|
if self.instance:
|
|
|
|
if self.instance.name == name:
|
|
|
|
return self.instance
|
|
|
|
self.instance.name = name
|
2021-12-30 20:35:54 +00:00
|
|
|
else:
|
2022-01-04 11:45:13 +00:00
|
|
|
self.instance = Lot(name=name)
|
2021-12-30 20:35:54 +00:00
|
|
|
|
2022-01-05 10:10:32 +00:00
|
|
|
if not self.id:
|
|
|
|
db.session.add(self.instance)
|
|
|
|
db.session.commit()
|
|
|
|
return self.instance
|
|
|
|
|
2021-12-30 20:35:54 +00:00
|
|
|
db.session.commit()
|
2022-01-05 10:10:32 +00:00
|
|
|
return self.id
|
2022-01-03 11:28:42 +00:00
|
|
|
|
|
|
|
def remove(self):
|
2022-01-04 11:45:13 +00:00
|
|
|
if self.instance and not self.instance.devices:
|
|
|
|
self.instance.delete()
|
2022-01-03 11:28:42 +00:00
|
|
|
db.session.commit()
|
2022-01-04 11:45:13 +00:00
|
|
|
return self.instance
|
|
|
|
|
|
|
|
|
2022-01-11 12:42:01 +00:00
|
|
|
class TagForm(FlaskForm):
|
|
|
|
code = StringField(u'Code', [validators.length(min=1)])
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
self.instance = Tag(id=self.code.data)
|
|
|
|
db.session.add(self.instance)
|
|
|
|
db.session.commit()
|
|
|
|
return self.instance
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
if not self.instance.device and not self.instance.provider:
|
|
|
|
self.instance.delete()
|
|
|
|
db.session.commit()
|
|
|
|
return self.instance
|
|
|
|
|
|
|
|
|
2022-01-04 11:56:28 +00:00
|
|
|
class NewActionForm(FlaskForm):
|
|
|
|
name = StringField(u'Name')
|
|
|
|
date = StringField(u'Date')
|
|
|
|
severity = StringField(u'Severity')
|
|
|
|
description = StringField(u'Description')
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
pass
|