remove device from a lot
This commit is contained in:
parent
9413e7b3c7
commit
d17114a0b0
|
@ -1,6 +1,5 @@
|
|||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, validators
|
||||
from flask_login import current_user
|
||||
from flask import g
|
||||
|
||||
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
|
||||
|
||||
|
||||
class LotDeviceAddForm(FlaskForm):
|
||||
class LotDeviceForm(FlaskForm):
|
||||
lot = StringField(u'Lot', [validators.UUID()])
|
||||
devices = StringField(u'Devices', [validators.length(min=1)])
|
||||
|
||||
|
@ -18,22 +17,26 @@ class LotDeviceAddForm(FlaskForm):
|
|||
if not is_valid:
|
||||
return False
|
||||
|
||||
self.lot = Lot.query.filter(Lot.id == self.lot.data).filter(
|
||||
Lot.owner_id == current_user.id).one()
|
||||
self._lot = Lot.query.filter(Lot.id == self.lot.data).filter(
|
||||
Lot.owner_id == g.user.id).one()
|
||||
|
||||
devices = set(self.devices.data.split(","))
|
||||
self.devices = set(Device.query.filter(Device.id.in_(devices)).filter(
|
||||
Device.owner_id == current_user.id).all())
|
||||
self._devices = set(Device.query.filter(Device.id.in_(devices)).filter(
|
||||
Device.owner_id == g.user.id).all())
|
||||
|
||||
if not self.devices:
|
||||
if not self._devices:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def save(self):
|
||||
self.lot.devices.update(self.devices)
|
||||
g.user = current_user
|
||||
db.session.add(self.lot)
|
||||
self._lot.devices.update(self._devices)
|
||||
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()
|
||||
|
||||
|
||||
|
@ -45,7 +48,7 @@ class LotForm(FlaskForm):
|
|||
self.lot = None
|
||||
if id:
|
||||
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)
|
||||
if self.lot and not self.name.data:
|
||||
self.name.data = self.lot.name
|
||||
|
@ -59,6 +62,5 @@ class LotForm(FlaskForm):
|
|||
else:
|
||||
self.lot = Lot(name=name)
|
||||
|
||||
g.user = current_user
|
||||
db.session.add(self.lot)
|
||||
db.session.commit()
|
||||
|
|
|
@ -5,7 +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 LotDeviceAddForm, LotForm
|
||||
from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm
|
||||
|
||||
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
|
||||
|
||||
|
@ -29,7 +29,7 @@ class DeviceListView(View):
|
|||
|
||||
context = {'devices': devices,
|
||||
'lots': lots,
|
||||
'form_lot_device': LotDeviceAddForm(),
|
||||
'form_lot_device': LotDeviceForm(),
|
||||
'lot': lot}
|
||||
return flask.render_template(self.template_name, **context)
|
||||
|
||||
|
@ -40,11 +40,25 @@ class LotDeviceAddView(View):
|
|||
template_name = 'inventory/device_list.html'
|
||||
|
||||
def dispatch_request(self):
|
||||
form = LotDeviceAddForm()
|
||||
form = LotDeviceForm()
|
||||
if form.validate_on_submit():
|
||||
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)
|
||||
|
||||
|
||||
|
@ -69,5 +83,6 @@ class LotView(View):
|
|||
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/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/<string:id>/', view_func=LotView.as_view('lot_edit'))
|
||||
|
|
|
@ -9,10 +9,15 @@ function deviceSelect() {
|
|||
if (devices_id == "") {
|
||||
$("#addingLotModal .text-danger").show();
|
||||
$("#addingLotModal .btn-primary").hide();
|
||||
$("#removeLotModal .text-danger").show();
|
||||
$("#removeLotModal .btn-primary").hide();
|
||||
} else {
|
||||
$("#addingLotModal .text-danger").hide();
|
||||
$("#addingLotModal .btn-primary").removeClass('d-none');
|
||||
$("#addingLotModal .btn-primary").show();
|
||||
$("#removeLotModal .text-danger").hide();
|
||||
$("#removeLotModal .btn-primary").removeClass('d-none');
|
||||
$("#removeLotModal .btn-primary").show();
|
||||
}
|
||||
$.map($(".devicesList"), function(x) {
|
||||
$(x).val(devices_id);
|
||||
|
|
|
@ -2,16 +2,31 @@
|
|||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<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>
|
||||
</div>
|
||||
|
||||
<form action="{{ url_for('inventory.devices.lot_devices_del') }}" method="post">
|
||||
{{ form_lot_device.csrf_token }}
|
||||
<div class="modal-body">
|
||||
Please select a lot
|
||||
Please write a name of a lot
|
||||
<select class="form-control selectpicker" id="selectLot" name="lot" data-live-search="true">
|
||||
{% 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>
|
||||
<button type="button" class="btn btn-primary">Save changes</button>
|
||||
</div>
|
||||
<input type="submit" class="btn btn-primary d-none" value="Save changes" />
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Reference in New Issue