From 5eb2b0ee5f8ccd6f3dfae3f3475ce80aefc86a9e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 25 Jan 2022 12:53:36 +0100 Subject: [PATCH] link a tag with one device --- ereuse_devicehub/inventory/forms.py | 49 +++++++++++++++++++ ereuse_devicehub/inventory/views.py | 36 +++++++++++++- ereuse_devicehub/static/js/main_inventory.js | 4 ++ .../templates/inventory/device_list.html | 3 +- ereuse_devicehub/templates/inventory/tag.html | 7 +++ 5 files changed, 96 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 0d77ad48..13d97032 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -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') diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 7de67fae..ca0e1173 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -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//', view_func=DeviceDetailsView.as_view('device_details')) devices.add_url_rule('/lot//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')) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index d876ddca..798e607f 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -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); diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 837d8993..1a6298c3 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -185,7 +185,7 @@