2022-02-08 12:59:50 +00:00
|
|
|
import copy
|
2022-02-10 10:29:47 +00:00
|
|
|
import json
|
2022-02-03 09:50:36 +00:00
|
|
|
from json.decoder import JSONDecodeError
|
2022-03-08 10:01:10 +00:00
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
from boltons.urlutils import URL
|
2022-03-17 17:52:58 +00:00
|
|
|
from flask import current_app as app
|
2022-02-10 10:29:47 +00:00
|
|
|
from flask import g, request
|
|
|
|
from flask_wtf import FlaskForm
|
2022-04-05 07:22:02 +00:00
|
|
|
from marshmallow import ValidationError
|
2022-03-08 10:01:10 +00:00
|
|
|
from sqlalchemy import or_
|
2022-02-10 10:29:47 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2022-02-22 10:55:01 +00:00
|
|
|
from wtforms import (
|
2022-03-08 10:01:10 +00:00
|
|
|
BooleanField,
|
|
|
|
DateField,
|
|
|
|
FileField,
|
|
|
|
FloatField,
|
|
|
|
Form,
|
|
|
|
HiddenField,
|
|
|
|
IntegerField,
|
|
|
|
MultipleFileField,
|
|
|
|
SelectField,
|
|
|
|
StringField,
|
|
|
|
TextAreaField,
|
|
|
|
URLField,
|
|
|
|
validators,
|
|
|
|
)
|
2022-02-10 10:29:47 +00:00
|
|
|
from wtforms.fields import FormField
|
|
|
|
|
2022-02-22 10:55:01 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2022-05-18 09:03:58 +00:00
|
|
|
from ereuse_devicehub.parser.models import SnapshotsLog
|
2022-03-29 11:36:18 +00:00
|
|
|
from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
|
2022-03-30 16:29:15 +00:00
|
|
|
from ereuse_devicehub.parser.schemas import Snapshot_lite
|
2022-03-29 15:20:58 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Snapshot, Trade
|
2022-03-08 10:01:10 +00:00
|
|
|
from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
|
2022-04-08 09:12:17 +00:00
|
|
|
from ereuse_devicehub.resources.action.views.snapshot import (
|
2022-05-16 15:52:31 +00:00
|
|
|
SnapshotMixin,
|
2022-04-08 09:12:17 +00:00
|
|
|
move_json,
|
|
|
|
save_json,
|
|
|
|
)
|
2022-02-22 10:55:01 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import (
|
2022-03-08 10:01:10 +00:00
|
|
|
SAI,
|
|
|
|
Cellphone,
|
2022-05-16 10:40:14 +00:00
|
|
|
ComputerMonitor,
|
2022-03-08 10:01:10 +00:00
|
|
|
Device,
|
|
|
|
Keyboard,
|
|
|
|
MemoryCardReader,
|
|
|
|
Mouse,
|
|
|
|
Smartphone,
|
|
|
|
Tablet,
|
|
|
|
)
|
2022-02-03 09:50:36 +00:00
|
|
|
from ereuse_devicehub.resources.device.sync import Sync
|
2022-02-10 10:29:47 +00:00
|
|
|
from ereuse_devicehub.resources.documents.models import DataWipeDocument
|
2022-04-08 09:17:58 +00:00
|
|
|
from ereuse_devicehub.resources.enums import Severity
|
2022-02-10 10:29:47 +00:00
|
|
|
from ereuse_devicehub.resources.hash_reports import insert_hash
|
2021-12-30 12:52:06 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2022-01-11 12:42:01 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
2022-02-21 12:02:28 +00:00
|
|
|
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
|
2022-02-22 10:55:01 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2021-12-30 12:52:06 +00:00
|
|
|
|
2022-03-15 09:02:30 +00:00
|
|
|
DEVICES = {
|
2022-05-16 09:01:52 +00:00
|
|
|
"All": ["All Devices"],
|
2022-03-15 09:02:30 +00:00
|
|
|
"Computer": [
|
2022-05-09 11:43:02 +00:00
|
|
|
"All Computers",
|
2022-03-15 09:02:30 +00:00
|
|
|
"Desktop",
|
|
|
|
"Laptop",
|
|
|
|
"Server",
|
|
|
|
],
|
2022-05-09 11:43:02 +00:00
|
|
|
"Monitor": [
|
|
|
|
"All Monitors",
|
|
|
|
"ComputerMonitor",
|
|
|
|
"Monitor",
|
|
|
|
"TelevisionSet",
|
|
|
|
"Projector",
|
|
|
|
],
|
|
|
|
"Mobile, tablet & smartphone": [
|
|
|
|
"All Mobile",
|
|
|
|
"Mobile",
|
|
|
|
"Tablet",
|
|
|
|
"Smartphone",
|
|
|
|
"Cellphone",
|
|
|
|
],
|
2022-03-15 09:02:30 +00:00
|
|
|
}
|
2022-03-07 12:07:26 +00:00
|
|
|
|
2022-05-09 14:35:44 +00:00
|
|
|
COMPUTERS = ['Desktop', 'Laptop', 'Server', 'Computer']
|
2022-05-09 11:43:02 +00:00
|
|
|
|
|
|
|
MONITORS = ["ComputerMonitor", "Monitor", "TelevisionSet", "Projector"]
|
|
|
|
MOBILE = ["Mobile", "Tablet", "Smartphone", "Cellphone"]
|
|
|
|
|
2022-03-07 12:07:26 +00:00
|
|
|
|
|
|
|
class FilterForm(FlaskForm):
|
2022-03-08 10:01:10 +00:00
|
|
|
filter = SelectField(
|
2022-05-09 11:43:02 +00:00
|
|
|
'', choices=DEVICES, default="All Computers", render_kw={'class': "form-select"}
|
2022-03-08 10:01:10 +00:00
|
|
|
)
|
2022-03-07 12:07:26 +00:00
|
|
|
|
2022-05-09 11:43:02 +00:00
|
|
|
def __init__(self, lots, lot_id, *args, **kwargs):
|
2022-03-08 09:30:53 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2022-05-09 11:43:02 +00:00
|
|
|
self.lots = lots
|
|
|
|
self.lot_id = lot_id
|
2022-05-13 08:22:32 +00:00
|
|
|
self.only_unassigned = kwargs.pop('only_unassigned', True)
|
2022-05-09 11:43:02 +00:00
|
|
|
self._get_types()
|
|
|
|
|
|
|
|
def _get_types(self):
|
2022-03-15 09:02:30 +00:00
|
|
|
types_of_devices = [item for sublist in DEVICES.values() for item in sublist]
|
|
|
|
dev = request.args.get('filter')
|
2022-05-09 11:43:02 +00:00
|
|
|
self.device_type = dev if dev in types_of_devices else None
|
|
|
|
if self.device_type:
|
|
|
|
self.filter.data = self.device_type
|
2022-03-08 09:30:53 +00:00
|
|
|
|
2022-05-09 14:48:34 +00:00
|
|
|
def filter_from_lots(self):
|
2022-05-09 11:43:02 +00:00
|
|
|
if self.lot_id:
|
|
|
|
self.lot = self.lots.filter(Lot.id == self.lot_id).one()
|
|
|
|
device_ids = (d.id for d in self.lot.devices)
|
|
|
|
self.devices = Device.query.filter(Device.id.in_(device_ids))
|
|
|
|
else:
|
2022-05-12 12:42:55 +00:00
|
|
|
self.devices = Device.query.filter(Device.owner_id == g.user.id)
|
2022-05-13 08:22:32 +00:00
|
|
|
if self.only_unassigned:
|
2022-05-12 12:42:55 +00:00
|
|
|
self.devices = self.devices.filter_by(lots=None)
|
2022-05-09 11:43:02 +00:00
|
|
|
|
2022-05-09 14:48:34 +00:00
|
|
|
def search(self):
|
|
|
|
self.filter_from_lots()
|
2022-05-09 11:43:02 +00:00
|
|
|
filter_type = None
|
|
|
|
if self.device_type:
|
|
|
|
filter_type = [self.device_type]
|
|
|
|
else:
|
|
|
|
# Case without Filter
|
|
|
|
filter_type = COMPUTERS
|
|
|
|
|
|
|
|
# Generic Filters
|
|
|
|
if "All Devices" == self.device_type:
|
2022-05-16 10:40:14 +00:00
|
|
|
filter_type = COMPUTERS + MONITORS + MOBILE
|
2022-05-09 11:43:02 +00:00
|
|
|
|
2022-05-09 14:48:34 +00:00
|
|
|
elif "All Computers" == self.device_type:
|
|
|
|
filter_type = COMPUTERS
|
|
|
|
|
2022-05-09 11:43:02 +00:00
|
|
|
elif "All Monitors" == self.device_type:
|
|
|
|
filter_type = MONITORS
|
|
|
|
|
|
|
|
elif "All Mobile" == self.device_type:
|
|
|
|
filter_type = MOBILE
|
|
|
|
|
|
|
|
if filter_type:
|
|
|
|
self.devices = self.devices.filter(Device.type.in_(filter_type))
|
2022-03-07 14:17:03 +00:00
|
|
|
|
2022-05-09 11:43:02 +00:00
|
|
|
return self.devices.order_by(Device.updated.desc())
|
2022-03-07 12:07:26 +00:00
|
|
|
|
|
|
|
|
2021-12-30 20:35:54 +00:00
|
|
|
class LotForm(FlaskForm):
|
2022-02-22 10:15:37 +00:00
|
|
|
name = StringField('Name', [validators.length(min=1)])
|
2021-12-30 20:35:54 +00:00
|
|
|
|
|
|
|
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:
|
2022-02-22 10:55:01 +00:00
|
|
|
self.instance = (
|
|
|
|
Lot.query.filter(Lot.id == self.id)
|
|
|
|
.filter(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):
|
2022-01-12 19:26:37 +00:00
|
|
|
if not self.id:
|
|
|
|
self.instance = Lot(name=self.name.data)
|
|
|
|
|
|
|
|
self.populate_obj(self.instance)
|
2021-12-30 20:35:54 +00:00
|
|
|
|
2022-01-05 10:10:32 +00:00
|
|
|
if not self.id:
|
2022-01-12 19:26:37 +00:00
|
|
|
self.id = self.instance.id
|
2022-01-05 10:10:32 +00:00
|
|
|
db.session.add(self.instance)
|
|
|
|
db.session.commit()
|
2022-01-12 19:26:37 +00:00
|
|
|
return self.id
|
2022-01-05 10:10:32 +00:00
|
|
|
|
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-03-07 09:20:18 +00:00
|
|
|
if self.instance and not self.instance.trade:
|
2022-01-04 11:45:13 +00:00
|
|
|
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-05-16 15:52:31 +00:00
|
|
|
class UploadSnapshotForm(SnapshotMixin, FlaskForm):
|
2022-02-22 10:15:37 +00:00
|
|
|
snapshot = MultipleFileField('Select a Snapshot File', [validators.DataRequired()])
|
2022-01-17 13:17:26 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
|
|
|
data = request.files.getlist(self.snapshot.name)
|
|
|
|
if not data:
|
|
|
|
return False
|
|
|
|
self.snapshots = []
|
|
|
|
self.result = {}
|
|
|
|
for d in data:
|
|
|
|
filename = d.filename
|
2022-01-18 10:45:28 +00:00
|
|
|
self.result[filename] = 'Not processed'
|
2022-01-17 13:17:26 +00:00
|
|
|
d = d.stream.read()
|
|
|
|
if not d:
|
2022-01-20 09:33:14 +00:00
|
|
|
self.result[filename] = 'Error this snapshot is empty'
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
d_json = json.loads(d)
|
|
|
|
except JSONDecodeError:
|
|
|
|
self.result[filename] = 'Error this snapshot is not a json'
|
|
|
|
continue
|
|
|
|
|
|
|
|
uuid_snapshot = d_json.get('uuid')
|
|
|
|
if Snapshot.query.filter(Snapshot.uuid == uuid_snapshot).all():
|
|
|
|
self.result[filename] = 'Error this snapshot exist'
|
2022-01-17 13:17:26 +00:00
|
|
|
continue
|
2022-01-20 09:33:14 +00:00
|
|
|
|
|
|
|
self.snapshots.append((filename, d_json))
|
2022-01-17 13:17:26 +00:00
|
|
|
|
|
|
|
if not self.snapshots:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2022-03-30 16:29:15 +00:00
|
|
|
def is_wb_lite_snapshot(self, version: str) -> bool:
|
|
|
|
is_lite = False
|
2022-05-16 15:57:09 +00:00
|
|
|
if version in app.config['SCHEMA_WORKBENCH']:
|
2022-03-30 16:29:15 +00:00
|
|
|
is_lite = True
|
|
|
|
|
|
|
|
return is_lite
|
|
|
|
|
2022-03-08 16:43:15 +00:00
|
|
|
def save(self, commit=True):
|
2022-01-17 13:17:26 +00:00
|
|
|
if any([x == 'Error' for x in self.result.values()]):
|
|
|
|
return
|
|
|
|
schema = SnapshotSchema()
|
2022-03-29 11:36:18 +00:00
|
|
|
schema_lite = Snapshot_lite()
|
2022-04-12 08:25:45 +00:00
|
|
|
devices = []
|
2022-03-17 17:52:58 +00:00
|
|
|
self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
|
2022-01-18 10:45:28 +00:00
|
|
|
for filename, snapshot_json in self.snapshots:
|
2022-01-17 13:17:26 +00:00
|
|
|
path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
|
|
|
|
snapshot_json.pop('debug', None)
|
2022-04-08 14:46:57 +00:00
|
|
|
version = snapshot_json.get('schema_api')
|
2022-03-30 16:29:15 +00:00
|
|
|
if self.is_wb_lite_snapshot(version):
|
2022-03-29 11:36:18 +00:00
|
|
|
self.snapshot_json = schema_lite.load(snapshot_json)
|
2022-04-08 09:12:17 +00:00
|
|
|
snapshot_json = ParseSnapshotLsHw(self.snapshot_json).snapshot_json
|
2022-03-29 11:36:18 +00:00
|
|
|
|
2022-04-05 07:22:02 +00:00
|
|
|
try:
|
|
|
|
snapshot_json = schema.load(snapshot_json)
|
|
|
|
except ValidationError as err:
|
|
|
|
txt = "{}".format(err)
|
|
|
|
uuid = snapshot_json.get('uuid')
|
2022-04-25 09:45:25 +00:00
|
|
|
sid = snapshot_json.get('sid')
|
2022-05-18 09:03:58 +00:00
|
|
|
error = SnapshotsLog(
|
2022-04-12 09:13:08 +00:00
|
|
|
description=txt,
|
|
|
|
snapshot_uuid=uuid,
|
|
|
|
severity=Severity.Error,
|
2022-04-25 09:45:25 +00:00
|
|
|
sid=sid,
|
2022-04-05 07:22:02 +00:00
|
|
|
)
|
|
|
|
error.save(commit=True)
|
|
|
|
self.result[filename] = 'Error'
|
|
|
|
continue
|
|
|
|
|
2022-01-20 12:13:20 +00:00
|
|
|
response = self.build(snapshot_json)
|
2022-04-08 09:12:17 +00:00
|
|
|
db.session.add(response)
|
2022-04-12 08:25:45 +00:00
|
|
|
devices.append(response.device)
|
2022-01-18 10:45:28 +00:00
|
|
|
|
|
|
|
if hasattr(response, 'type'):
|
|
|
|
self.result[filename] = 'Ok'
|
|
|
|
else:
|
|
|
|
self.result[filename] = 'Error'
|
|
|
|
|
2022-01-17 13:17:26 +00:00
|
|
|
move_json(self.tmp_snapshots, path_snapshot, g.user.email)
|
|
|
|
|
2022-03-08 16:43:15 +00:00
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
2022-04-12 08:25:45 +00:00
|
|
|
return self.result, devices
|
2022-01-20 09:33:14 +00:00
|
|
|
|
2022-01-17 13:17:26 +00:00
|
|
|
|
2022-01-19 12:40:40 +00:00
|
|
|
class NewDeviceForm(FlaskForm):
|
2022-02-22 10:15:37 +00:00
|
|
|
type = StringField('Type', [validators.DataRequired()])
|
|
|
|
label = StringField('Label')
|
|
|
|
serial_number = StringField('Seria Number', [validators.DataRequired()])
|
|
|
|
model = StringField('Model', [validators.DataRequired()])
|
|
|
|
manufacturer = StringField('Manufacturer', [validators.DataRequired()])
|
|
|
|
appearance = StringField('Appearance', [validators.Optional()])
|
|
|
|
functionality = StringField('Functionality', [validators.Optional()])
|
|
|
|
brand = StringField('Brand')
|
|
|
|
generation = IntegerField('Generation')
|
|
|
|
version = StringField('Version')
|
|
|
|
weight = FloatField('Weight', [validators.DataRequired()])
|
|
|
|
width = FloatField('Width', [validators.DataRequired()])
|
|
|
|
height = FloatField('Height', [validators.DataRequired()])
|
|
|
|
depth = FloatField('Depth', [validators.DataRequired()])
|
|
|
|
variant = StringField('Variant', [validators.Optional()])
|
|
|
|
sku = StringField('SKU', [validators.Optional()])
|
|
|
|
image = StringField('Image', [validators.Optional(), validators.URL()])
|
|
|
|
imei = IntegerField('IMEI', [validators.Optional()])
|
|
|
|
meid = StringField('MEID', [validators.Optional()])
|
|
|
|
resolution = IntegerField('Resolution width', [validators.Optional()])
|
|
|
|
screen = FloatField('Screen size', [validators.Optional()])
|
2022-01-18 13:09:01 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-02-22 10:55:01 +00:00
|
|
|
self.devices = {
|
|
|
|
"Smartphone": Smartphone,
|
|
|
|
"Tablet": Tablet,
|
|
|
|
"Cellphone": Cellphone,
|
2022-05-16 10:40:14 +00:00
|
|
|
"ComputerMonitor": ComputerMonitor,
|
2022-02-22 10:55:01 +00:00
|
|
|
"Mouse": Mouse,
|
|
|
|
"Keyboard": Keyboard,
|
|
|
|
"SAI": SAI,
|
|
|
|
"MemoryCardReader": MemoryCardReader,
|
|
|
|
}
|
2022-01-20 12:13:20 +00:00
|
|
|
|
|
|
|
if not self.generation.data:
|
2022-01-21 13:19:39 +00:00
|
|
|
self.generation.data = 1
|
2022-01-20 12:13:20 +00:00
|
|
|
|
|
|
|
if not self.weight.data:
|
|
|
|
self.weight.data = 0.1
|
|
|
|
|
|
|
|
if not self.height.data:
|
|
|
|
self.height.data = 0.1
|
|
|
|
|
|
|
|
if not self.width.data:
|
|
|
|
self.width.data = 0.1
|
|
|
|
|
|
|
|
if not self.depth.data:
|
|
|
|
self.depth.data = 0.1
|
|
|
|
|
2022-03-02 12:36:06 +00:00
|
|
|
def validate(self, extra_validators=None): # noqa: C901
|
2022-01-24 12:48:16 +00:00
|
|
|
error = ["Not a correct value"]
|
2022-01-20 12:13:20 +00:00
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
2022-01-21 13:19:39 +00:00
|
|
|
if self.generation.data < 1:
|
2022-01-24 12:48:16 +00:00
|
|
|
self.generation.errors = error
|
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
|
|
|
if self.weight.data < 0.1:
|
2022-01-24 12:48:16 +00:00
|
|
|
self.weight.errors = error
|
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
|
|
|
if self.height.data < 0.1:
|
2022-01-24 12:48:16 +00:00
|
|
|
self.height.errors = error
|
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
|
|
|
if self.width.data < 0.1:
|
2022-01-24 12:48:16 +00:00
|
|
|
self.width.errors = error
|
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
|
|
|
if self.depth.data < 0.1:
|
2022-01-24 12:48:16 +00:00
|
|
|
self.depth.errors = error
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
if self.imei.data:
|
|
|
|
if not 13 < len(str(self.imei.data)) < 17:
|
|
|
|
self.imei.errors = error
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
if self.meid.data:
|
|
|
|
meid = self.meid.data
|
|
|
|
if not 13 < len(meid) < 17:
|
|
|
|
is_valid = False
|
|
|
|
try:
|
|
|
|
int(meid, 16)
|
|
|
|
except ValueError:
|
|
|
|
self.meid.errors = error
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
if not is_valid:
|
2022-01-20 12:13:20 +00:00
|
|
|
return False
|
|
|
|
|
2022-01-21 10:07:15 +00:00
|
|
|
if self.image.data == '':
|
|
|
|
self.image.data = None
|
|
|
|
if self.manufacturer.data:
|
|
|
|
self.manufacturer.data = self.manufacturer.data.lower()
|
|
|
|
if self.model.data:
|
|
|
|
self.model.data = self.model.data.lower()
|
|
|
|
if self.serial_number.data:
|
|
|
|
self.serial_number.data = self.serial_number.data.lower()
|
|
|
|
|
2022-01-20 12:13:20 +00:00
|
|
|
return True
|
|
|
|
|
2022-03-08 16:43:15 +00:00
|
|
|
def save(self, commit=True):
|
2022-01-21 13:19:39 +00:00
|
|
|
|
|
|
|
json_snapshot = {
|
|
|
|
'type': 'Snapshot',
|
|
|
|
'software': 'Web',
|
|
|
|
'version': '11.0',
|
|
|
|
'device': {
|
2022-02-22 10:55:01 +00:00
|
|
|
'type': self.type.data,
|
|
|
|
'model': self.model.data,
|
|
|
|
'manufacturer': self.manufacturer.data,
|
|
|
|
'serialNumber': self.serial_number.data,
|
|
|
|
'brand': self.brand.data,
|
|
|
|
'version': self.version.data,
|
|
|
|
'generation': self.generation.data,
|
|
|
|
'sku': self.sku.data,
|
|
|
|
'weight': self.weight.data,
|
|
|
|
'width': self.width.data,
|
|
|
|
'height': self.height.data,
|
|
|
|
'depth': self.depth.data,
|
|
|
|
'variant': self.variant.data,
|
|
|
|
'image': self.image.data,
|
|
|
|
},
|
2022-01-21 13:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.appearance.data or self.functionality.data:
|
2022-02-22 10:55:01 +00:00
|
|
|
json_snapshot['device']['actions'] = [
|
|
|
|
{
|
|
|
|
'type': 'VisualTest',
|
|
|
|
'appearanceRange': self.appearance.data,
|
|
|
|
'functionalityRange': self.functionality.data,
|
|
|
|
}
|
|
|
|
]
|
2022-01-21 13:19:39 +00:00
|
|
|
|
|
|
|
upload_form = UploadSnapshotForm()
|
2022-01-24 12:48:16 +00:00
|
|
|
upload_form.sync = Sync()
|
|
|
|
|
|
|
|
schema = SnapshotSchema()
|
|
|
|
self.tmp_snapshots = '/tmp/'
|
|
|
|
path_snapshot = save_json(json_snapshot, self.tmp_snapshots, g.user.email)
|
|
|
|
snapshot_json = schema.load(json_snapshot)
|
|
|
|
|
2022-05-16 10:40:14 +00:00
|
|
|
if self.type.data == 'ComputerMonitor':
|
2022-01-24 12:48:16 +00:00
|
|
|
snapshot_json['device'].resolution_width = self.resolution.data
|
|
|
|
snapshot_json['device'].size = self.screen.data
|
|
|
|
|
|
|
|
if self.type.data in ['Smartphone', 'Tablet', 'Cellphone']:
|
|
|
|
snapshot_json['device'].imei = self.imei.data
|
|
|
|
snapshot_json['device'].meid = self.meid.data
|
|
|
|
|
|
|
|
snapshot = upload_form.build(snapshot_json)
|
|
|
|
|
|
|
|
move_json(self.tmp_snapshots, path_snapshot, g.user.email)
|
2022-05-16 10:40:14 +00:00
|
|
|
if self.type.data == 'ComputerMonitor':
|
2022-01-24 12:48:16 +00:00
|
|
|
snapshot.device.resolution = self.resolution.data
|
|
|
|
snapshot.device.screen = self.screen.data
|
|
|
|
|
2022-03-08 16:43:15 +00:00
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
2022-01-24 12:48:16 +00:00
|
|
|
return snapshot
|
2022-01-18 13:09:01 +00:00
|
|
|
|
|
|
|
|
2022-01-25 11:53:36 +00:00
|
|
|
class TagDeviceForm(FlaskForm):
|
2022-02-22 10:15:37 +00:00
|
|
|
tag = SelectField('Tag', choices=[])
|
|
|
|
device = StringField('Device', [validators.Optional()])
|
2022-01-25 13:39:15 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-01-26 09:59:13 +00:00
|
|
|
self.delete = kwargs.pop('delete', None)
|
|
|
|
self.device_id = kwargs.pop('device', None)
|
2022-01-25 13:39:15 +00:00
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-01-26 09:59:13 +00:00
|
|
|
if self.delete:
|
2022-03-08 10:01:10 +00:00
|
|
|
tags = (
|
|
|
|
Tag.query.filter(Tag.owner_id == g.user.id)
|
|
|
|
.filter_by(device_id=self.device_id)
|
|
|
|
.order_by(Tag.id)
|
|
|
|
)
|
2022-01-25 13:39:15 +00:00
|
|
|
else:
|
2022-03-08 10:01:10 +00:00
|
|
|
tags = (
|
|
|
|
Tag.query.filter(Tag.owner_id == g.user.id)
|
|
|
|
.filter_by(device_id=None)
|
|
|
|
.order_by(Tag.id)
|
|
|
|
)
|
2022-01-25 13:39:15 +00:00
|
|
|
|
|
|
|
self.tag.choices = [(tag.id, tag.id) for tag in tags]
|
2022-01-25 11:53:36 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
2022-02-22 10:55:01 +00:00
|
|
|
self._tag = (
|
|
|
|
Tag.query.filter(Tag.id == self.tag.data)
|
|
|
|
.filter(Tag.owner_id == g.user.id)
|
|
|
|
.one()
|
|
|
|
)
|
2022-01-25 11:53:36 +00:00
|
|
|
|
2022-01-26 12:50:43 +00:00
|
|
|
if not self.delete and self._tag.device_id:
|
|
|
|
self.tag.errors = [("This tag is actualy in use.")]
|
|
|
|
return False
|
|
|
|
|
2022-01-26 09:59:13 +00:00
|
|
|
if self.device.data:
|
|
|
|
try:
|
|
|
|
self.device.data = int(self.device.data.split(',')[-1])
|
2022-03-02 12:36:06 +00:00
|
|
|
except: # noqa: E722
|
2022-01-26 09:59:13 +00:00
|
|
|
self.device.data = None
|
|
|
|
|
|
|
|
if self.device_id or self.device.data:
|
|
|
|
self.device_id = self.device_id or self.device.data
|
2022-02-22 10:55:01 +00:00
|
|
|
self._device = (
|
|
|
|
Device.query.filter(Device.id == self.device_id)
|
|
|
|
.filter(Device.owner_id == g.user.id)
|
|
|
|
.one()
|
|
|
|
)
|
2022-01-26 09:59:13 +00:00
|
|
|
|
2022-01-25 11:53:36 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
self._tag.device_id = self._device.id
|
|
|
|
db.session.add(self._tag)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
self._tag.device = None
|
|
|
|
db.session.add(self._tag)
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-01-11 12:42:01 +00:00
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class ActionFormMixin(FlaskForm):
|
2022-02-22 10:55:01 +00:00
|
|
|
name = StringField(
|
|
|
|
'Name',
|
|
|
|
[validators.length(max=50)],
|
|
|
|
description="A name or title of the event. Something to look for.",
|
|
|
|
)
|
2022-01-05 11:13:44 +00:00
|
|
|
devices = HiddenField()
|
2022-02-22 10:55:01 +00:00
|
|
|
date = DateField(
|
|
|
|
'Date',
|
|
|
|
[validators.Optional()],
|
|
|
|
description="""When the action ends. For some actions like booking
|
2022-02-07 13:02:28 +00:00
|
|
|
the time when it expires, for others like renting the
|
|
|
|
time that the end rents. For specific actions, it is the
|
|
|
|
time in which they are carried out; differs from created
|
2022-02-22 10:55:01 +00:00
|
|
|
in that created is where the system receives the action.""",
|
|
|
|
)
|
|
|
|
severity = SelectField(
|
|
|
|
'Severity',
|
|
|
|
choices=[(v.name, v.name) for v in Severity],
|
|
|
|
description="""An indicator that evaluates the execution of the event.
|
|
|
|
For example, failed events are set to Error""",
|
|
|
|
)
|
2022-02-22 10:15:37 +00:00
|
|
|
description = TextAreaField('Description')
|
2022-02-04 11:10:27 +00:00
|
|
|
lot = HiddenField()
|
2022-01-05 15:00:13 +00:00
|
|
|
type = HiddenField()
|
|
|
|
|
2022-01-10 12:07:05 +00:00
|
|
|
def validate(self, extra_validators=None):
|
2022-02-17 12:52:55 +00:00
|
|
|
is_valid = self.generic_validation(extra_validators=extra_validators)
|
2022-01-10 12:07:05 +00:00
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
2022-04-21 12:01:33 +00:00
|
|
|
if self.type.data in [None, '']:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self.devices.data:
|
|
|
|
return False
|
|
|
|
|
2022-02-16 13:03:01 +00:00
|
|
|
self._devices = OrderedSet()
|
2022-01-10 12:07:05 +00:00
|
|
|
|
2022-04-21 12:01:33 +00:00
|
|
|
devices = set(self.devices.data.split(","))
|
|
|
|
self._devices = OrderedSet(
|
|
|
|
Device.query.filter(Device.id.in_(devices))
|
|
|
|
.filter(Device.owner_id == g.user.id)
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self._devices:
|
|
|
|
return False
|
2022-01-10 12:07:05 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2022-02-17 12:52:55 +00:00
|
|
|
def generic_validation(self, extra_validators=None):
|
|
|
|
# Some times we want check validations without devices list
|
|
|
|
return super().validate(extra_validators)
|
|
|
|
|
2022-01-04 11:56:28 +00:00
|
|
|
def save(self):
|
2022-02-01 12:40:06 +00:00
|
|
|
Model = db.Model._decl_class_registry.data[self.type.data]()
|
|
|
|
self.instance = Model()
|
2022-02-02 11:52:16 +00:00
|
|
|
devices = self.devices.data
|
|
|
|
severity = self.severity.data
|
2022-02-01 12:40:06 +00:00
|
|
|
self.devices.data = self._devices
|
|
|
|
self.severity.data = Severity[self.severity.data]
|
|
|
|
|
|
|
|
self.populate_obj(self.instance)
|
2022-02-02 12:51:54 +00:00
|
|
|
db.session.add(self.instance)
|
|
|
|
db.session.commit()
|
2022-02-02 11:52:16 +00:00
|
|
|
|
|
|
|
self.devices.data = devices
|
|
|
|
self.severity.data = severity
|
|
|
|
|
2022-01-10 12:07:05 +00:00
|
|
|
return self.instance
|
2022-01-10 12:27:27 +00:00
|
|
|
|
2022-02-18 12:37:45 +00:00
|
|
|
def check_valid(self):
|
|
|
|
if self.type.data in ['', None]:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.validate():
|
|
|
|
return self.type.data
|
|
|
|
|
2022-01-10 12:27:27 +00:00
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class NewActionForm(ActionFormMixin):
|
2022-04-21 12:01:33 +00:00
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.type.data in ['Allocate', 'Deallocate', 'Trade', 'DataWipe']:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class AllocateForm(ActionFormMixin):
|
2022-05-12 08:09:03 +00:00
|
|
|
date = HiddenField('')
|
2022-02-22 10:15:37 +00:00
|
|
|
start_time = DateField('Start time')
|
2022-05-05 09:31:13 +00:00
|
|
|
end_time = DateField('End time', [validators.Optional()])
|
|
|
|
final_user_code = StringField(
|
|
|
|
'Final user code', [validators.Optional(), validators.length(max=50)]
|
|
|
|
)
|
|
|
|
transaction = StringField(
|
|
|
|
'Transaction', [validators.Optional(), validators.length(max=50)]
|
|
|
|
)
|
2022-05-12 08:05:37 +00:00
|
|
|
end_users = IntegerField('Number of end users', [validators.Optional()])
|
2022-02-01 12:40:06 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
2022-05-05 11:10:02 +00:00
|
|
|
if not super().validate(extra_validators):
|
|
|
|
return False
|
2022-02-01 12:40:06 +00:00
|
|
|
|
2022-04-21 12:01:33 +00:00
|
|
|
if self.type.data not in ['Allocate', 'Deallocate']:
|
|
|
|
return False
|
|
|
|
|
2022-05-05 11:10:02 +00:00
|
|
|
if not self.validate_dates():
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self.check_devices():
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def validate_dates(self):
|
2022-02-01 12:40:06 +00:00
|
|
|
start_time = self.start_time.data
|
|
|
|
end_time = self.end_time.data
|
2022-05-05 11:10:02 +00:00
|
|
|
|
|
|
|
if not start_time:
|
|
|
|
self.start_time.errors = ['Not a valid date value.!']
|
|
|
|
return False
|
|
|
|
|
2022-02-01 12:40:06 +00:00
|
|
|
if start_time and end_time and end_time < start_time:
|
|
|
|
error = ['The action cannot finish before it starts.']
|
|
|
|
self.end_time.errors = error
|
2022-05-05 11:10:02 +00:00
|
|
|
return False
|
2022-02-01 12:40:06 +00:00
|
|
|
|
2022-05-05 11:10:02 +00:00
|
|
|
if not end_time:
|
2022-05-05 09:31:13 +00:00
|
|
|
self.end_time.data = self.start_time.data
|
|
|
|
|
2022-05-05 11:10:02 +00:00
|
|
|
return True
|
2022-02-02 11:52:16 +00:00
|
|
|
|
2022-05-05 11:10:02 +00:00
|
|
|
def check_devices(self):
|
2022-05-05 09:31:13 +00:00
|
|
|
if self.type.data == 'Allocate':
|
|
|
|
txt = "You need deallocate before allocate this device again"
|
|
|
|
for device in self._devices:
|
|
|
|
if device.allocated:
|
|
|
|
self.devices.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
|
|
|
device.allocated = True
|
|
|
|
|
|
|
|
if self.type.data == 'Deallocate':
|
|
|
|
txt = "Sorry some of this devices are actually deallocate"
|
|
|
|
for device in self._devices:
|
|
|
|
if not device.allocated:
|
|
|
|
self.devices.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
|
|
|
device.allocated = False
|
2022-02-02 11:52:16 +00:00
|
|
|
|
2022-05-05 11:10:02 +00:00
|
|
|
return True
|
2022-02-03 10:27:16 +00:00
|
|
|
|
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
class DataWipeDocumentForm(Form):
|
2022-02-22 10:55:01 +00:00
|
|
|
date = DateField(
|
|
|
|
'Date', [validators.Optional()], description="Date when was data wipe"
|
|
|
|
)
|
|
|
|
url = URLField(
|
|
|
|
'Url', [validators.Optional()], description="Url where the document resides"
|
|
|
|
)
|
|
|
|
success = BooleanField(
|
|
|
|
'Success', [validators.Optional()], description="The erase was success or not?"
|
|
|
|
)
|
|
|
|
software = StringField(
|
|
|
|
'Software',
|
|
|
|
[validators.Optional()],
|
|
|
|
description="Which software has you use for erase the disks",
|
|
|
|
)
|
|
|
|
id_document = StringField(
|
|
|
|
'Document Id',
|
|
|
|
[validators.Optional()],
|
|
|
|
description="Identification number of document",
|
|
|
|
)
|
|
|
|
file_name = FileField(
|
|
|
|
'File',
|
|
|
|
[validators.DataRequired()],
|
|
|
|
description="""This file is not stored on our servers, it is only used to
|
|
|
|
generate a digital signature and obtain the name of the file.""",
|
|
|
|
)
|
2022-02-03 10:27:16 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
return is_valid
|
2022-02-07 13:02:28 +00:00
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
def save(self, commit=True):
|
2022-02-07 13:02:28 +00:00
|
|
|
file_name = ''
|
|
|
|
file_hash = ''
|
|
|
|
if self.file_name.data:
|
|
|
|
file_name = self.file_name.data.filename
|
|
|
|
file_hash = insert_hash(self.file_name.data.read(), commit=False)
|
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
self.url.data = URL(self.url.data)
|
|
|
|
self._obj = DataWipeDocument(
|
|
|
|
document_type='DataWipeDocument',
|
2022-02-07 13:02:28 +00:00
|
|
|
)
|
2022-02-08 12:59:50 +00:00
|
|
|
self.populate_obj(self._obj)
|
|
|
|
self._obj.file_name = file_name
|
|
|
|
self._obj.file_hash = file_hash
|
|
|
|
db.session.add(self._obj)
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|
2022-02-07 13:02:28 +00:00
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class DataWipeForm(ActionFormMixin):
|
2022-02-08 12:59:50 +00:00
|
|
|
document = FormField(DataWipeDocumentForm)
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
self.document.form.save(commit=False)
|
2022-02-07 13:02:28 +00:00
|
|
|
|
|
|
|
Model = db.Model._decl_class_registry.data[self.type.data]()
|
|
|
|
self.instance = Model()
|
|
|
|
devices = self.devices.data
|
|
|
|
severity = self.severity.data
|
|
|
|
self.devices.data = self._devices
|
|
|
|
self.severity.data = Severity[self.severity.data]
|
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
document = copy.copy(self.document)
|
|
|
|
del self.document
|
2022-02-07 13:02:28 +00:00
|
|
|
self.populate_obj(self.instance)
|
2022-02-08 12:59:50 +00:00
|
|
|
self.instance.document = document.form._obj
|
2022-02-07 13:02:28 +00:00
|
|
|
db.session.add(self.instance)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
self.devices.data = devices
|
|
|
|
self.severity.data = severity
|
2022-02-08 12:59:50 +00:00
|
|
|
self.document = document
|
2022-02-07 13:02:28 +00:00
|
|
|
|
|
|
|
return self.instance
|
2022-02-09 13:06:28 +00:00
|
|
|
|
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class TradeForm(ActionFormMixin):
|
2022-02-22 10:55:01 +00:00
|
|
|
user_from = StringField(
|
|
|
|
'Supplier',
|
|
|
|
[validators.Optional()],
|
|
|
|
description="Please enter the supplier's email address",
|
|
|
|
render_kw={'data-email': ""},
|
|
|
|
)
|
|
|
|
user_to = StringField(
|
|
|
|
'Receiver',
|
|
|
|
[validators.Optional()],
|
|
|
|
description="Please enter the receiver's email address",
|
|
|
|
render_kw={'data-email': ""},
|
|
|
|
)
|
|
|
|
confirm = BooleanField(
|
|
|
|
'Confirm',
|
|
|
|
[validators.Optional()],
|
|
|
|
default=True,
|
|
|
|
description="I need confirmation from the other user for every device and document.",
|
|
|
|
)
|
|
|
|
code = StringField(
|
|
|
|
'Code',
|
|
|
|
[validators.Optional()],
|
|
|
|
description="If you don't need confirm, you need put a code for trace the user in the statistics.",
|
|
|
|
)
|
2022-02-10 12:22:47 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-02-18 08:30:20 +00:00
|
|
|
self.user_from.render_kw['data-email'] = g.user.email
|
|
|
|
self.user_to.render_kw['data-email'] = g.user.email
|
2022-02-22 10:55:01 +00:00
|
|
|
self._lot = (
|
2022-03-01 13:29:25 +00:00
|
|
|
Lot.query.outerjoin(Trade)
|
|
|
|
.filter(Lot.id == self.lot.data)
|
2022-03-08 10:01:10 +00:00
|
|
|
.filter(
|
|
|
|
or_(
|
|
|
|
Trade.user_from == g.user,
|
|
|
|
Trade.user_to == g.user,
|
|
|
|
Lot.owner_id == g.user.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.one()
|
2022-02-22 10:55:01 +00:00
|
|
|
)
|
2022-02-10 12:22:47 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
2022-02-17 12:52:55 +00:00
|
|
|
is_valid = self.generic_validation(extra_validators=extra_validators)
|
2022-02-18 12:37:45 +00:00
|
|
|
email_from = self.user_from.data
|
|
|
|
email_to = self.user_to.data
|
2022-02-10 12:22:47 +00:00
|
|
|
|
2022-04-21 12:01:33 +00:00
|
|
|
if self.type.data != "Trade":
|
|
|
|
return False
|
|
|
|
|
2022-02-10 12:53:52 +00:00
|
|
|
if not self.confirm.data and not self.code.data:
|
2022-02-22 11:04:26 +00:00
|
|
|
self.code.errors = ["If you don't want to confirm, you need a code"]
|
2022-02-10 12:22:47 +00:00
|
|
|
is_valid = False
|
2022-02-17 12:52:55 +00:00
|
|
|
|
2022-02-22 10:55:01 +00:00
|
|
|
if (
|
|
|
|
self.confirm.data
|
|
|
|
and not (email_from and email_to)
|
|
|
|
or email_to == email_from
|
|
|
|
or g.user.email not in [email_from, email_to]
|
|
|
|
):
|
2022-02-10 12:53:52 +00:00
|
|
|
|
2022-02-18 12:37:45 +00:00
|
|
|
errors = ["If you want confirm, you need a correct email"]
|
|
|
|
self.user_to.errors = errors
|
|
|
|
self.user_from.errors = errors
|
2022-02-10 12:22:47 +00:00
|
|
|
|
2022-02-10 12:53:52 +00:00
|
|
|
is_valid = False
|
|
|
|
|
2022-02-16 13:03:01 +00:00
|
|
|
if self.confirm.data and is_valid:
|
2022-02-18 12:37:45 +00:00
|
|
|
user_to = User.query.filter_by(email=email_to).first() or g.user
|
|
|
|
user_from = User.query.filter_by(email=email_from).first() or g.user
|
2022-02-16 13:03:01 +00:00
|
|
|
if user_to == user_from:
|
|
|
|
is_valid = False
|
|
|
|
else:
|
2022-02-18 12:37:45 +00:00
|
|
|
self.db_user_to = user_to
|
|
|
|
self.db_user_from = user_from
|
2022-02-16 13:03:01 +00:00
|
|
|
|
2022-02-22 11:04:26 +00:00
|
|
|
self.has_errors = not is_valid
|
2022-02-10 12:53:52 +00:00
|
|
|
return is_valid
|
2022-02-16 13:03:01 +00:00
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
def save(self, commit=True):
|
2022-02-22 11:04:26 +00:00
|
|
|
if self.has_errors:
|
|
|
|
raise ValueError(
|
|
|
|
"The %s could not be saved because the data didn't validate."
|
|
|
|
% (self.instance._meta.object_name)
|
|
|
|
)
|
2022-02-22 10:24:11 +00:00
|
|
|
if not self.confirm.data:
|
|
|
|
self.create_phantom_account()
|
2022-02-17 12:52:55 +00:00
|
|
|
self.prepare_instance()
|
|
|
|
self.create_automatic_trade()
|
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
2022-02-17 12:52:55 +00:00
|
|
|
|
|
|
|
return self.instance
|
|
|
|
|
|
|
|
def prepare_instance(self):
|
2022-02-21 12:02:28 +00:00
|
|
|
Model = db.Model._decl_class_registry.data['Trade']()
|
2022-02-16 13:03:01 +00:00
|
|
|
self.instance = Model()
|
2022-02-18 12:37:45 +00:00
|
|
|
self.instance.user_from = self.db_user_from
|
|
|
|
self.instance.user_to = self.db_user_to
|
2022-02-16 13:03:01 +00:00
|
|
|
self.instance.lot_id = self._lot.id
|
2022-02-18 08:30:20 +00:00
|
|
|
self.instance.devices = self._lot.devices
|
2022-02-16 13:03:01 +00:00
|
|
|
self.instance.code = self.code.data
|
|
|
|
self.instance.confirm = self.confirm.data
|
|
|
|
self.instance.date = self.date.data
|
|
|
|
self.instance.name = self.name.data
|
|
|
|
self.instance.description = self.description.data
|
|
|
|
db.session.add(self.instance)
|
|
|
|
|
2022-02-17 12:52:55 +00:00
|
|
|
def create_phantom_account(self):
|
|
|
|
"""
|
|
|
|
If exist both users not to do nothing
|
|
|
|
If exist from but not to:
|
|
|
|
search if exist in the DB
|
|
|
|
if exist use it
|
|
|
|
else create new one
|
|
|
|
The same if exist to but not from
|
|
|
|
|
|
|
|
"""
|
2022-02-18 08:30:20 +00:00
|
|
|
user_from = self.user_from.data
|
|
|
|
user_to = self.user_to.data
|
2022-02-17 12:52:55 +00:00
|
|
|
code = self.code.data
|
|
|
|
|
|
|
|
if user_from and user_to:
|
2022-02-22 10:12:15 +00:00
|
|
|
# both users exist, no further action is necessary
|
2022-02-17 12:52:55 +00:00
|
|
|
return
|
|
|
|
|
2022-02-22 10:13:00 +00:00
|
|
|
# Create receiver (to) phantom account
|
2022-02-17 12:52:55 +00:00
|
|
|
if user_from and not user_to:
|
|
|
|
assert g.user.email == user_from
|
2022-02-25 09:07:44 +00:00
|
|
|
|
2022-02-17 12:52:55 +00:00
|
|
|
self.user_from = g.user
|
2022-02-22 10:55:01 +00:00
|
|
|
self.user_to = self.get_or_create_user(code)
|
2022-02-17 12:52:55 +00:00
|
|
|
|
2022-02-22 10:13:00 +00:00
|
|
|
# Create supplier (from) phantom account
|
2022-02-17 12:52:55 +00:00
|
|
|
if not user_from and user_to:
|
|
|
|
assert g.user.email == user_to
|
2022-02-25 09:07:44 +00:00
|
|
|
|
2022-02-22 10:55:01 +00:00
|
|
|
self.user_from = self.get_or_create_user(code)
|
2022-02-17 12:52:55 +00:00
|
|
|
self.user_to = g.user
|
|
|
|
|
2022-03-02 09:56:31 +00:00
|
|
|
self.db_user_to = self.user_to
|
|
|
|
self.db_user_from = self.user_from
|
|
|
|
|
2022-02-22 10:24:11 +00:00
|
|
|
def get_or_create_user(self, code):
|
2022-02-17 12:52:55 +00:00
|
|
|
email = "{}_{}@dhub.com".format(str(g.user.id), code)
|
|
|
|
user = User.query.filter_by(email=email).first()
|
|
|
|
if not user:
|
|
|
|
user = User(email=email, password='', active=False, phantom=True)
|
|
|
|
db.session.add(user)
|
|
|
|
return user
|
|
|
|
|
|
|
|
def create_automatic_trade(self):
|
|
|
|
# This method change the ownership of devices
|
|
|
|
# do nothing if an explicit confirmation is required
|
|
|
|
if self.confirm.data:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Change the owner for every devices
|
|
|
|
for dev in self._lot.devices:
|
2022-02-18 12:37:45 +00:00
|
|
|
dev.change_owner(self.db_user_to)
|
|
|
|
|
|
|
|
def check_valid(self):
|
|
|
|
if self.user_from.data == self.user_to.data:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.user_from.data == g.user.email:
|
|
|
|
return 'user_to'
|
|
|
|
|
|
|
|
if self.user_to.data == g.user.email:
|
2022-02-21 12:02:28 +00:00
|
|
|
return 'user_from'
|
|
|
|
|
|
|
|
|
|
|
|
class TradeDocumentForm(FlaskForm):
|
2022-02-22 10:55:01 +00:00
|
|
|
url = URLField(
|
|
|
|
'Url',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="Url where the document resides",
|
|
|
|
)
|
|
|
|
description = StringField(
|
|
|
|
'Description',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="",
|
|
|
|
)
|
|
|
|
id_document = StringField(
|
|
|
|
'Document Id',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="Identification number of document",
|
|
|
|
)
|
|
|
|
date = DateField(
|
|
|
|
'Date',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="",
|
|
|
|
)
|
|
|
|
file_name = FileField(
|
|
|
|
'File',
|
|
|
|
[validators.DataRequired()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="""This file is not stored on our servers, it is only used to
|
|
|
|
generate a digital signature and obtain the name of the file.""",
|
|
|
|
)
|
2022-02-21 12:02:28 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
lot_id = kwargs.pop('lot')
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-02-22 10:55:01 +00:00
|
|
|
self._lot = Lot.query.filter(Lot.id == lot_id).one()
|
2022-02-21 12:02:28 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if g.user not in [self._lot.trade.user_from, self._lot.trade.user_to]:
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
file_name = ''
|
|
|
|
file_hash = ''
|
|
|
|
if self.file_name.data:
|
|
|
|
file_name = self.file_name.data.filename
|
|
|
|
file_hash = insert_hash(self.file_name.data.read(), commit=False)
|
|
|
|
|
|
|
|
self.url.data = URL(self.url.data)
|
|
|
|
self._obj = TradeDocument(lot_id=self._lot.id)
|
|
|
|
self.populate_obj(self._obj)
|
|
|
|
self._obj.file_name = file_name
|
|
|
|
self._obj.file_hash = file_hash
|
|
|
|
db.session.add(self._obj)
|
|
|
|
self._lot.trade.documents.add(self._obj)
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|