From f35dab89838623d7ec0897c786de6c686e017c08 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 19 Jan 2022 13:40:40 +0100 Subject: [PATCH] render the correct form in a new page --- ereuse_devicehub/inventory/forms.py | 47 +++- ereuse_devicehub/inventory/views.py | 21 +- ereuse_devicehub/static/js/create_device.js | 26 ++ .../templates/inventory/create_device.html | 236 ++++++++++++++++++ .../templates/inventory/device_list.html | 2 +- 5 files changed, 321 insertions(+), 11 deletions(-) create mode 100644 ereuse_devicehub/static/js/create_device.js create mode 100644 ereuse_devicehub/templates/inventory/create_device.html diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 344081a5..f1064ea1 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -7,7 +7,9 @@ from sqlalchemy.util import OrderedSet from psycopg2.errors import UniqueViolation from ereuse_devicehub.db import db -from ereuse_devicehub.resources.device.models import Device, Computer +from ereuse_devicehub.resources.device.models import Device, Computer, Smartphone, Cellphone, \ + Tablet, Monitor, Mouse, Keyboard, \ + MemoryCardReader, SAI from ereuse_devicehub.resources.action.models import RateComputer, Snapshot from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema from ereuse_devicehub.resources.lot.models import Lot @@ -200,17 +202,48 @@ class UploadSnapshotForm(FlaskForm): return snapshot -class DeviceForm(FlaskForm): - type = StringField(u'Name', [validators.length(min=1)]) +class NewDeviceForm(FlaskForm): + type = StringField(u'Type', [validators.DataRequired()]) + label = StringField(u'Label') + serial_number = StringField(u'Seria Number', [validators.DataRequired()]) + model = StringField(u'Model', [validators.DataRequired()]) + manufacturer = StringField(u'Manufacturer', [validators.DataRequired()]) + appearance = StringField(u'Appearance') + functionality = StringField(u'Functionality') + brand = StringField(u'Brand') + generation = StringField(u'Generation') + version = StringField(u'Version') + weight = StringField(u'Weight') + width = StringField(u'Width') + height = StringField(u'Height') + depth = StringField(u'Depth') + variant = StringField(u'Variant') + sku = StringField(u'SKU') + image = StringField(u'Image') + imei = StringField(u'IMEI') + meid = StringField(u'MEID') + resolution = StringField(u'Resolution width') + screen = StringField(u'Screen size') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.devices = {"Smartphone": Smartphone, + "Tablet": Tablet, + "Cellphone": Cellphone, + "Monitor": Monitor, + "Mouse": Mouse, + "Keyboard": Keyboard, + "SAI": SAI, + "MemoryCardReader": MemoryCardReader, + } + if self.type.data: + self.instance = self.devices[self.type.data]() + self.populate_obj(self.instance) def save(self): - pass - - def remove(self): - pass + db.session.add(self.instance) + db.session.commit() + return self.instance class NewActionForm(FlaskForm): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 4d7f0d57..6ecbe6a5 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -5,9 +5,7 @@ from flask_login import login_required, current_user from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.device.models import Device -from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm, UploadSnapshotForm -from ereuse_devicehub.resources.action import SnapshotDef -from ereuse_devicehub.resources.action.views.snapshot import SnapshotView as TealSnapshotView +from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm, UploadSnapshotForm, NewDeviceForm devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') @@ -139,6 +137,22 @@ class UploadSnapshotView(View): return flask.render_template(self.template_name, form=form, lots=lots) +class CreateDeviceView(View): + methods = ['GET', 'POST'] + decorators = [login_required] + template_name = 'inventory/create_device.html' + + def dispatch_request(self): + lots = Lot.query.filter(Lot.owner_id == current_user.id).all() + form = NewDeviceForm() + if form.validate_on_submit(): + form.save() + next_url = url_for('inventory.devices.devicelist') + return flask.redirect(next_url) + + return flask.render_template(self.template_name, form=form, lots=lots) + + devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule('/device//', view_func=DeviceDetailsView.as_view('device_details')) devices.add_url_rule('/lot//device/', view_func=DeviceListView.as_view('lotdevicelist')) @@ -148,3 +162,4 @@ devices.add_url_rule('/lot/add/', view_func=LotCreateView.as_view('lot_add')) devices.add_url_rule('/lot//del/', view_func=LotDeleteView.as_view('lot_del')) devices.add_url_rule('/lot//', view_func=LotUpdateView.as_view('lot_edit')) devices.add_url_rule('/upload-snapshot/', view_func=UploadSnapshotView.as_view('upload_snapshot')) +devices.add_url_rule('/device/add/', view_func=CreateDeviceView.as_view('device_add')) diff --git a/ereuse_devicehub/static/js/create_device.js b/ereuse_devicehub/static/js/create_device.js new file mode 100644 index 00000000..d34be039 --- /dev/null +++ b/ereuse_devicehub/static/js/create_device.js @@ -0,0 +1,26 @@ +$(document).ready(function() { + $("#type").on("change", deviceInputs); + $("#screen").hide(); + $("#resolution").hide(); + $("#imei").hide(); + $("#meid").hide(); +}) + +function deviceInputs() { + if ($("#type").val() == 'Monitor') { + $("#screen").show(); + $("#resolution").show(); + $("#imei").hide(); + $("#meid").hide(); + } else if (['Smartphone', 'Cellphone', 'Tablet'].includes($("#type").val())) { + $("#screen").hide(); + $("#resolution").hide(); + $("#imei").show(); + $("#meid").show(); + } else { + $("#screen").hide(); + $("#resolution").hide(); + $("#imei").hide(); + $("#meid").hide(); + } +} diff --git a/ereuse_devicehub/templates/inventory/create_device.html b/ereuse_devicehub/templates/inventory/create_device.html new file mode 100644 index 00000000..8dbd300f --- /dev/null +++ b/ereuse_devicehub/templates/inventory/create_device.html @@ -0,0 +1,236 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
+

