2022-02-08 12:59:50 +00:00
|
|
|
import copy
|
2022-09-12 08:55:49 +00:00
|
|
|
import csv
|
2022-05-26 15:55:52 +00:00
|
|
|
import datetime
|
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-07-04 09:26:24 +00:00
|
|
|
import pandas as pd
|
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
|
|
|
|
|
2023-06-14 08:17:01 +00:00
|
|
|
from ereuse_devicehub import messages
|
2022-02-22 10:55:01 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2023-02-14 19:03:33 +00:00
|
|
|
from ereuse_devicehub.inventory.models import (
|
|
|
|
DeliveryNote,
|
2023-03-31 16:06:22 +00:00
|
|
|
DeviceDocument,
|
2023-02-14 19:03:33 +00:00
|
|
|
ReceiverNote,
|
|
|
|
Transfer,
|
|
|
|
TransferCustomerDetails,
|
|
|
|
)
|
2023-01-10 09:49:32 +00:00
|
|
|
from ereuse_devicehub.parser.models import PlaceholdersLog, 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
|
2023-06-19 09:31:40 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Snapshot, Trade, VisualTest
|
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,
|
2023-01-26 10:35:08 +00:00
|
|
|
Computer,
|
2022-05-16 10:40:14 +00:00
|
|
|
ComputerMonitor,
|
2023-05-23 11:45:32 +00:00
|
|
|
DataStorage,
|
2022-06-29 09:51:03 +00:00
|
|
|
Desktop,
|
2022-03-08 10:01:10 +00:00
|
|
|
Device,
|
2023-06-09 16:17:06 +00:00
|
|
|
HardDrive,
|
2022-03-08 10:01:10 +00:00
|
|
|
Keyboard,
|
2022-06-29 09:51:03 +00:00
|
|
|
Laptop,
|
2022-03-08 10:01:10 +00:00
|
|
|
MemoryCardReader,
|
2023-06-09 16:17:06 +00:00
|
|
|
Mobile,
|
2022-11-22 14:44:44 +00:00
|
|
|
Monitor,
|
2022-03-08 10:01:10 +00:00
|
|
|
Mouse,
|
2022-11-29 15:49:23 +00:00
|
|
|
Other,
|
2022-06-28 15:39:08 +00:00
|
|
|
Placeholder,
|
2022-11-22 14:44:44 +00:00
|
|
|
Projector,
|
2022-06-29 09:51:03 +00:00
|
|
|
Server,
|
2022-03-08 10:01:10 +00:00
|
|
|
Smartphone,
|
2023-06-09 16:17:06 +00:00
|
|
|
SolidStateDrive,
|
2022-03-08 10:01:10 +00:00
|
|
|
Tablet,
|
2022-11-22 14:44:44 +00:00
|
|
|
TelevisionSet,
|
2022-03-08 10:01:10 +00:00
|
|
|
)
|
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
|
2023-04-28 10:10:25 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, ShareLot
|
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",
|
|
|
|
"Tablet",
|
|
|
|
"Smartphone",
|
|
|
|
"Cellphone",
|
|
|
|
],
|
2022-10-14 13:33:43 +00:00
|
|
|
"Drives & Storage": [
|
|
|
|
"All DataStorage",
|
2022-11-22 10:58:56 +00:00
|
|
|
"HardDrive",
|
2023-06-09 16:17:06 +00:00
|
|
|
"SolidStateDrive",
|
2022-10-14 13:33:43 +00:00
|
|
|
],
|
2022-11-22 14:44:44 +00:00
|
|
|
"Accessories": [
|
|
|
|
"All Accessories",
|
|
|
|
"Mouse",
|
|
|
|
"MemoryCardReader",
|
|
|
|
"SAI",
|
|
|
|
"Keyboard",
|
|
|
|
],
|
2022-11-29 15:49:23 +00:00
|
|
|
"Other Devices": ["Other"],
|
2022-03-15 09:02:30 +00:00
|
|
|
}
|
2022-03-07 12:07:26 +00:00
|
|
|
|
2023-04-04 15:23:50 +00:00
|
|
|
TYPES_DOCUMENTS = [
|
|
|
|
("", ""),
|
|
|
|
("image", "Image"),
|
|
|
|
("main_image", "Main Image"),
|
|
|
|
("functionality_report", "Functionality Report"),
|
|
|
|
("data_sanitization_report", "Data Sanitization Report"),
|
|
|
|
("disposition_report", "Disposition Report"),
|
|
|
|
]
|
|
|
|
|
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-10-14 13:33:43 +00:00
|
|
|
STORAGE = ["HardDrive", "SolidStateDrive"]
|
2022-11-22 14:44:44 +00:00
|
|
|
ACCESSORIES = ["Mouse", "MemoryCardReader", "SAI", "Keyboard"]
|
2022-11-29 15:49:23 +00:00
|
|
|
OTHERS = ["Other"]
|
2023-06-09 16:17:06 +00:00
|
|
|
DATASTORAGE = ['HardDrive', 'SolidStateDrive']
|
2022-05-09 11:43:02 +00:00
|
|
|
|
2022-03-07 12:07:26 +00:00
|
|
|
|
2022-06-02 11:46:10 +00:00
|
|
|
class AdvancedSearchForm(FlaskForm):
|
|
|
|
q = StringField('Search', [validators.length(min=1)])
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
dhids = [x.strip() for x in self.q.data.split(' ')]
|
|
|
|
if ',' in self.q.data:
|
|
|
|
dhids = [x.strip() for x in self.q.data.split(',')]
|
|
|
|
if ';' in self.q.data:
|
|
|
|
dhids = [x.strip() for x in self.q.data.split(';')]
|
|
|
|
|
|
|
|
self.devices = []
|
|
|
|
if dhids:
|
|
|
|
self.search(dhids)
|
|
|
|
|
|
|
|
def search(self, dhids):
|
2022-08-30 13:12:14 +00:00
|
|
|
query = Device.query.filter(Device.owner_id == g.user.id)
|
|
|
|
self.devices = query.join(Device.placeholder).filter(
|
|
|
|
or_(
|
|
|
|
Device.devicehub_id.in_(dhids),
|
|
|
|
Placeholder.phid.in_(dhids),
|
|
|
|
)
|
|
|
|
)
|
2022-06-02 11:46:10 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-04-28 10:10:25 +00:00
|
|
|
def __init__(self, lots, lot, lot_id, *args, **kwargs):
|
2022-09-09 09:48:23 +00:00
|
|
|
self.all_devices = kwargs.pop('all_devices', False)
|
2022-03-08 09:30:53 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2022-05-09 11:43:02 +00:00
|
|
|
self.lots = lots
|
2023-04-28 10:10:25 +00:00
|
|
|
self.lot = lot
|
2022-05-09 11:43:02 +00:00
|
|
|
self.lot_id = lot_id
|
2023-04-28 10:10:25 +00:00
|
|
|
if self.lot_id and not self.lot:
|
|
|
|
self.lot = self.lots.filter(Lot.id == self.lot_id).one()
|
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):
|
2023-04-28 10:10:25 +00:00
|
|
|
if self.lot:
|
2022-05-09 11:43:02 +00:00
|
|
|
device_ids = (d.id for d in self.lot.devices)
|
2022-07-15 14:47:00 +00:00
|
|
|
self.devices = Device.query.filter(Device.id.in_(device_ids)).filter(
|
2022-08-30 11:10:35 +00:00
|
|
|
Device.binding == None # noqa: E711
|
2022-07-15 14:47:00 +00:00
|
|
|
)
|
2022-05-09 11:43:02 +00:00
|
|
|
else:
|
2022-07-15 14:47:00 +00:00
|
|
|
self.devices = Device.query.filter(Device.owner_id == g.user.id).filter(
|
2022-08-30 11:10:35 +00:00
|
|
|
Device.binding == None # noqa: E711
|
2022-07-15 14:47:00 +00:00
|
|
|
)
|
2022-09-09 09:48:23 +00:00
|
|
|
if not self.all_devices:
|
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:
|
2023-06-09 16:17:06 +00:00
|
|
|
filter_type = COMPUTERS + MONITORS + MOBILE + DATASTORAGE + OTHERS
|
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
|
|
|
|
|
2022-10-14 13:33:43 +00:00
|
|
|
elif "All DataStorage" == self.device_type:
|
|
|
|
filter_type = STORAGE
|
|
|
|
|
2022-11-22 14:44:44 +00:00
|
|
|
elif "All Accessories" == self.device_type:
|
|
|
|
filter_type = ACCESSORIES
|
|
|
|
|
2022-05-09 11:43:02 +00:00
|
|
|
if filter_type:
|
|
|
|
self.devices = self.devices.filter(Device.type.in_(filter_type))
|
2022-03-07 14:17:03 +00:00
|
|
|
|
2023-01-17 12:18:18 +00:00
|
|
|
return self.devices.filter(Device.active.is_(True)).order_by(
|
2022-12-23 17:33:49 +00:00
|
|
|
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):
|
2023-04-28 10:10:25 +00:00
|
|
|
shared = ShareLot.query.filter_by(lot=self.instance).first()
|
|
|
|
if self.instance and not self.instance.trade and not shared:
|
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
|
|
|
|
2023-01-13 16:42:42 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.create_new_devices = kwargs.pop('create_new_devices', False)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
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
|
|
|
|
|
2023-01-26 09:46:34 +00:00
|
|
|
def save(self, commit=True, user_trusts=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)
|
2022-06-13 15:36:03 +00:00
|
|
|
debug = snapshot_json.pop('debug', None)
|
|
|
|
self.version = snapshot_json.get('schema_api')
|
|
|
|
self.uuid = snapshot_json.get('uuid')
|
|
|
|
self.sid = snapshot_json.get('sid')
|
|
|
|
|
|
|
|
if self.is_wb_lite_snapshot(self.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-06-13 15:36:03 +00:00
|
|
|
else:
|
|
|
|
self.version = snapshot_json.get('version')
|
|
|
|
system_uuid = self.get_uuid(debug)
|
2022-06-14 08:48:01 +00:00
|
|
|
if system_uuid:
|
2022-06-15 10:18:44 +00:00
|
|
|
snapshot_json['device']['system_uuid'] = system_uuid
|
2022-11-16 10:51:31 +00:00
|
|
|
self.get_fields_extra(debug, 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)
|
2023-01-13 16:42:42 +00:00
|
|
|
response = self.build(
|
|
|
|
snapshot_json, create_new_device=self.create_new_devices
|
|
|
|
)
|
2022-04-05 07:22:02 +00:00
|
|
|
except ValidationError as err:
|
|
|
|
txt = "{}".format(err)
|
2022-06-13 15:36:03 +00:00
|
|
|
self.errors(txt=txt)
|
2022-04-05 07:22:02 +00:00
|
|
|
self.result[filename] = 'Error'
|
|
|
|
continue
|
|
|
|
|
2023-01-26 10:35:08 +00:00
|
|
|
if isinstance(response.device, Computer):
|
|
|
|
response.device.user_trusts = user_trusts
|
2022-04-08 09:12:17 +00:00
|
|
|
db.session.add(response)
|
2022-09-22 10:08:37 +00:00
|
|
|
devices.append(response.device.binding.device)
|
2022-01-18 10:45:28 +00:00
|
|
|
|
|
|
|
if hasattr(response, 'type'):
|
|
|
|
self.result[filename] = 'Ok'
|
2022-06-13 15:36:03 +00:00
|
|
|
self.errors(txt="Ok", severity=Severity.Info, snapshot=response)
|
2022-01-18 10:45:28 +00:00
|
|
|
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()])
|
2022-06-28 15:39:08 +00:00
|
|
|
amount = IntegerField(
|
|
|
|
'Amount',
|
|
|
|
[validators.DataRequired(), validators.NumberRange(min=1, max=100)],
|
|
|
|
default=1,
|
|
|
|
)
|
|
|
|
id_device_supplier = StringField('Id Supplier', [validators.Optional()])
|
2022-10-05 07:18:42 +00:00
|
|
|
id_device_internal = StringField('Id Internal', [validators.Optional()])
|
2022-06-28 15:39:08 +00:00
|
|
|
pallet = StringField('Identity of pallet', [validators.Optional()])
|
2022-08-09 08:49:56 +00:00
|
|
|
components = TextAreaField('Components', [validators.Optional()])
|
2022-06-28 15:39:08 +00:00
|
|
|
info = TextAreaField('Info', [validators.Optional()])
|
|
|
|
model = StringField('Model', [validators.Optional()])
|
|
|
|
manufacturer = StringField('Manufacturer', [validators.Optional()])
|
2022-09-13 16:15:50 +00:00
|
|
|
part_number = StringField('Part Number', [validators.Optional()])
|
|
|
|
serial_number = StringField('Serial Number', [validators.Optional()])
|
2022-02-22 10:15:37 +00:00
|
|
|
appearance = StringField('Appearance', [validators.Optional()])
|
|
|
|
functionality = StringField('Functionality', [validators.Optional()])
|
|
|
|
brand = StringField('Brand')
|
|
|
|
version = StringField('Version')
|
2022-06-28 15:39:08 +00:00
|
|
|
weight = FloatField('Weight', [validators.Optional()])
|
|
|
|
width = FloatField('Width', [validators.Optional()])
|
|
|
|
height = FloatField('Height', [validators.Optional()])
|
|
|
|
depth = FloatField('Depth', [validators.Optional()])
|
2022-02-22 10:15:37 +00:00
|
|
|
variant = StringField('Variant', [validators.Optional()])
|
|
|
|
sku = StringField('SKU', [validators.Optional()])
|
2022-10-25 15:54:58 +00:00
|
|
|
image = URLField('Image', [validators.Optional(), validators.URL()])
|
2022-02-22 10:15:37 +00:00
|
|
|
imei = IntegerField('IMEI', [validators.Optional()])
|
2023-06-12 16:47:55 +00:00
|
|
|
data_storage_size = FloatField('Storage Size', [validators.Optional()])
|
2022-02-22 10:15:37 +00:00
|
|
|
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):
|
2022-07-18 08:56:36 +00:00
|
|
|
self.objs = set()
|
2022-07-05 16:09:47 +00:00
|
|
|
self._obj = kwargs.pop('_obj', None)
|
2022-01-18 13:09:01 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2022-07-05 16:09:47 +00:00
|
|
|
if self._obj:
|
|
|
|
self.type.data = self._obj.type
|
|
|
|
if not request.form:
|
|
|
|
self.reset_from_obj()
|
2022-02-22 10:55:01 +00:00
|
|
|
self.devices = {
|
2022-06-29 09:51:03 +00:00
|
|
|
"Laptop": Laptop,
|
|
|
|
"Desktop": Desktop,
|
|
|
|
"Server": Server,
|
2022-02-22 10:55:01 +00:00
|
|
|
"Smartphone": Smartphone,
|
|
|
|
"Tablet": Tablet,
|
|
|
|
"Cellphone": Cellphone,
|
2023-06-09 16:17:06 +00:00
|
|
|
"HardDrive": HardDrive,
|
|
|
|
"SolidStateDrive": SolidStateDrive,
|
2022-05-16 10:40:14 +00:00
|
|
|
"ComputerMonitor": ComputerMonitor,
|
2022-11-22 14:44:44 +00:00
|
|
|
"Monitor": Monitor,
|
|
|
|
"TelevisionSet": TelevisionSet,
|
|
|
|
"Projector": Projector,
|
2022-02-22 10:55:01 +00:00
|
|
|
"Mouse": Mouse,
|
|
|
|
"Keyboard": Keyboard,
|
|
|
|
"SAI": SAI,
|
|
|
|
"MemoryCardReader": MemoryCardReader,
|
2022-11-29 15:49:23 +00:00
|
|
|
"Other": Other,
|
2022-02-22 10:55:01 +00:00
|
|
|
}
|
2022-01-20 12:13:20 +00:00
|
|
|
|
2023-06-12 16:47:55 +00:00
|
|
|
def reset_from_obj(self): # noqa: C901
|
2022-07-05 16:09:47 +00:00
|
|
|
if not self._obj:
|
|
|
|
return
|
|
|
|
disabled = {'disabled': "disabled"}
|
|
|
|
appearance = self._obj.appearance()
|
|
|
|
functionality = self._obj.functionality()
|
|
|
|
if appearance:
|
|
|
|
appearance = appearance.name
|
|
|
|
if functionality:
|
|
|
|
functionality = functionality.name
|
|
|
|
self.type.render_kw = disabled
|
|
|
|
self.type.data = self._obj.type
|
|
|
|
self.amount.render_kw = disabled
|
|
|
|
self.id_device_supplier.data = self._obj.placeholder.id_device_supplier
|
2022-10-05 07:18:42 +00:00
|
|
|
self.id_device_internal.data = self._obj.placeholder.id_device_internal
|
2022-07-05 16:09:47 +00:00
|
|
|
self.pallet.data = self._obj.placeholder.pallet
|
|
|
|
self.info.data = self._obj.placeholder.info
|
2022-08-09 08:49:56 +00:00
|
|
|
self.components.data = self._obj.placeholder.components
|
2022-07-05 16:09:47 +00:00
|
|
|
self.serial_number.data = self._obj.serial_number
|
2022-09-13 16:15:50 +00:00
|
|
|
self.part_number.data = self._obj.part_number
|
2022-07-05 16:09:47 +00:00
|
|
|
self.model.data = self._obj.model
|
|
|
|
self.manufacturer.data = self._obj.manufacturer
|
|
|
|
self.appearance.data = appearance
|
|
|
|
self.functionality.data = functionality
|
|
|
|
self.brand.data = self._obj.brand
|
|
|
|
self.version.data = self._obj.version
|
|
|
|
self.weight.data = self._obj.weight
|
|
|
|
self.width.data = self._obj.width
|
|
|
|
self.height.data = self._obj.height
|
|
|
|
self.depth.data = self._obj.depth
|
|
|
|
self.variant.data = self._obj.variant
|
|
|
|
self.sku.data = self._obj.sku
|
2022-10-26 07:33:28 +00:00
|
|
|
if self._obj.image:
|
|
|
|
self.image.data = self._obj.image.to_text()
|
2022-07-05 16:09:47 +00:00
|
|
|
if self._obj.type in ['Smartphone', 'Tablet', 'Cellphone']:
|
|
|
|
self.imei.data = self._obj.imei
|
|
|
|
self.meid.data = self._obj.meid
|
2023-06-09 16:17:06 +00:00
|
|
|
self.data_storage_size.data = self._obj.data_storage_size
|
2022-07-05 16:09:47 +00:00
|
|
|
if self._obj.type == 'ComputerMonitor':
|
|
|
|
self.resolution.data = self._obj.resolution_width
|
|
|
|
self.screen.data = self._obj.size
|
2023-06-12 16:47:55 +00:00
|
|
|
if self._obj.type in ['HardDrive', 'SolidStateDrive']:
|
|
|
|
if self._obj.size:
|
|
|
|
self.data_storage_size.data = self._obj.size / 1000
|
2023-06-19 09:31:40 +00:00
|
|
|
if self._obj.appearance():
|
|
|
|
self.appearance.data = self._obj.appearance().name
|
|
|
|
if self._obj.functionality():
|
|
|
|
self.functionality.data = self._obj.functionality().name
|
2022-07-05 16:09:47 +00:00
|
|
|
|
2022-08-05 14:17:19 +00:00
|
|
|
if self._obj.placeholder.is_abstract:
|
|
|
|
self.type.render_kw = disabled
|
|
|
|
self.amount.render_kw = disabled
|
|
|
|
self.info.render_kw = disabled
|
2022-08-09 08:49:56 +00:00
|
|
|
self.components.render_kw = disabled
|
2022-08-05 14:17:19 +00:00
|
|
|
self.serial_number.render_kw = disabled
|
2022-09-13 16:15:50 +00:00
|
|
|
self.part_number.render_kw = disabled
|
2022-08-05 14:17:19 +00:00
|
|
|
self.model.render_kw = disabled
|
|
|
|
self.manufacturer.render_kw = disabled
|
|
|
|
self.appearance.render_kw = disabled
|
|
|
|
self.functionality.render_kw = disabled
|
|
|
|
self.brand.render_kw = disabled
|
|
|
|
self.version.render_kw = disabled
|
|
|
|
self.weight.render_kw = disabled
|
|
|
|
self.width.render_kw = disabled
|
|
|
|
self.height.render_kw = disabled
|
|
|
|
self.depth.render_kw = disabled
|
|
|
|
self.variant.render_kw = disabled
|
|
|
|
self.sku.render_kw = disabled
|
|
|
|
self.image.render_kw = disabled
|
|
|
|
if self._obj.type in ['Smartphone', 'Tablet', 'Cellphone']:
|
|
|
|
self.imei.render_kw = disabled
|
|
|
|
self.meid.render_kw = disabled
|
2023-06-09 16:17:06 +00:00
|
|
|
self.data_storage_size.render_kw = disabled
|
2023-06-12 16:47:55 +00:00
|
|
|
if self._obj.type in ['HardDrive', 'SolidStateDrive']:
|
|
|
|
self.data_storage_size.render_kw = disabled
|
2022-08-05 14:17:19 +00:00
|
|
|
if self._obj.type == 'ComputerMonitor':
|
|
|
|
self.resolution.render_kw = disabled
|
|
|
|
self.screen.render_kw = disabled
|
|
|
|
|
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-08-31 10:43:59 +00:00
|
|
|
if self.weight.data and not (0.1 <= self.weight.data <= 5):
|
|
|
|
txt = ["Supported values between 0.1 and 5"]
|
|
|
|
self.weight.errors = txt
|
2022-01-24 12:48:16 +00:00
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
2022-08-31 10:43:59 +00:00
|
|
|
if self.height.data and not (0.1 <= self.height.data <= 5):
|
|
|
|
txt = ["Supported values between 0.1 and 5"]
|
|
|
|
self.height.errors = txt
|
2022-01-24 12:48:16 +00:00
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
2022-08-31 10:43:59 +00:00
|
|
|
if self.width.data and not (0.1 <= self.width.data <= 5):
|
|
|
|
txt = ["Supported values between 0.1 and 5"]
|
|
|
|
self.width.errors = txt
|
2022-01-24 12:48:16 +00:00
|
|
|
is_valid = False
|
2022-01-20 12:13:20 +00:00
|
|
|
|
2022-08-31 10:43:59 +00:00
|
|
|
if self.depth.data and not (0.1 <= self.depth.data <= 5):
|
|
|
|
txt = ["Supported values between 0.1 and 5"]
|
|
|
|
self.depth.errors = txt
|
2022-01-24 12:48:16 +00:00
|
|
|
is_valid = False
|
|
|
|
|
2023-06-07 10:28:00 +00:00
|
|
|
# if self.imei.data and self.amount.data == 1:
|
|
|
|
# if not 13 < len(str(self.imei.data)) < 17:
|
|
|
|
# self.imei.errors = error
|
|
|
|
# is_valid = False
|
2022-01-24 12:48:16 +00:00
|
|
|
|
2022-06-29 15:31:54 +00:00
|
|
|
if self.meid.data and self.amount.data == 1:
|
2022-01-24 12:48:16 +00:00
|
|
|
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
|
2022-10-05 11:44:29 +00:00
|
|
|
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()
|
|
|
|
if self.part_number.data:
|
|
|
|
self.part_number.data = self.part_number.data.lower()
|
2022-01-21 10:07:15 +00:00
|
|
|
|
2022-06-29 15:31:54 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
2022-07-05 16:09:47 +00:00
|
|
|
if self._obj:
|
|
|
|
self.edit_device()
|
|
|
|
else:
|
|
|
|
for n in range(self.amount.data):
|
|
|
|
self.reset_ids()
|
|
|
|
self.create_device()
|
2022-06-29 11:40:00 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def create_device(self):
|
2022-07-04 09:26:24 +00:00
|
|
|
schema = SnapshotSchema()
|
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,
|
2022-09-13 16:15:50 +00:00
|
|
|
'partNumber': self.part_number.data,
|
2022-02-22 10:55:01 +00:00
|
|
|
'brand': self.brand.data,
|
|
|
|
'version': self.version.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-24 12:48:16 +00:00
|
|
|
snapshot_json = schema.load(json_snapshot)
|
2022-07-04 09:26:24 +00:00
|
|
|
device = snapshot_json['device']
|
2022-01-24 12:48:16 +00:00
|
|
|
|
2022-05-16 10:40:14 +00:00
|
|
|
if self.type.data == 'ComputerMonitor':
|
2022-07-04 09:26:24 +00:00
|
|
|
device.resolution_width = self.resolution.data
|
|
|
|
device.size = self.screen.data
|
2022-01-24 12:48:16 +00:00
|
|
|
|
|
|
|
if self.type.data in ['Smartphone', 'Tablet', 'Cellphone']:
|
2022-07-04 09:26:24 +00:00
|
|
|
device.imei = self.imei.data
|
|
|
|
device.meid = self.meid.data
|
2023-06-09 16:17:06 +00:00
|
|
|
device.data_storage_size = self.data_storage_size.data
|
|
|
|
|
|
|
|
if self.type.data in ['HardDrive', 'SolidStateDrive']:
|
2023-06-12 16:47:55 +00:00
|
|
|
if self.data_storage_size.data:
|
2023-06-13 09:07:24 +00:00
|
|
|
device.size = self.data_storage_size.data * 1000
|
2022-06-29 16:07:57 +00:00
|
|
|
|
2022-10-26 07:33:28 +00:00
|
|
|
device.image = URL(self.image.data)
|
|
|
|
|
2022-07-04 09:26:24 +00:00
|
|
|
device.placeholder = self.get_placeholder()
|
2022-12-19 19:03:34 +00:00
|
|
|
device.set_hid()
|
2022-07-04 09:26:24 +00:00
|
|
|
db.session.add(device)
|
2022-06-29 16:07:57 +00:00
|
|
|
|
2022-07-07 11:10:05 +00:00
|
|
|
placeholder_log = PlaceholdersLog(
|
|
|
|
type="New device", source='Web form', placeholder=device.placeholder
|
|
|
|
)
|
|
|
|
db.session.add(placeholder_log)
|
2022-07-18 08:56:36 +00:00
|
|
|
self.objs.add(device)
|
2022-07-07 11:10:05 +00:00
|
|
|
|
2022-06-29 16:07:57 +00:00
|
|
|
def reset_ids(self):
|
|
|
|
if self.amount.data > 1:
|
|
|
|
self.id_device_supplier.data = None
|
2022-10-05 07:18:42 +00:00
|
|
|
self.id_device_internal.data = None
|
2022-06-29 16:07:57 +00:00
|
|
|
self.serial_number.data = None
|
2022-09-13 16:15:50 +00:00
|
|
|
self.part_number.data = None
|
2022-06-29 16:07:57 +00:00
|
|
|
self.sku.data = None
|
|
|
|
self.imei.data = None
|
|
|
|
self.meid.data = None
|
|
|
|
|
2022-06-28 15:39:08 +00:00
|
|
|
def get_placeholder(self):
|
|
|
|
self.placeholder = Placeholder(
|
|
|
|
**{
|
|
|
|
'id_device_supplier': self.id_device_supplier.data,
|
2022-10-05 07:18:42 +00:00
|
|
|
'id_device_internal': self.id_device_internal.data,
|
2022-06-28 15:39:08 +00:00
|
|
|
'info': self.info.data,
|
2022-08-09 08:49:56 +00:00
|
|
|
'components': self.components.data,
|
2022-06-28 15:39:08 +00:00
|
|
|
'pallet': self.pallet.data,
|
2022-07-28 15:48:14 +00:00
|
|
|
'is_abstract': False,
|
2022-06-28 15:39:08 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return self.placeholder
|
|
|
|
|
2022-07-05 16:09:47 +00:00
|
|
|
def edit_device(self):
|
2022-08-05 14:17:19 +00:00
|
|
|
if not self._obj.placeholder.is_abstract:
|
|
|
|
self._obj.placeholder.id_device_supplier = (
|
|
|
|
self.id_device_supplier.data or None
|
|
|
|
)
|
2022-10-05 07:18:42 +00:00
|
|
|
self._obj.placeholder.id_device_internal = (
|
|
|
|
self.id_device_internal.data or None
|
|
|
|
)
|
2022-08-05 14:17:19 +00:00
|
|
|
self._obj.placeholder.info = self.info.data or None
|
2022-08-09 08:49:56 +00:00
|
|
|
self._obj.placeholder.components = self.components.data or None
|
2022-08-05 14:17:19 +00:00
|
|
|
self._obj.placeholder.pallet = self.pallet.data or None
|
|
|
|
self._obj.model = self.model.data
|
|
|
|
self._obj.manufacturer = self.manufacturer.data
|
|
|
|
self._obj.serial_number = self.serial_number.data
|
2022-09-13 16:15:50 +00:00
|
|
|
self._obj.part_number = self.part_number.data
|
2022-08-05 14:17:19 +00:00
|
|
|
self._obj.brand = self.brand.data
|
|
|
|
self._obj.version = self.version.data
|
|
|
|
self._obj.sku = self.sku.data
|
|
|
|
self._obj.weight = self.weight.data
|
|
|
|
self._obj.width = self.width.data
|
|
|
|
self._obj.height = self.height.data
|
|
|
|
self._obj.depth = self.depth.data
|
|
|
|
self._obj.variant = self.variant.data
|
2022-10-25 15:54:58 +00:00
|
|
|
self._obj.image = URL(self.image.data)
|
2022-08-05 14:17:19 +00:00
|
|
|
|
|
|
|
if self._obj.type == 'ComputerMonitor':
|
|
|
|
self._obj.resolution_width = self.resolution.data
|
|
|
|
self._obj.size = self.screen.data
|
|
|
|
|
|
|
|
if self._obj.type in ['Smartphone', 'Tablet', 'Cellphone']:
|
|
|
|
self._obj.imei = self.imei.data
|
|
|
|
self._obj.meid = self.meid.data
|
2023-06-09 16:17:06 +00:00
|
|
|
self._obj.data_storage_size = self.data_storage_size.data
|
2022-08-05 14:17:19 +00:00
|
|
|
|
2023-06-12 16:47:55 +00:00
|
|
|
if self.type.data in ['HardDrive', 'SolidStateDrive']:
|
|
|
|
if self.data_storage_size.data:
|
|
|
|
self._obj.size = self.data_storage_size.data * 1000
|
|
|
|
|
2023-06-19 09:31:40 +00:00
|
|
|
if not self._obj.appearance() or not self._obj.functionality():
|
|
|
|
visual_test = VisualTest(
|
|
|
|
appearance_range=self.appearance.data,
|
|
|
|
functionality_range=self.functionality.data,
|
|
|
|
device=self._obj,
|
|
|
|
)
|
|
|
|
db.session.add(visual_test)
|
|
|
|
else:
|
|
|
|
if (
|
|
|
|
self.appearance.data
|
|
|
|
and self.appearance.data != self._obj.appearance().name
|
|
|
|
):
|
|
|
|
self._obj.set_appearance(self.appearance.data)
|
|
|
|
|
|
|
|
if (
|
|
|
|
self.functionality.data
|
|
|
|
and self.functionality.data != self._obj.functionality().name
|
|
|
|
):
|
|
|
|
self._obj.set_functionality(self.functionality.data)
|
2022-07-05 16:09:47 +00:00
|
|
|
|
2023-04-21 10:47:53 +00:00
|
|
|
else:
|
|
|
|
self._obj.placeholder.id_device_supplier = (
|
|
|
|
self.id_device_supplier.data or None
|
|
|
|
)
|
|
|
|
self._obj.placeholder.id_device_internal = (
|
|
|
|
self.id_device_internal.data or None
|
|
|
|
)
|
|
|
|
self._obj.placeholder.pallet = self.pallet.data or None
|
2022-07-07 11:10:05 +00:00
|
|
|
placeholder_log = PlaceholdersLog(
|
|
|
|
type="Update", source='Web form', placeholder=self._obj.placeholder
|
|
|
|
)
|
|
|
|
db.session.add(placeholder_log)
|
2022-07-05 16:09:47 +00:00
|
|
|
|
2022-01-18 13:09:01 +00:00
|
|
|
|
2022-01-25 11:53:36 +00:00
|
|
|
class TagDeviceForm(FlaskForm):
|
2022-08-30 11:10:35 +00:00
|
|
|
tag = SelectField(
|
|
|
|
'Tag',
|
|
|
|
choices=[],
|
|
|
|
render_kw={
|
|
|
|
'class': 'form-control selectpicker',
|
|
|
|
'data-live-search': 'true',
|
|
|
|
},
|
|
|
|
)
|
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)
|
2022-08-30 11:10:35 +00:00
|
|
|
self.dhid = kwargs.pop('dhid', None)
|
|
|
|
self._device = (
|
|
|
|
Device.query.filter(Device.devicehub_id == self.dhid)
|
|
|
|
.filter(Device.owner_id == g.user.id)
|
|
|
|
.one()
|
|
|
|
)
|
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)
|
2022-08-30 11:10:35 +00:00
|
|
|
.filter_by(device_id=self._device.id)
|
2022-03-08 10:01:10 +00:00
|
|
|
.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-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
|
|
|
|
|
2023-05-23 11:45:32 +00:00
|
|
|
if self.type.data in [
|
|
|
|
'Allocate',
|
|
|
|
'Deallocate',
|
|
|
|
'Trade',
|
|
|
|
'DataWipe',
|
|
|
|
'EraseDataWipe',
|
|
|
|
]:
|
2022-04-21 12:01:33 +00:00
|
|
|
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-05-26 15:55:52 +00:00
|
|
|
if start_time > datetime.datetime.now().date():
|
|
|
|
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':
|
2022-05-26 15:55:52 +00:00
|
|
|
return self.check_allocate()
|
|
|
|
if self.type.data == 'Deallocate':
|
|
|
|
return self.check_deallocate()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check_allocate(self):
|
|
|
|
txt = "You need deallocate before allocate this device again"
|
|
|
|
for device in self._devices:
|
|
|
|
# | Allo - Deallo | Allo - Deallo |
|
|
|
|
|
|
|
|
allocates = [
|
|
|
|
ac for ac in device.actions if ac.type in ['Allocate', 'Deallocate']
|
|
|
|
]
|
|
|
|
allocates.sort(key=lambda x: x.start_time)
|
|
|
|
allocates.reverse()
|
|
|
|
last_deallocate = None
|
|
|
|
last_allocate = None
|
|
|
|
for ac in allocates:
|
|
|
|
if (
|
|
|
|
ac.type == 'Deallocate'
|
|
|
|
and ac.start_time.date() < self.start_time.data
|
|
|
|
):
|
|
|
|
# allow to do the action
|
|
|
|
break
|
|
|
|
|
|
|
|
# check if this action is between an old allocate - deallocate
|
|
|
|
if ac.type == 'Deallocate':
|
|
|
|
last_deallocate = ac
|
|
|
|
continue
|
|
|
|
|
|
|
|
if (
|
|
|
|
ac.type == 'Allocate'
|
|
|
|
and ac.start_time.date() > self.start_time.data
|
|
|
|
):
|
|
|
|
last_deallocate = None
|
|
|
|
last_allocate = None
|
|
|
|
continue
|
|
|
|
|
|
|
|
if ac.type == 'Allocate':
|
|
|
|
last_allocate = ac
|
|
|
|
|
|
|
|
if last_allocate or not last_deallocate:
|
2022-05-05 09:31:13 +00:00
|
|
|
self.devices.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
2022-05-26 15:55:52 +00:00
|
|
|
device.allocated = True
|
|
|
|
return True
|
2022-05-05 09:31:13 +00:00
|
|
|
|
2022-05-26 15:55:52 +00:00
|
|
|
def check_deallocate(self):
|
2022-09-22 11:16:14 +00:00
|
|
|
txt = "Error, some of this devices are actually deallocate"
|
2022-05-26 15:55:52 +00:00
|
|
|
for device in self._devices:
|
|
|
|
allocates = [
|
|
|
|
ac for ac in device.actions if ac.type in ['Allocate', 'Deallocate']
|
|
|
|
]
|
|
|
|
allocates.sort(key=lambda x: x.start_time)
|
|
|
|
allocates.reverse()
|
|
|
|
last_deallocate = None
|
|
|
|
last_allocate = None
|
|
|
|
|
|
|
|
for ac in allocates:
|
|
|
|
# check if this action is between an old allocate - deallocate
|
|
|
|
# | Allo - Deallo | Allo - Deallo |
|
|
|
|
# | Allo |
|
|
|
|
if (
|
|
|
|
ac.type == 'Allocate'
|
|
|
|
and ac.start_time.date() > self.start_time.data
|
|
|
|
):
|
|
|
|
last_allocate = None
|
|
|
|
last_deallocate = None
|
|
|
|
continue
|
|
|
|
|
|
|
|
if ac.type == 'Allocate' and not last_deallocate:
|
|
|
|
last_allocate = ac
|
|
|
|
break
|
|
|
|
|
|
|
|
if (
|
|
|
|
ac.type == 'Deallocate'
|
|
|
|
and ac.start_time.date() > self.start_time.data
|
|
|
|
):
|
|
|
|
last_deallocate = ac
|
|
|
|
continue
|
|
|
|
|
|
|
|
if ac.type == 'Deallocate':
|
|
|
|
last_allocate = None
|
|
|
|
|
|
|
|
if last_deallocate or not last_allocate:
|
2022-05-05 09:31:13 +00:00
|
|
|
self.devices.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
2022-05-26 15:55:52 +00:00
|
|
|
if not last_deallocate and not last_allocate:
|
|
|
|
self.devices.errors = [txt]
|
|
|
|
return False
|
2022-02-02 11:52:16 +00:00
|
|
|
|
2022-05-26 15:55:52 +00:00
|
|
|
device.allocated = False
|
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)
|
|
|
|
|
2023-06-14 08:17:01 +00:00
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
|
|
|
txt = "Error: Only Data Sanitization actions are "
|
|
|
|
txt += "allowed on Placeholders that are of the Data Storage type."
|
|
|
|
for dev in self._devices:
|
|
|
|
if dev.is_abstract() == 'Placeholder':
|
|
|
|
if not (isinstance(dev, DataStorage) or isinstance(dev, Mobile)):
|
|
|
|
messages.error(txt)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
2022-02-08 12:59:50 +00:00
|
|
|
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
|
2023-05-26 15:11:47 +00:00
|
|
|
if not self.document.success.data:
|
|
|
|
self.severity.data = Severity.Error.name
|
2022-02-07 13:02:28 +00:00
|
|
|
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
|
2023-05-23 11:45:32 +00:00
|
|
|
for dev in self._devices:
|
|
|
|
ac = None
|
2023-06-09 16:17:06 +00:00
|
|
|
if isinstance(dev, Mobile) or isinstance(dev, DataStorage):
|
|
|
|
ac = Model()
|
|
|
|
self.populate_obj(ac)
|
|
|
|
ac.device_id = dev.id
|
|
|
|
ac.document = document.form._obj
|
|
|
|
db.session.add(ac)
|
|
|
|
continue
|
|
|
|
|
2023-05-23 11:45:32 +00:00
|
|
|
for hd in dev.components:
|
|
|
|
if not isinstance(hd, DataStorage):
|
|
|
|
continue
|
|
|
|
ac = Model()
|
|
|
|
self.populate_obj(ac)
|
|
|
|
ac.parent = dev
|
|
|
|
ac.device = hd
|
|
|
|
ac.device_id = hd.id
|
|
|
|
ac.document = document.form._obj
|
|
|
|
db.session.add(ac)
|
2022-02-07 13:02:28 +00:00
|
|
|
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')
|
2023-03-28 15:09:47 +00:00
|
|
|
doc_id = kwargs.pop('document', None)
|
2022-02-22 10:55:01 +00:00
|
|
|
self._lot = Lot.query.filter(Lot.id == lot_id).one()
|
2023-03-29 11:40:27 +00:00
|
|
|
self._obj = None
|
2023-03-28 15:09:47 +00:00
|
|
|
if doc_id:
|
2023-03-29 11:40:27 +00:00
|
|
|
self._obj = TradeDocument.query.filter_by(
|
2023-03-28 15:09:47 +00:00
|
|
|
id=doc_id, lot=self._lot, owner=g.user
|
|
|
|
).one()
|
2023-03-29 11:40:27 +00:00
|
|
|
kwargs['obj'] = self._obj
|
|
|
|
|
2023-04-19 14:17:29 +00:00
|
|
|
if not self.file_name.args:
|
|
|
|
self.file_name.args = ("File", [validators.DataRequired()])
|
|
|
|
if doc_id:
|
2023-04-18 16:15:44 +00:00
|
|
|
self.file_name.args = ()
|
2023-03-29 11:40:27 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if self._obj:
|
|
|
|
if isinstance(self.url.data, URL):
|
|
|
|
self.url.data = self.url.data.to_text()
|
2022-02-21 12:02:28 +00:00
|
|
|
|
2022-09-23 09:54:19 +00:00
|
|
|
if not self._lot.transfer:
|
|
|
|
self.form_errors = ['Error, this lot is not a transfer lot.']
|
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
2022-09-23 09:54:19 +00:00
|
|
|
if g.user not in [self._lot.transfer.user_from, self._lot.transfer.user_to]:
|
2022-02-21 12:02:28 +00:00
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
file_name = ''
|
|
|
|
file_hash = ''
|
2023-04-18 16:15:44 +00:00
|
|
|
if self._obj:
|
|
|
|
file_name = self._obj.file_name
|
|
|
|
file_hash = self._obj.file_hash
|
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
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)
|
2023-03-29 11:40:27 +00:00
|
|
|
if not self._obj:
|
|
|
|
self._obj = TradeDocument(lot_id=self._lot.id)
|
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
self.populate_obj(self._obj)
|
2023-03-29 11:40:27 +00:00
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
self._obj.file_name = file_name
|
|
|
|
self._obj.file_hash = file_hash
|
2023-03-29 11:40:27 +00:00
|
|
|
|
|
|
|
if not self._obj.id:
|
|
|
|
db.session.add(self._obj)
|
|
|
|
self._lot.documents.add(self._obj)
|
|
|
|
|
2022-02-21 12:02:28 +00:00
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|
2022-05-27 14:32:22 +00:00
|
|
|
|
2023-03-28 15:09:47 +00:00
|
|
|
def remove(self):
|
2023-03-29 11:40:27 +00:00
|
|
|
if self._obj:
|
|
|
|
self._obj.delete()
|
2023-03-28 15:09:47 +00:00
|
|
|
db.session.commit()
|
2023-03-29 11:40:27 +00:00
|
|
|
return self._obj
|
2023-03-28 15:09:47 +00:00
|
|
|
|
2022-05-27 14:32:22 +00:00
|
|
|
|
2023-03-31 16:06:22 +00:00
|
|
|
class DeviceDocumentForm(FlaskForm):
|
|
|
|
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",
|
|
|
|
)
|
2023-04-04 15:23:50 +00:00
|
|
|
type = SelectField(
|
2023-03-31 16:06:22 +00:00
|
|
|
'Type',
|
|
|
|
[validators.Optional()],
|
2023-04-04 15:23:50 +00:00
|
|
|
choices=TYPES_DOCUMENTS,
|
|
|
|
default="",
|
|
|
|
render_kw={'class': "form-select"},
|
2023-03-31 16:06:22 +00:00
|
|
|
)
|
|
|
|
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.""",
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
id = kwargs.pop('dhid')
|
|
|
|
doc_id = kwargs.pop('document', None)
|
|
|
|
self._device = Device.query.filter(Device.devicehub_id == id).first()
|
|
|
|
self._obj = None
|
|
|
|
if doc_id:
|
|
|
|
self._obj = DeviceDocument.query.filter_by(
|
|
|
|
id=doc_id, device=self._device, owner=g.user
|
|
|
|
).one()
|
|
|
|
kwargs['obj'] = self._obj
|
|
|
|
|
2023-04-19 14:17:29 +00:00
|
|
|
if not self.file_name.args:
|
|
|
|
self.file_name.args = ("File", [validators.DataRequired()])
|
|
|
|
if doc_id:
|
2023-04-18 16:15:44 +00:00
|
|
|
self.file_name.args = ()
|
2023-03-31 16:06:22 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if self._obj:
|
|
|
|
if isinstance(self.url.data, URL):
|
|
|
|
self.url.data = self.url.data.to_text()
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
2023-04-04 14:56:27 +00:00
|
|
|
if g.user != self._device.owner:
|
2023-03-31 16:06:22 +00:00
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
file_name = ''
|
|
|
|
file_hash = ''
|
2023-04-18 16:15:44 +00:00
|
|
|
if self._obj:
|
|
|
|
file_name = self._obj.file_name
|
|
|
|
file_hash = self._obj.file_hash
|
|
|
|
|
2023-03-31 16:06:22 +00:00
|
|
|
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)
|
|
|
|
if not self._obj:
|
|
|
|
self._obj = DeviceDocument(device_id=self._device.id)
|
|
|
|
|
|
|
|
self.populate_obj(self._obj)
|
|
|
|
|
|
|
|
self._obj.file_name = file_name
|
|
|
|
self._obj.file_hash = file_hash
|
|
|
|
|
|
|
|
if not self._obj.id:
|
|
|
|
db.session.add(self._obj)
|
2023-04-04 14:56:27 +00:00
|
|
|
# self._device.documents.add(self._obj)
|
2023-03-31 16:06:22 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
if self._obj:
|
|
|
|
self._obj.delete()
|
|
|
|
db.session.commit()
|
|
|
|
return self._obj
|
|
|
|
|
|
|
|
|
2022-05-27 14:32:22 +00:00
|
|
|
class TransferForm(FlaskForm):
|
2022-10-10 09:10:39 +00:00
|
|
|
lot_name = StringField(
|
|
|
|
'Lot Name',
|
|
|
|
[validators.DataRequired()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="You need put a lot name",
|
|
|
|
)
|
2022-05-27 14:32:22 +00:00
|
|
|
code = StringField(
|
|
|
|
'Code',
|
2022-05-30 14:33:01 +00:00
|
|
|
[validators.DataRequired()],
|
2022-05-27 14:32:22 +00:00
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="You need put a code for transfer the external user",
|
|
|
|
)
|
|
|
|
description = TextAreaField(
|
|
|
|
'Description',
|
2022-05-30 14:33:01 +00:00
|
|
|
[validators.Optional()],
|
2022-05-27 14:32:22 +00:00
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
)
|
|
|
|
type = HiddenField()
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-05-30 14:33:01 +00:00
|
|
|
self._type = kwargs.get('type')
|
|
|
|
lot_id = kwargs.pop('lot_id', None)
|
2022-10-06 14:56:12 +00:00
|
|
|
self._tmp_lot = None
|
|
|
|
if lot_id:
|
|
|
|
self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one()
|
2022-05-30 14:33:01 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._obj = None
|
2022-05-27 14:32:22 +00:00
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
2022-05-30 14:33:01 +00:00
|
|
|
if self._type and self.type.data not in ['incoming', 'outgoing']:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self._obj and self.date.data:
|
|
|
|
if self.date.data > datetime.datetime.now().date():
|
|
|
|
return False
|
2022-05-27 14:32:22 +00:00
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
2022-05-30 14:33:01 +00:00
|
|
|
self.set_obj()
|
|
|
|
db.session.add(self._obj)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|
|
|
|
|
|
|
|
def set_obj(self):
|
2022-10-10 09:10:39 +00:00
|
|
|
name = self.lot_name.data
|
2022-10-06 14:56:12 +00:00
|
|
|
self.newlot = Lot(name=name)
|
|
|
|
if self._tmp_lot:
|
|
|
|
self.newlot.devices = self._tmp_lot.devices
|
2022-05-27 14:32:22 +00:00
|
|
|
db.session.add(self.newlot)
|
|
|
|
|
|
|
|
self._obj = Transfer(lot=self.newlot)
|
2022-05-30 14:33:01 +00:00
|
|
|
|
2022-05-27 14:32:22 +00:00
|
|
|
self.populate_obj(self._obj)
|
|
|
|
|
|
|
|
if self.type.data == 'incoming':
|
|
|
|
self._obj.user_to = g.user
|
|
|
|
elif self.type.data == 'outgoing':
|
|
|
|
self._obj.user_from = g.user
|
|
|
|
|
|
|
|
|
2022-05-30 14:33:01 +00:00
|
|
|
class EditTransferForm(TransferForm):
|
|
|
|
date = DateField(
|
|
|
|
'Date',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="""Date when the transfer is closed""",
|
|
|
|
)
|
2022-05-27 14:32:22 +00:00
|
|
|
|
2022-05-30 14:33:01 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
del self.type
|
|
|
|
|
|
|
|
self._obj = self._tmp_lot.transfer
|
|
|
|
|
|
|
|
if not self.data['csrf_token']:
|
|
|
|
self.code.data = self._obj.code
|
|
|
|
self.description.data = self._obj.description
|
|
|
|
self.date.data = self._obj.date
|
2022-10-10 09:10:39 +00:00
|
|
|
self.lot_name.data = self._obj.lot.name
|
2022-05-30 14:33:01 +00:00
|
|
|
|
2022-06-06 11:37:58 +00:00
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
date = self.date.data
|
|
|
|
if date and date > datetime.datetime.now().date():
|
|
|
|
self.date.errors = ["You have to choose a date before today."]
|
|
|
|
is_valid = False
|
|
|
|
return is_valid
|
|
|
|
|
2022-05-30 14:33:01 +00:00
|
|
|
def set_obj(self, commit=True):
|
|
|
|
self.populate_obj(self._obj)
|
2022-10-10 09:10:39 +00:00
|
|
|
self._obj.lot.name = self.lot_name.data
|
2022-06-03 18:01:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotesForm(FlaskForm):
|
|
|
|
number = StringField(
|
|
|
|
'Number',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="You can put a number for tracer of receiver or delivery",
|
|
|
|
)
|
|
|
|
date = DateField(
|
|
|
|
'Date',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="""Date when the transfer was do it""",
|
|
|
|
)
|
|
|
|
units = IntegerField(
|
|
|
|
'Units',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="Number of units",
|
|
|
|
)
|
|
|
|
weight = IntegerField(
|
|
|
|
'Weight',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="Weight expressed in Kg",
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.type = kwargs.pop('type', None)
|
|
|
|
lot_id = kwargs.pop('lot_id', None)
|
|
|
|
self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one()
|
|
|
|
self._obj = None
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if self._tmp_lot.transfer:
|
|
|
|
if self.type == 'Delivery':
|
|
|
|
self._obj = self._tmp_lot.transfer.delivery_note
|
|
|
|
if not self._obj:
|
|
|
|
self._obj = DeliveryNote(transfer_id=self._tmp_lot.transfer.id)
|
|
|
|
|
|
|
|
self.date.description = """Date when the delivery was do it."""
|
|
|
|
self.number.description = (
|
|
|
|
"""You can put a number for tracer of delivery note."""
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.type == 'Receiver':
|
|
|
|
self._obj = self._tmp_lot.transfer.receiver_note
|
|
|
|
if not self._obj:
|
|
|
|
self._obj = ReceiverNote(transfer_id=self._tmp_lot.transfer.id)
|
|
|
|
|
|
|
|
self.date.description = """Date when the receipt was do it."""
|
|
|
|
self.number.description = (
|
|
|
|
"""You can put a number for tracer of receiber note."""
|
|
|
|
)
|
|
|
|
|
2022-06-06 11:37:58 +00:00
|
|
|
if self.is_editable():
|
|
|
|
self.number.render_kw.pop('disabled', None)
|
|
|
|
self.date.render_kw.pop('disabled', None)
|
|
|
|
self.units.render_kw.pop('disabled', None)
|
|
|
|
self.weight.render_kw.pop('disabled', None)
|
|
|
|
else:
|
|
|
|
disabled = {'disabled': "disabled"}
|
|
|
|
self.number.render_kw.update(disabled)
|
|
|
|
self.date.render_kw.update(disabled)
|
|
|
|
self.units.render_kw.update(disabled)
|
|
|
|
self.weight.render_kw.update(disabled)
|
|
|
|
|
2022-06-03 18:01:13 +00:00
|
|
|
if self._obj and not self.data['csrf_token']:
|
|
|
|
self.number.data = self._obj.number
|
|
|
|
self.date.data = self._obj.date
|
|
|
|
self.units.data = self._obj.units
|
|
|
|
self.weight.data = self._obj.weight
|
|
|
|
|
2022-06-06 11:37:58 +00:00
|
|
|
def is_editable(self):
|
|
|
|
if not self._tmp_lot.transfer:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self._tmp_lot.transfer.closed:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self._tmp_lot.transfer.code:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if self._tmp_lot.transfer.user_from == g.user and self.type == 'Receiver':
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self._tmp_lot.transfer.user_to == g.user and self.type == 'Delivery':
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
date = self.date.data
|
|
|
|
if date and date > datetime.datetime.now().date():
|
|
|
|
self.date.errors = ["You have to choose a date before today."]
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
if not self.is_editable():
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
2022-06-03 18:01:13 +00:00
|
|
|
def save(self, commit=True):
|
2022-06-06 11:37:58 +00:00
|
|
|
if self._tmp_lot.transfer.closed:
|
|
|
|
return self._obj
|
|
|
|
|
2022-06-03 18:01:13 +00:00
|
|
|
self.populate_obj(self._obj)
|
|
|
|
db.session.add(self._obj)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|
2022-07-04 09:26:24 +00:00
|
|
|
|
|
|
|
|
2023-02-14 19:03:33 +00:00
|
|
|
class CustomerDetailsForm(FlaskForm):
|
|
|
|
company_name = StringField(
|
|
|
|
'Company name',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="Name of the company",
|
|
|
|
)
|
|
|
|
location = StringField(
|
|
|
|
'Location',
|
|
|
|
[validators.Optional()],
|
|
|
|
render_kw={'class': "form-control"},
|
|
|
|
description="""Location where is the company""",
|
|
|
|
)
|
|
|
|
logo = URLField(
|
|
|
|
'Logo',
|
|
|
|
[validators.Optional()],
|
2023-03-09 09:43:35 +00:00
|
|
|
render_kw={
|
|
|
|
'class': "form-control",
|
|
|
|
"placeholder": "Url where is the logo - acceptd only .png, .jpg, .gif, svg",
|
|
|
|
},
|
2023-02-14 19:03:33 +00:00
|
|
|
description="Url where is the logo",
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
lot_id = kwargs.pop('lot_id', None)
|
|
|
|
self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one()
|
|
|
|
self._obj = self._tmp_lot.transfer.customer_details
|
|
|
|
if self._obj:
|
|
|
|
kwargs['obj'] = self._obj
|
|
|
|
if not self._obj:
|
|
|
|
self._obj = TransferCustomerDetails(transfer_id=self._tmp_lot.transfer.id)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if isinstance(self.logo.data, URL):
|
|
|
|
self.logo.data = URL(self.logo.data).to_text()
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
2023-03-09 17:07:16 +00:00
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return is_valid
|
|
|
|
|
2023-03-13 16:04:57 +00:00
|
|
|
if not self.logo.data:
|
|
|
|
return True
|
|
|
|
|
2023-03-09 17:07:16 +00:00
|
|
|
extensions = ["jpg", "jpeg", "png", "gif", "svg"]
|
|
|
|
if self.logo.data.lower().split(".")[-1] not in extensions:
|
|
|
|
txt = "Error in Url field - accepted only .PNG, .JPG and .GIF. extensions"
|
|
|
|
self.logo.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2023-02-14 19:03:33 +00:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
self.populate_obj(self._obj)
|
|
|
|
self._obj.logo = URL(self._obj.logo)
|
|
|
|
db.session.add(self._obj)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self._obj
|
|
|
|
|
|
|
|
|
2022-07-04 09:26:24 +00:00
|
|
|
class UploadPlaceholderForm(FlaskForm):
|
|
|
|
type = StringField('Type', [validators.DataRequired()])
|
|
|
|
placeholder_file = FileField(
|
|
|
|
'Select a Placeholder File', [validators.DataRequired()]
|
|
|
|
)
|
|
|
|
|
2022-09-12 15:38:29 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.dev_new = 0
|
|
|
|
self.dev_update = 0
|
|
|
|
|
2022-07-05 16:09:47 +00:00
|
|
|
def get_data_file(self):
|
|
|
|
files = request.files.getlist(self.placeholder_file.name)
|
|
|
|
|
|
|
|
if not files:
|
|
|
|
return False
|
|
|
|
|
|
|
|
_file = files[0]
|
|
|
|
if _file.content_type == 'text/csv':
|
2022-07-07 11:10:05 +00:00
|
|
|
self.source = "CSV File: {}".format(_file.filename)
|
2022-09-12 08:55:49 +00:00
|
|
|
data = pd.read_csv(
|
|
|
|
_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL
|
|
|
|
)
|
|
|
|
data = data.fillna('').to_dict()
|
|
|
|
return data
|
2022-07-05 16:09:47 +00:00
|
|
|
else:
|
2022-09-14 13:43:34 +00:00
|
|
|
if _file.content_type == 'application/vnd.oasis.opendocument.spreadsheet':
|
|
|
|
self.source = "Ods File: {}".format(_file.filename)
|
|
|
|
else:
|
|
|
|
self.source = "Excel File: {}".format(_file.filename)
|
2022-07-05 16:36:27 +00:00
|
|
|
try:
|
2022-10-05 07:49:15 +00:00
|
|
|
data = pd.read_excel(_file).fillna('').to_dict()
|
2022-07-05 16:36:27 +00:00
|
|
|
except ValueError:
|
2022-09-12 08:55:49 +00:00
|
|
|
txt = ["File don't have a correct format"]
|
|
|
|
self.placeholder_file.errors = txt
|
2022-07-05 16:36:27 +00:00
|
|
|
return False
|
2022-07-05 16:09:47 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-07-04 09:26:24 +00:00
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
2022-07-05 16:09:47 +00:00
|
|
|
if not request.files.getlist(self.placeholder_file.name):
|
2022-07-04 09:26:24 +00:00
|
|
|
return False
|
|
|
|
|
2022-07-05 16:09:47 +00:00
|
|
|
data = self.get_data_file()
|
2022-07-05 16:36:27 +00:00
|
|
|
if not data:
|
|
|
|
return False
|
2022-07-05 16:09:47 +00:00
|
|
|
|
2022-07-04 09:26:24 +00:00
|
|
|
header = [
|
|
|
|
'Model',
|
|
|
|
'Manufacturer',
|
|
|
|
'Serial Number',
|
2022-09-13 16:15:50 +00:00
|
|
|
'Part Number',
|
2022-07-04 09:26:24 +00:00
|
|
|
'Id device Supplier',
|
2022-10-05 07:49:15 +00:00
|
|
|
'Id device Internal',
|
2022-07-04 09:26:24 +00:00
|
|
|
'Pallet',
|
|
|
|
'Info',
|
|
|
|
]
|
|
|
|
|
|
|
|
for k in header:
|
|
|
|
if k not in data.keys():
|
2022-09-12 08:55:49 +00:00
|
|
|
txt = ["Missing required fields in the file"]
|
|
|
|
self.placeholder_file.errors = txt
|
2022-07-04 09:26:24 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
self.placeholders = []
|
|
|
|
schema = SnapshotSchema()
|
|
|
|
self.path_snapshots = {}
|
2022-10-05 07:49:15 +00:00
|
|
|
for i in data['Model'].keys():
|
2022-07-04 09:26:24 +00:00
|
|
|
# create a new one
|
|
|
|
json_snapshot = {
|
|
|
|
'type': 'Snapshot',
|
|
|
|
'software': 'Web',
|
|
|
|
'version': '11.0',
|
|
|
|
'device': {
|
|
|
|
'type': self.type.data,
|
|
|
|
'model': "{}".format(data['Model'][i]),
|
|
|
|
'manufacturer': "{}".format(data['Manufacturer'][i]),
|
|
|
|
'serialNumber': "{}".format(data['Serial Number'][i]),
|
2022-09-13 16:15:50 +00:00
|
|
|
'partNumber': "{}".format(data['Part Number'][i]),
|
2022-07-04 09:26:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
json_placeholder = {
|
|
|
|
'id_device_supplier': data['Id device Supplier'][i],
|
2022-10-05 07:49:15 +00:00
|
|
|
'id_device_internal': data['Id device Internal'][i],
|
2022-07-04 09:26:24 +00:00
|
|
|
'pallet': data['Pallet'][i],
|
|
|
|
'info': data['Info'][i],
|
2022-07-28 15:48:14 +00:00
|
|
|
'is_abstract': False,
|
2022-07-04 09:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshot_json = schema.load(json_snapshot)
|
|
|
|
device = snapshot_json['device']
|
|
|
|
device.placeholder = Placeholder(**json_placeholder)
|
2022-09-12 15:38:29 +00:00
|
|
|
self.dev_new += 1
|
2022-07-04 09:26:24 +00:00
|
|
|
|
2022-09-12 15:38:29 +00:00
|
|
|
typ = 'New device'
|
2022-07-07 11:10:05 +00:00
|
|
|
placeholder_log = PlaceholdersLog(
|
2022-09-12 15:38:29 +00:00
|
|
|
type=typ, source=self.source, placeholder=device.placeholder
|
2022-07-07 11:10:05 +00:00
|
|
|
)
|
|
|
|
self.placeholders.append((device, placeholder_log))
|
2022-07-04 09:26:24 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
|
2022-07-07 11:10:05 +00:00
|
|
|
for device, placeholder_log in self.placeholders:
|
2022-07-04 09:26:24 +00:00
|
|
|
db.session.add(device)
|
2022-07-07 11:10:05 +00:00
|
|
|
db.session.add(placeholder_log)
|
2022-07-04 09:26:24 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self.placeholders
|
2022-07-05 16:09:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EditPlaceholderForm(FlaskForm):
|
|
|
|
manufacturer = StringField('Manufacturer', [validators.Optional()])
|
|
|
|
model = StringField('Model', [validators.Optional()])
|
|
|
|
serial_number = StringField('Serial Number', [validators.Optional()])
|
2022-09-13 16:15:50 +00:00
|
|
|
part_number = StringField('Part Number', [validators.Optional()])
|
2022-07-05 16:09:47 +00:00
|
|
|
id_device_supplier = StringField('Id Supplier', [validators.Optional()])
|
|
|
|
pallet = StringField('Pallet', [validators.Optional()])
|
|
|
|
info = StringField('Info', [validators.Optional()])
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
|
|
|
|
for device in self.placeholders:
|
|
|
|
db.session.add(device)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self.placeholders
|
2022-07-28 15:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BindingForm(FlaskForm):
|
|
|
|
phid = StringField('Phid', [validators.DataRequired()])
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.device = kwargs.pop('device', None)
|
|
|
|
self.placeholder = kwargs.pop('placeholder', None)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
txt = "This placeholder not exist."
|
|
|
|
self.phid.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
2022-09-15 09:51:27 +00:00
|
|
|
if self.device.is_abstract() not in ['Snapshot', 'Placeholder']:
|
|
|
|
txt = "This is not a Snapshot or Placeholder device."
|
2022-07-28 15:48:14 +00:00
|
|
|
self.phid.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self.placeholder:
|
|
|
|
self.placeholder = Placeholder.query.filter(
|
2022-08-08 16:09:25 +00:00
|
|
|
Placeholder.phid == self.phid.data.strip(), Placeholder.owner == g.user
|
2022-07-28 15:48:14 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
if not self.placeholder:
|
|
|
|
txt = "This placeholder not exist."
|
|
|
|
self.phid.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
2022-09-15 09:51:27 +00:00
|
|
|
if self.placeholder.status not in ['Snapshot', 'Placeholder']:
|
2022-08-02 15:44:36 +00:00
|
|
|
txt = "This placeholder have a binding with other device. "
|
|
|
|
txt += "Before you need to do an unbinding with this other device."
|
2022-07-28 15:48:14 +00:00
|
|
|
self.phid.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2022-12-22 11:01:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserTrustsForm(FlaskForm):
|
|
|
|
snapshot_type = SelectField(
|
|
|
|
'',
|
|
|
|
[validators.DataRequired()],
|
|
|
|
choices=[("new_device", "New Device"), ("update", "Update")],
|
|
|
|
default="new_device",
|
|
|
|
render_kw={'class': "form-select"},
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, snapshot_uuid, *args, **kwargs):
|
2022-12-23 17:33:49 +00:00
|
|
|
self.snapshot = Snapshot.query.filter_by(uuid=snapshot_uuid).one()
|
|
|
|
self.device = None
|
|
|
|
if self.snapshot.device:
|
|
|
|
self.device = self.snapshot.device
|
|
|
|
|
2022-12-22 11:01:31 +00:00
|
|
|
self.snapshot_type.kwargs['default'] = self.snapshot.get_new_device()
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def validate(self, extra_validators=None):
|
|
|
|
is_valid = super().validate(extra_validators)
|
|
|
|
|
|
|
|
if not is_valid:
|
|
|
|
txt = ""
|
|
|
|
self.snapthot_type.errors = [txt]
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def unic(self):
|
|
|
|
try:
|
|
|
|
return self._unic
|
|
|
|
except Exception:
|
2023-01-16 14:26:58 +00:00
|
|
|
self._devices = (
|
2022-12-22 11:01:31 +00:00
|
|
|
Device.query.filter_by(
|
2022-12-23 17:33:49 +00:00
|
|
|
hid=self.device.hid, owner=g.user, placeholder=None, active=True
|
2023-01-16 14:26:58 +00:00
|
|
|
)
|
|
|
|
.order_by(Device.updated.asc())
|
|
|
|
.all()
|
2022-12-22 11:01:31 +00:00
|
|
|
)
|
|
|
|
|
2023-01-16 14:26:58 +00:00
|
|
|
self._unic = len(self._devices) < 2
|
2022-12-22 11:01:31 +00:00
|
|
|
return self._unic
|
|
|
|
|
2023-01-18 11:38:08 +00:00
|
|
|
def dhids_all_devices(self):
|
|
|
|
self.unic()
|
|
|
|
return ", ".join([x.dhid for x in self._devices][1:])
|
|
|
|
|
|
|
|
def dhid_base(self):
|
|
|
|
self.unic()
|
|
|
|
if not self._devices:
|
|
|
|
return ''
|
|
|
|
return self._devices[0].dhid
|
|
|
|
|
2022-12-22 11:01:31 +00:00
|
|
|
def show(self):
|
|
|
|
if not self.snapshot or not self.device:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not hasattr(self.device, 'system_uuid'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self.device.system_uuid:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.snapshot.get_new_device() == 'update':
|
|
|
|
# To do Split
|
|
|
|
return True
|
|
|
|
|
|
|
|
if not self.unic():
|
2023-01-16 14:26:58 +00:00
|
|
|
if self.device == self._devices[0]:
|
|
|
|
return False
|
2022-12-22 11:01:31 +00:00
|
|
|
# To do merge
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
if not self.show():
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.snapshot_type.data == self.snapshot.get_new_device():
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.snapshot_type.data == 'update' and not self.unic():
|
2022-12-23 17:33:49 +00:00
|
|
|
self.device.reliable()
|
2022-12-22 11:01:31 +00:00
|
|
|
|
|
|
|
if self.snapshot_type.data == 'new_device' and self.unic():
|
2022-12-23 17:33:49 +00:00
|
|
|
self.device.unreliable()
|
2023-01-10 09:49:32 +00:00
|
|
|
txt = "This devices is assigned as unreliable for the user "
|
|
|
|
txt += "and never is possible to do an update of this device."
|
|
|
|
self.error_log(txt)
|
2022-12-22 11:01:31 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return self.snapshot
|
2023-01-10 09:49:32 +00:00
|
|
|
|
|
|
|
def error_log(self, txt):
|
|
|
|
snapshot = self.get_first_snapshot()
|
|
|
|
error = SnapshotsLog(
|
|
|
|
description=txt,
|
|
|
|
snapshot=snapshot,
|
|
|
|
snapshot_uuid=snapshot.uuid,
|
|
|
|
severity=Severity.Error,
|
|
|
|
sid=snapshot.sid,
|
|
|
|
version="{}".format(snapshot.version),
|
|
|
|
)
|
|
|
|
db.session.add(error)
|
|
|
|
|
|
|
|
def get_first_snapshot(self):
|
|
|
|
device = self.snapshot.device
|
|
|
|
for ac in device.actions:
|
|
|
|
if ac.type == 'Snapshot':
|
|
|
|
return ac
|