remove device from a lot

This commit is contained in:
Cayo Puigdefabregas 2022-01-03 11:32:12 +01:00
parent 9413e7b3c7
commit d17114a0b0
4 changed files with 61 additions and 24 deletions

View File

@ -1,6 +1,5 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, validators from wtforms import StringField, validators
from flask_login import current_user
from flask import g from flask import g
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
@ -8,7 +7,7 @@ from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.lot.models import Lot
class LotDeviceAddForm(FlaskForm): class LotDeviceForm(FlaskForm):
lot = StringField(u'Lot', [validators.UUID()]) lot = StringField(u'Lot', [validators.UUID()])
devices = StringField(u'Devices', [validators.length(min=1)]) devices = StringField(u'Devices', [validators.length(min=1)])
@ -18,22 +17,26 @@ class LotDeviceAddForm(FlaskForm):
if not is_valid: if not is_valid:
return False return False
self.lot = Lot.query.filter(Lot.id == self.lot.data).filter( self._lot = Lot.query.filter(Lot.id == self.lot.data).filter(
Lot.owner_id == current_user.id).one() Lot.owner_id == g.user.id).one()
devices = set(self.devices.data.split(",")) devices = set(self.devices.data.split(","))
self.devices = set(Device.query.filter(Device.id.in_(devices)).filter( self._devices = set(Device.query.filter(Device.id.in_(devices)).filter(
Device.owner_id == current_user.id).all()) Device.owner_id == g.user.id).all())
if not self.devices: if not self._devices:
return False return False
return True return True
def save(self): def save(self):
self.lot.devices.update(self.devices) self._lot.devices.update(self._devices)
g.user = current_user db.session.add(self._lot)
db.session.add(self.lot) db.session.commit()
def remove(self):
self._lot.devices.difference_update(self._devices)
db.session.add(self._lot)
db.session.commit() db.session.commit()
@ -45,7 +48,7 @@ class LotForm(FlaskForm):
self.lot = None self.lot = None
if id: if id:
self.lot = Lot.query.filter(Lot.id == id).filter( self.lot = Lot.query.filter(Lot.id == id).filter(
Lot.owner_id == current_user.id).one() Lot.owner_id == g.user.id).one()
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.lot and not self.name.data: if self.lot and not self.name.data:
self.name.data = self.lot.name self.name.data = self.lot.name
@ -59,6 +62,5 @@ class LotForm(FlaskForm):
else: else:
self.lot = Lot(name=name) self.lot = Lot(name=name)
g.user = current_user
db.session.add(self.lot) db.session.add(self.lot)
db.session.commit() db.session.commit()

View File

@ -5,7 +5,7 @@ from flask_login import login_required, current_user
from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.inventory.forms import LotDeviceAddForm, LotForm from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
@ -29,7 +29,7 @@ class DeviceListView(View):
context = {'devices': devices, context = {'devices': devices,
'lots': lots, 'lots': lots,
'form_lot_device': LotDeviceAddForm(), 'form_lot_device': LotDeviceForm(),
'lot': lot} 'lot': lot}
return flask.render_template(self.template_name, **context) return flask.render_template(self.template_name, **context)
@ -40,11 +40,25 @@ class LotDeviceAddView(View):
template_name = 'inventory/device_list.html' template_name = 'inventory/device_list.html'
def dispatch_request(self): def dispatch_request(self):
form = LotDeviceAddForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.save() form.save()
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.id) next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data)
return flask.redirect(next_url)
class LotDeviceDeleteView(View):
methods = ['POST']
decorators = [login_required]
template_name = 'inventory/device_list.html'
def dispatch_request(self):
form = LotDeviceForm()
if form.validate_on_submit():
form.remove()
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data)
return flask.redirect(next_url) return flask.redirect(next_url)
@ -69,5 +83,6 @@ class LotView(View):
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
devices.add_url_rule('/lot/<string:id>/device/', view_func=DeviceListView.as_view('lotdevicelist')) devices.add_url_rule('/lot/<string:id>/device/', view_func=DeviceListView.as_view('lotdevicelist'))
devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add')) devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add'))
devices.add_url_rule('/lot/devices/del/', view_func=LotDeviceDeleteView.as_view('lot_devices_del'))
devices.add_url_rule('/lot/add', view_func=LotView.as_view('lot_add')) devices.add_url_rule('/lot/add', view_func=LotView.as_view('lot_add'))
devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit')) devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit'))

View File

@ -9,10 +9,15 @@ function deviceSelect() {
if (devices_id == "") { if (devices_id == "") {
$("#addingLotModal .text-danger").show(); $("#addingLotModal .text-danger").show();
$("#addingLotModal .btn-primary").hide(); $("#addingLotModal .btn-primary").hide();
$("#removeLotModal .text-danger").show();
$("#removeLotModal .btn-primary").hide();
} else { } else {
$("#addingLotModal .text-danger").hide(); $("#addingLotModal .text-danger").hide();
$("#addingLotModal .btn-primary").removeClass('d-none'); $("#addingLotModal .btn-primary").removeClass('d-none');
$("#addingLotModal .btn-primary").show(); $("#addingLotModal .btn-primary").show();
$("#removeLotModal .text-danger").hide();
$("#removeLotModal .btn-primary").removeClass('d-none');
$("#removeLotModal .btn-primary").show();
} }
$.map($(".devicesList"), function(x) { $.map($(".devicesList"), function(x) {
$(x).val(devices_id); $(x).val(devices_id);

View File

@ -2,16 +2,31 @@
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title">Remove from a lot</h5> <h5 class="modal-title">Remove from lot</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<div class="modal-body">
Please select a lot <form action="{{ url_for('inventory.devices.lot_devices_del') }}" method="post">
</div> {{ form_lot_device.csrf_token }}
<div class="modal-footer"> <div class="modal-body">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> Please write a name of a lot
<button type="button" class="btn btn-primary">Save changes</button> <select class="form-control selectpicker" id="selectLot" name="lot" data-live-search="true">
</div> {% for lot in lots %}
<option value="{{ lot.id }}">{{ lot.name }}</option>
{% endfor %}
</select>
<input class="devicesList" type="hidden" name="devices" />
<p class="text-danger">
You need select first some device for remove this from a lot
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary d-none" value="Save changes" />
</div>
</form>
</div> </div>
</div> </div>
</div> </div>