{{ title }}

+ +
+ +
+
+
+ +
+
+ +
+
New Device
+ {% if form.form_errors %} +

+ {% for error in form.form_errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %} +
+ +
+ {{ form.csrf_token }} + +
+
+ + + Type of devices +
+ +
+ + {{ form.label(class_="form-control") }} + Label that you want link to this device +
+ +
+ + {{ form.serial_number(class_="form-control") }} + Serial number of this device +
+ +
+ + {{ form.model(class_="form-control") }} + Name of model +
+ +
+ + {{ form.manufacturer(class_="form-control") }} + Name of manufacturer +
+ +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ Rate the imperfections that affect the device aesthetically, but not its use. +
+ +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ It qualifies the defects of a device that affect its use. +
+ +
+ + {{ 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. +
+ +
+ + {{ form.generation(class_="form-control") }} + The generation of the device. +
+ +
+ + {{ form.version(class_="form-control") }} + The version code o fthis device, like 'v1' or 'A001'. +
+ +
+ + {{ form.weight(class_="form-control") }} + The weight of the device in Kg. +
+ +
+ + {{ form.width(class_="form-control") }} + The width of the device in meters. +
+ +
+ + {{ form.height(class_="form-control") }} + The height of the device in meters. +
+ +
+ + {{ form.depth(class_="form-control") }} + The depth of the device in meters. +
+ +
+ + {{ form.variant(class_="form-control") }} + A variant or sub-model of the device. +
+ +
+ + {{ form.sku(class_="form-control") }} + The Stock Keeping Unit (SKU), i.e. a merchant-specific identifier for a product or service. +
+ +
+ + {{ form.image(class_="form-control") }} + An URL containing an image of the device. +
+ +
+ + {{ form.imei(class_="form-control") }} + A number from 14 to 16 digits. +
+ +
+ + {{ form.meid(class_="form-control") }} + 14 hexadecimal digits. +
+ +
+ + {{ form.resolution(class_="form-control") }} + The resolution width of the screen. +
+ +
+ + {{ form.screen(class_="form-control") }} + The size of the screen. +
+ +
+ +
+ Cancel + +
+
+ +
+ +
+ +
+ +
+
+
+
+ + +{% endblock main %} diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 1ca5fae9..837d8993 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -212,7 +212,7 @@
  • - + Create a new Device