save a new device and validations

This commit is contained in:
Cayo Puigdefabregas 2022-01-20 13:13:20 +01:00
parent 6e0f627aca
commit f497cc03e5
2 changed files with 202 additions and 25 deletions

View File

@ -1,10 +1,8 @@
import json
import teal
from flask_wtf import FlaskForm
from wtforms import StringField, validators, MultipleFileField
from flask import g, request, app
from wtforms import StringField, validators, MultipleFileField, FloatField, IntegerField
from flask import g, request
from sqlalchemy.util import OrderedSet
from psycopg2.errors import UniqueViolation
from json.decoder import JSONDecodeError
from ereuse_devicehub.db import db
@ -142,11 +140,7 @@ class UploadSnapshotForm(FlaskForm):
path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
snapshot_json.pop('debug', None)
snapshot_json = schema.load(snapshot_json)
try:
response = self.build(snapshot_json)
except teal.db.DBError as error:
self.result[filename] = 'Error: {0}'.format(error.code)
break
if hasattr(response, 'type'):
self.result[filename] = 'Ok'
@ -225,15 +219,15 @@ class NewDeviceForm(FlaskForm):
appearance = StringField(u'Appearance')
functionality = StringField(u'Functionality')
brand = StringField(u'Brand')
generation = StringField(u'Generation')
generation = IntegerField(u'Generation')
version = StringField(u'Version')
weight = StringField(u'Weight')
width = StringField(u'Width')
height = StringField(u'Height')
depth = StringField(u'Depth')
weight = FloatField(u'Weight', [validators.DataRequired()])
width = FloatField(u'Width', [validators.DataRequired()])
height = FloatField(u'Height', [validators.DataRequired()])
depth = FloatField(u'Depth', [validators.DataRequired()])
variant = StringField(u'Variant')
sku = StringField(u'SKU')
image = StringField(u'Image')
image = StringField(u'Image', [validators.Optional(), validators.URL()])
imei = StringField(u'IMEI')
meid = StringField(u'MEID')
resolution = StringField(u'Resolution width')
@ -248,14 +242,53 @@ class NewDeviceForm(FlaskForm):
"Mouse": Mouse,
"Keyboard": Keyboard,
"SAI": SAI,
"MemoryCardReader": MemoryCardReader,
}
"MemoryCardReader": MemoryCardReader}
if not self.generation.data:
self.generation.data = 0
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
if self.type.data:
self.instance = self.devices[self.type.data]()
self.populate_obj(self.instance)
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if not is_valid:
return False
if self.generation.data < 0:
return False
if self.weight.data < 0.1:
return False
if self.height.data < 0.1:
return False
if self.width.data < 0.1:
return False
if self.depth.data < 0.1:
return False
return True
def save(self):
db.session.add(self.instance)
import pdb; pdb.set_trace()
db.session.commit()
return self.instance
@ -265,6 +298,3 @@ class NewActionForm(FlaskForm):
date = StringField(u'Date')
severity = StringField(u'Severity')
description = StringField(u'Description')
def save(self):
pass

View File

