diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 48a42928..5c233e66 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -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 + response = self.build(snapshot_json) 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 diff --git a/ereuse_devicehub/templates/inventory/create_device.html b/ereuse_devicehub/templates/inventory/create_device.html index 8dbd300f..9f21c68a 100644 --- a/ereuse_devicehub/templates/inventory/create_device.html +++ b/ereuse_devicehub/templates/inventory/create_device.html @@ -53,30 +53,65 @@ Type of devices + {% if form.type.errors %} +

+ {% for error in form.type.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.label(class_="form-control") }} Label that you want link to this device + {% if form.label.errors %} +

+ {% for error in form.label.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.serial_number(class_="form-control") }} Serial number of this device + {% if form.serial_number.errors %} +

+ {% for error in form.serial_number.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.model(class_="form-control") }} Name of model + {% if form.model.errors %} +

+ {% for error in form.model.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.manufacturer(class_="form-control") }} Name of manufacturer + {% if form.manufacturer.errors %} +

+ {% for error in form.manufacturer.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
@@ -106,6 +141,13 @@
Rate the imperfections that affect the device aesthetically, but not its use. + {% if form.appearance.errors %} +

+ {% for error in form.appearance.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
@@ -127,90 +169,195 @@
It qualifies the defects of a device that affect its use. + {% if form.functionality.errors %} +

+ {% for error in form.functionality.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.brand(class_="form-control") }} A naming for consumers. This field can represent several models, so it can be ambiguous, and it is not used to identify the product. + {% if form.brand.errors %} +

+ {% for error in form.brand.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
- + {{ form.generation(class_="form-control") }} The generation of the device. + {% if form.generation.errors %} +

+ {% for error in form.generation.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.version(class_="form-control") }} The version code o fthis device, like 'v1' or 'A001'. + {% if form.version.errors %} +

+ {% for error in form.version.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
- + {{ form.weight(class_="form-control") }} The weight of the device in Kg. + {% if form.weight.errors %} +

+ {% for error in form.weight.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
- + {{ form.width(class_="form-control") }} The width of the device in meters. + {% if form.width.errors %} +

+ {% for error in form.width.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
- + {{ form.height(class_="form-control") }} The height of the device in meters. + {% if form.height.errors %} +

+ {% for error in form.height.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
- + {{ form.depth(class_="form-control") }} The depth of the device in meters. + {% if form.depth.errors %} +

+ {% for error in form.depth.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.variant(class_="form-control") }} A variant or sub-model of the device. + {% if form.variant.errors %} +

+ {% for error in form.variant.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.sku(class_="form-control") }} The Stock Keeping Unit (SKU), i.e. a merchant-specific identifier for a product or service. + {% if form.sku.errors %} +

+ {% for error in form.sku.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.image(class_="form-control") }} An URL containing an image of the device. + {% if form.image.errors %} +

+ {% for error in form.image.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.imei(class_="form-control") }} A number from 14 to 16 digits. + {% if form.imei.errors %} +

+ {% for error in form.imei.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.meid(class_="form-control") }} 14 hexadecimal digits. + {% if form.meid.errors %} +

+ {% for error in form.meid.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.resolution(class_="form-control") }} The resolution width of the screen. + {% if form.resolution.errors %} +

+ {% for error in form.resolution.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}
{{ form.screen(class_="form-control") }} The size of the screen. + {% if form.screen.errors %} +

+ {% for error in form.screen.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %}