link a tag with one device

This commit is contained in:
Cayo Puigdefabregas 2022-01-25 12:53:36 +01:00
parent 01b355d955
commit 5eb2b0ee5f
5 changed files with 96 additions and 3 deletions

View File

@ -375,6 +375,18 @@ class NewDeviceForm(FlaskForm):
class TagForm(FlaskForm):
code = StringField(u'Code', [validators.length(min=1)])
def validate(self, extra_validators=None):
error = ["This value is being used"]
is_valid = super().validate(extra_validators)
if not is_valid:
return False
tag = Tag.query.filter(Tag.id==self.code.data).all()
if tag:
self.code.errors = error
return False
return True
def save(self):
self.instance = Tag(id=self.code.data)
db.session.add(self.instance)
@ -387,6 +399,43 @@ class TagForm(FlaskForm):
db.session.commit()
return self.instance
class TagDeviceForm(FlaskForm):
tag = StringField(u'Tag')
device = IntegerField(u'Devices')
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if not is_valid:
return False
self._tag = Tag.query.filter(Tag.id == self.tag.data).filter(
Tag.owner_id == g.user.id).one()
device_id = self.device.data
self._device = Device.query.filter(Device.id == device_id).filter(
Device.owner_id == g.user.id).all()
if not self._device:
error = ["You need select one device"]
self.tag.errors = error
return False
self._device = self._device[0]
return True
def save(self):
self._tag.device_id = self._device.id
db.session.add(self._tag)
db.session.commit()
def remove(self):
self._tag.device = None
db.session.add(self._tag)
db.session.commit()
class NewActionForm(FlaskForm):
name = StringField(u'Name')

View File

@ -7,7 +7,7 @@ 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
NewDeviceForm, TagForm, TagDeviceForm
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
@ -22,6 +22,9 @@ class DeviceListView(View):
filter_types = ['Desktop', 'Laptop', 'Server']
lots = Lot.query.filter(Lot.owner_id == current_user.id)
lot = None
tags = Tag.query.filter(Tag.owner_id == current_user.id).filter(
Tag.device_id == None).order_by(Tag.created.desc())
if lot_id:
lot = lots.filter(Lot.id == lot_id).one()
devices = [dev for dev in lot.devices if dev.type in filter_types]
@ -35,7 +38,9 @@ class DeviceListView(View):
context = {'devices': devices,
'lots': lots,
'form_lot_device': LotDeviceForm(),
'lot': lot}
'form_tag_device': TagDeviceForm(),
'lot': lot,
'tags': tags}
return flask.render_template(self.template_name, **context)
@ -184,6 +189,32 @@ class TagAddView(View):
return flask.render_template(self.template_name, form=form)
class TagDeviceAddView(View):
methods = ['POST']
decorators = [login_required]
template_name = 'inventory/device_list.html'
def dispatch_request(self):
form = TagDeviceForm()
if form.validate_on_submit():
form.save()
return flask.redirect(request.referrer)
class TagDeviceDeleteView(View):
methods = ['POST']
decorators = [login_required]
template_name = 'inventory/device_list.html'
def dispatch_request(self):
form = TagDeviceForm()
if form.validate_on_submit():
form.remove()
return flask.redirect(request.referrer)
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
devices.add_url_rule('/device/<string:id>/', view_func=DeviceDetailsView.as_view('device_details'))
devices.add_url_rule('/lot/<string:lot_id>/device/', view_func=DeviceListView.as_view('lotdevicelist'))
@ -196,3 +227,4 @@ devices.add_url_rule('/upload-snapshot/', view_func=UploadSnapshotView.as_view('
devices.add_url_rule('/device/add/', view_func=CreateDeviceView.as_view('device_add'))
devices.add_url_rule('/tag/', view_func=TagListView.as_view('taglist'))
devices.add_url_rule('/tag/add/', view_func=TagAddView.as_view('tag_add'))
devices.add_url_rule('/tag/devices/add/', view_func=TagDeviceAddView.as_view('tag_devices_add'))

View File

@ -11,6 +11,8 @@ function deviceSelect() {
$("#addingLotModal .btn-primary").hide();
$("#removeLotModal .text-danger").show();
$("#removeLotModal .btn-primary").hide();
$("#addingTagModal .text-danger").show();
$("#addingTagModal .btn-primary").hide();
} else {
$("#addingLotModal .text-danger").hide();
$("#addingLotModal .btn-primary").removeClass('d-none');
@ -18,6 +20,8 @@ function deviceSelect() {
$("#removeLotModal .text-danger").hide();
$("#removeLotModal .btn-primary").removeClass('d-none');
$("#removeLotModal .btn-primary").show();
$("#addingTagModal .text-danger").hide();
$("#addingTagModal .btn-primary").removeClass('d-none');
}
$.map($(".devicesList"), function(x) {
$(x).val(devices_id);

View File

@ -185,7 +185,7 @@
</button>
<ul class="dropdown-menu" aria-labelledby="btnTags">
<li>
<a href="javascript:openAdd()" class="dropdown-item">
<a href="javascript:void()" class="dropdown-item" data-bs-toggle="modal" data-bs-target="#addingTagModal">
<i class="bi bi-plus"></i>
Adding Tag
</a>
@ -263,6 +263,7 @@
</div>
</section>
{% include "inventory/addDeviceslot.html" %}
{% include "inventory/addDevicestag.html" %}
{% include "inventory/removeDeviceslot.html" %}
{% include "inventory/removelot.html" %}
{% include "inventory/actions.html" %}

View File

@ -39,6 +39,13 @@
<input type="text" name="code" class="form-control" required value="{{ form.code.data|default('', true) }}">
<div class="invalid-feedback">Please enter a code of the tag.</div>
</div>
{% if form.code.errors %}
<p class="text-danger">
{% for error in form.code.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="col-12">