@ -53,30 +53,65 @@
</optgroup>
</select>
<small class="text-muted form-text">Type of devices</small>
{% if form.type.errors %}
<p class="text-danger">
{% for error in form.type.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="form-group mb-2">
<label for="label" class="form-label">Label</label>
{{ form.label(class_="form-control") }}
<small class="text-muted form-text">Label that you want link to this device</small>
{% if form.label.errors %}
<p class="text-danger">
{% for error in form.label.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="serialNumber" class="form-label">{{ form.serial_number.label }} *</label>
{{ form.serial_number(class_="form-control") }}
<small class="text-muted form-text">Serial number of this device</small>
{% if form.serial_number.errors %}
<p class="text-danger">
{% for error in form.serial_number.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.model.label }} *</label>
{{ form.model(class_="form-control") }}
<small class="text-muted form-text">Name of model</small>
{% if form.model.errors %}
<p class="text-danger">
{% for error in form.model.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.manufacturer.label }} *</label>
{{ form.manufacturer(class_="form-control") }}
<small class="text-muted form-text">Name of manufacturer</small>
{% if form.manufacturer.errors %}
<p class="text-danger">
{% for error in form.manufacturer.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
@ -106,6 +141,13 @@
<label class="form-check-label">E. It is unacceptable (substantial visual damage that may affect use)</label>
</div>
<small class="text-muted form-text">Rate the imperfections that affect the device aesthetically, but not its use.</small>
{% if form.appearance.errors %}
<p class="text-danger">
{% for error in form.appearance.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
@ -127,90 +169,195 @@
<label class="form-check-label">D. Multiple buttons do not work properly; the screen has severe damage that may affect use</label>
</div>
<small class="text-muted form-text">It qualifies the defects of a device that affect its use.</small>
{% if form.functionality.errors %}
<p class="text-danger">
{% for error in form.functionality.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.brand.label }}</label>
{{ form.brand(class_="form-control") }}
<small class="text-muted form-text">A naming for consumers. This field can represent several models, so it can be ambiguous, and it is not used to identify the product.</small>
{% if form.brand.errors %}
<p class="text-danger">
{% for error in form.brand.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.generation.label }}</label>
<label for="model" class="form-label">{{ form.generation.label }} *</label>
{{ form.generation(class_="form-control") }}
<small class="text-muted form-text">The generation of the device.</small>
{% if form.generation.errors %}
<p class="text-danger">
{% for error in form.generation.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.version.label }}</label>
{{ form.version(class_="form-control") }}
<small class="text-muted form-text">The version code o fthis device, like 'v1' or 'A001'.</small>
{% if form.version.errors %}
<p class="text-danger">
{% for error in form.version.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.weight.label }}</label>
<label for="model" class="form-label">{{ form.weight.label }} *</label>
{{ form.weight(class_="form-control") }}
<small class="text-muted form-text">The weight of the device in Kg.</small>
{% if form.weight.errors %}
<p class="text-danger">
{% for error in form.weight.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.width.label }}</label>
<label for="model" class="form-label">{{ form.width.label }} *</label>
{{ form.width(class_="form-control") }}
<small class="text-muted form-text">The width of the device in meters.</small>
{% if form.width.errors %}
<p class="text-danger">
{% for error in form.width.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.height.label }}</label>
<label for="model" class="form-label">{{ form.height.label }} *</label>
{{ form.height(class_="form-control") }}
<small class="text-muted form-text">The height of the device in meters.</small>
{% if form.height.errors %}
<p class="text-danger">
{% for error in form.height.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.depth.label }}</label>
<label for="model" class="form-label">{{ form.depth.label }} *</label>
{{ form.depth(class_="form-control") }}
<small class="text-muted form-text">The depth of the device in meters.</small>
{% if form.depth.errors %}
<p class="text-danger">
{% for error in form.depth.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.variant.label }}</label>
{{ form.variant(class_="form-control") }}
<small class="text-muted form-text">A variant or sub-model of the device.</small>
{% if form.variant.errors %}
<p class="text-danger">
{% for error in form.variant.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="model" class="form-label">{{ form.sku.label }}</label>
{{ form.sku(class_="form-control") }}
<small class="text-muted form-text">The Stock Keeping Unit (SKU), i.e. a merchant-specific identifier for a product or service.</small>
{% if form.sku.errors %}
<p class="text-danger">
{% for error in form.sku.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="from-group has-validation mb-2">
<label for="image" class="form-label">{{ form.image.label }}</label>
{{ form.image(class_="form-control") }}
<small class="text-muted form-text">An URL containing an image of the device.</small>
{% if form.image.errors %}
<p class="text-danger">
{% for error in form.image.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div id="imei" class="from-group has-validation mb-2">
<label for="imei" class="form-label">{{ form.imei.label }}</label>
{{ form.imei(class_="form-control") }}
<small class="text-muted form-text">A number from 14 to 16 digits.</small>
{% if form.imei.errors %}
<p class="text-danger">
{% for error in form.imei.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div id="meid" class="from-group has-validation mb-2">
<label for="meid" class="form-label">{{ form.meid.label }}</label>
{{ form.meid(class_="form-control") }}
<small class="text-muted form-text">14 hexadecimal digits.</small>
{% if form.meid.errors %}
<p class="text-danger">
{% for error in form.meid.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div id="resolution" class="from-group has-validation mb-2">
<label for="resolution" class="form-label">{{ form.resolution.label }}</label>
{{ form.resolution(class_="form-control") }}
<small class="text-muted form-text">The resolution width of the screen.</small>
{% if form.resolution.errors %}
<p class="text-danger">
{% for error in form.resolution.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div id="screen" class="from-group has-validation mb-2">
<label for="screen" class="form-label">{{ form.screen.label }}</label>
{{ form.screen(class_="form-control") }}
<small class="text-muted form-text">The size of the screen.</small>
{% if form.screen.errors %}
<p class="text-danger">
{% for error in form.screen.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
</div>