remove tag from device

This commit is contained in:
Cayo Puigdefabregas 2022-01-25 14:39:15 +01:00
parent 9d3f288213
commit 9b87a7415e
5 changed files with 105 additions and 20 deletions

View File

@ -1,6 +1,7 @@
import json
from flask_wtf import FlaskForm
from wtforms import StringField, validators, MultipleFileField, FloatField, IntegerField
from wtforms import StringField, validators, MultipleFileField, FloatField, IntegerField, \
SelectField
from flask import g, request
from sqlalchemy.util import OrderedSet
from json.decoder import JSONDecodeError
@ -400,8 +401,22 @@ class TagForm(FlaskForm):
return self.instance
class TagDeviceForm(FlaskForm):
tag = StringField(u'Tag')
device = IntegerField(u'Devices')
tag = SelectField(u'Tag', choices=[])
def __init__(self, *args, **kwargs):
delete = kwargs.pop('delete', None)
device_id = kwargs.pop('device', None)
self._device = Device.query.filter(Device.id == device_id).filter(
Device.owner_id == g.user.id).one()
super().__init__(*args, **kwargs)
if delete:
tags = Tag.query.filter(Tag.owner_id==g.user.id).filter(Tag.device_id==device_id)
else:
tags = Tag.query.filter(Tag.owner_id==g.user.id).filter(Tag.device_id==None)
self.tag.choices = [(tag.id, tag.id) for tag in tags]
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
@ -409,21 +424,9 @@ class TagDeviceForm(FlaskForm):
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):

View File

@ -203,17 +203,19 @@ class TagDeviceAddView(View):
class TagDeviceDeleteView(View):
methods = ['POST']
methods = ['POST', 'GET']
decorators = [login_required]
template_name = 'inventory/device_list.html'
template_name = 'inventory/removeDevicestag.html'
def dispatch_request(self):
form = TagDeviceForm()
def dispatch_request(self, id):
form = TagDeviceForm(delete=True, device=id)
if form.validate_on_submit():
form.remove()
return flask.redirect(request.referrer)
return flask.render_template(self.template_name, form=form, referrer=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'))
@ -228,3 +230,4 @@ devices.add_url_rule('/device/add/', view_func=CreateDeviceView.as_view('device_
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'))
devices.add_url_rule('/tag/devices/<int:id>/del/', view_func=TagDeviceDeleteView.as_view('tag_devices_del'))

View File

@ -28,6 +28,16 @@ function deviceSelect() {
});
}
function removeTag() {
var devices = $(".deviceSelect").filter(':checked');
var devices_id = $.map(devices, function(x) { return $(x).attr('data')});
console.log(devices_id);
if (devices_id.length > 0) {
var url = "/inventory/tag/devices/"+devices_id[0]+"/del/";
window.location.href = url;
}
}
function newAction(action) {
console.log(action);
}

View File

@ -191,7 +191,7 @@
</a>
</li>
<li>
<a href="javascript:openRemove()" class="dropdown-item">
<a href="javascript:removeTag()" class="dropdown-item">
<i class="bi bi-x"></i>
Remove Tag
</a>

View File

@ -0,0 +1,69 @@
{% extends "ereuse_devicehub/base_site.html" %}
{% block main %}
<div class="pagetitle">
<h1>{{ title }}</h1>
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">Inventory</a></li>
<li class="breadcrumb-item">Tag</li>
<li class="breadcrumb-item">Device</li>
<li class="breadcrumb-item">Remove</li>
</ol>
</nav>
</div><!-- End Page Title -->
<section class="section profile">
<div class="row">
<div class="col-xl-4">
<div class="card">
<div class="card-body">
<div class="pt-4 pb-2">
<h5 class="card-title text-center pb-0 fs-4">Unlink tag of device</h5>
<p class="text-center small">Please enter a code for the tag.</p>
{% if form.form_errors %}
<p class="text-danger">
{% for error in form.form_errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<form method="post" class="row g-3 needs-validation" novalidate>
{{ form.csrf_token }}
<div class="col-12">
<label for="tag" class="form-label">Tag</label>
<div class="input-group has-validation">
{{ form.tag(class_="form-control") }}
<div class="invalid-feedback">Please select tag.</div>
</div>
{% if form.tag.errors %}
<p class="text-danger">
{% for error in form.tag.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<div class="col-12">
<a href="{{ referrer }}" class="btn btn-danger">Cancel</a>
<button class="btn btn-primary" type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-xl-8">
</div>
</div>
</section>
{% endblock main %}