diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 198d762c..9716ebaf 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1,24 +1,31 @@ import json -from flask_wtf import FlaskForm -from wtforms import StringField, validators, MultipleFileField, FloatField, IntegerField, \ - HiddenField, DateField, TextAreaField, SelectField -from flask import g, request -from sqlalchemy.util import OrderedSet from json.decoder import JSONDecodeError +from flask import g, request +from flask_wtf import FlaskForm +from sqlalchemy.util import OrderedSet +from wtforms import (DateField, FloatField, HiddenField, IntegerField, + MultipleFileField, SelectField, StringField, + TextAreaField, validators) + from ereuse_devicehub.db import db -from ereuse_devicehub.resources.device.models import Device, Computer, Smartphone, Cellphone, \ - Tablet, Monitor, Mouse, Keyboard, \ - MemoryCardReader, SAI -from ereuse_devicehub.resources.action.models import Action, RateComputer, Snapshot, VisualTest -from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema +from ereuse_devicehub.resources.action.models import (Action, RateComputer, + Snapshot, VisualTest) +from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate +from ereuse_devicehub.resources.action.schemas import \ + Snapshot as SnapshotSchema +from ereuse_devicehub.resources.action.views.snapshot import (move_json, + save_json) +from ereuse_devicehub.resources.device.models import (SAI, Cellphone, Computer, + Device, Keyboard, + MemoryCardReader, + Monitor, Mouse, + Smartphone, Tablet) +from ereuse_devicehub.resources.device.sync import Sync +from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.tag.model import Tag -from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.user.exceptions import InsufficientPermission -from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate -from ereuse_devicehub.resources.device.sync import Sync -from ereuse_devicehub.resources.action.views.snapshot import save_json, move_json class LotDeviceForm(FlaskForm): @@ -470,6 +477,7 @@ class NewActionForm(FlaskForm): date = DateField(u'Date', validators=(validators.Optional(),)) severity = SelectField(u'Severity', choices=[(v.name, v.name) for v in Severity]) description = TextAreaField(u'Description') + lot = HiddenField() type = HiddenField() def validate(self, extra_validators=None): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 29dcc78f..bd52eb77 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1,14 +1,18 @@ -import flask import datetime -from flask.views import View -from flask import Blueprint, url_for, request -from flask_login import login_required, current_user +import flask +from flask import Blueprint, request, url_for +from flask.views import View +from flask_login import current_user, login_required + +from ereuse_devicehub.inventory.forms import (AllocateForm, LotDeviceForm, + LotForm, NewActionForm, + NewDeviceForm, TagDeviceForm, + TagForm, TagUnnamedForm, + UploadSnapshotForm) +from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.tag.model import Tag -from ereuse_devicehub.resources.device.models import Device -from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm, UploadSnapshotForm, \ - NewDeviceForm, TagForm, TagUnnamedForm, TagDeviceForm, NewActionForm, AllocateForm # TODO(@slamora): rename base 'inventory.devices' --> 'inventory' devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') @@ -41,6 +45,11 @@ class DeviceListMix(View): form_new_action = NewActionForm() form_new_allocate = AllocateForm() + action_devices = form_new_action.devices.data + list_devices = [] + if action_devices: + list_devices.extend([int(x) for x in action_devices.split(",")]) + self.context = { 'devices': devices, 'lots': lots, @@ -49,7 +58,8 @@ class DeviceListMix(View): 'form_new_action': form_new_action, 'form_new_allocate': form_new_allocate, 'lot': lot, - 'tags': tags + 'tags': tags, + 'list_devices': list_devices } return self.context @@ -285,9 +295,14 @@ class NewActionView(View): def dispatch_request(self): self.form = self._form() + + next_url = url_for('inventory.devices.devicelist') + lot_id = self.form.lot.data + if lot_id: + next_url = url_for('inventory.devices.lotdevicelist', lot_id=lot_id) + if self.form.validate_on_submit(): self.form.save() - next_url = request.referrer or url_for('inventory.devices.devicelist') return flask.redirect(next_url) @@ -296,11 +311,13 @@ class NewAllocateView(NewActionView, DeviceListMix): _form = AllocateForm def dispatch_request(self, lot_id=None): + self.form = self._form() + next_url = url_for('inventory.devices.devicelist') + lot_id = self.form.lot.data if lot_id: next_url = url_for('inventory.devices.lotdevicelist', lot_id=lot_id) - self.form = self._form() if self.form.validate_on_submit(): self.form.save() return flask.redirect(next_url) @@ -312,8 +329,6 @@ class NewAllocateView(NewActionView, DeviceListMix): devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add')) devices.add_url_rule('/action/allocate/add/', view_func=NewAllocateView.as_view('allocate_add')) -devices.add_url_rule('/lot//action/allocate/add/', - view_func=NewAllocateView.as_view('lot_allocate_add')) devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule('/device//', view_func=DeviceDetailView.as_view('device_details')) devices.add_url_rule('/lot//device/', view_func=DeviceListView.as_view('lotdevicelist')) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index c2fa6f8f..05833fe5 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -10,34 +10,37 @@ $(document).ready(function() { }) function deviceSelect() { - var devices = $(".deviceSelect").filter(':checked'); - var devices_id = $.map(devices, function(x) { return $(x).attr('data')}).join(","); - if (devices_id == "") { - $("#addingLotModal .text-danger").show(); + var devices_count = $(".deviceSelect").filter(':checked').length; + if (devices_count == 0) { + $("#addingLotModal .pol").show(); $("#addingLotModal .btn-primary").hide(); - $("#removeLotModal .text-danger").show(); + $("#removeLotModal .pol").show(); $("#removeLotModal .btn-primary").hide(); - $("#addingTagModal .text-danger").show(); + + $("#addingTagModal .pol").show(); $("#addingTagModal .btn-primary").hide(); + + $("#actionModal .pol").show(); + $("#actionModal .btn-primary").hide(); + + $("#allocateModal .pol").show(); + $("#allocateModal .btn-primary").hide(); } else { - $("#addingLotModal .text-danger").hide(); + $("#addingLotModal .pol").hide(); $("#addingLotModal .btn-primary").show(); - $("#removeLotModal .text-danger").hide(); + $("#removeLotModal .pol").hide(); $("#removeLotModal .btn-primary").show(); - $("#actionModal .text-danger").hide(); + $("#actionModal .pol").hide(); $("#actionModal .btn-primary").show(); - $("#allocateModal .text-danger").hide(); + $("#allocateModal .pol").hide(); $("#allocateModal .btn-primary").show(); - $("#addingTagModal .text-danger").hide(); + $("#addingTagModal .pol").hide(); } - $.map($(".devicesList"), function(x) { - $(x).val(devices_id); - }); } function removeTag() { @@ -52,10 +55,49 @@ function removeTag() { function newAction(action) { $("#actionModal #type").val(action); + $("#actionModal #title-action").html(action); + get_device_list(); + deviceSelect(); $("#activeActionModal").click(); } function newAllocate(action) { $("#allocateModal #type").val(action); + $("#allocateModal #title-action").html(action); + get_device_list(); + deviceSelect(); $("#activeAllocateModal").click(); } + +function get_device_list() { + var devices = $(".deviceSelect").filter(':checked'); + + /* Insert the correct count of devices in actions form */ + var devices_count = devices.length; + $("#allocateModal .devices-count").html(devices_count); + $("#actionModal .devices-count").html(devices_count); + + /* Insert the correct value in the input devicesList */ + var devices_id = $.map(devices, function(x) { return $(x).attr('data')}).join(","); + $.map($(".devicesList"), function(x) { + $(x).val(devices_id); + }); + + /* Create a list of devices for human representation */ + var computer = { + "Desktop": "", + "Laptop": "", + }; + list_devices = devices.map(function (x) { + var typ = $(devices[x]).data("device-type"); + var manuf = $(devices[x]).data("device-manufacturer"); + var dhid = $(devices[x]).data("device-dhid"); + if (computer[typ]) { + typ = computer[typ]; + }; + return typ + " " + manuf + " " + dhid; + }); + + description = $.map(list_devices, function(x) { return x }).join(", "); + $(".enumeration-devices").html(description); +} diff --git a/ereuse_devicehub/templates/inventory/actions.html b/ereuse_devicehub/templates/inventory/actions.html index 61a942c5..f3fc4a7f 100644 --- a/ereuse_devicehub/templates/inventory/actions.html +++ b/ereuse_devicehub/templates/inventory/actions.html @@ -1,24 +1,30 @@