diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 13d97032..2f2dff34 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -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): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index ca0e1173..e71180d7 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -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//', 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//del/', view_func=TagDeviceDeleteView.as_view('tag_devices_del')) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 798e607f..ab499ee4 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -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); } diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 1a6298c3..bb48ff04 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -191,7 +191,7 @@
  • - + Remove Tag diff --git a/ereuse_devicehub/templates/inventory/removeDevicestag.html b/ereuse_devicehub/templates/inventory/removeDevicestag.html new file mode 100644 index 00000000..893d5a52 --- /dev/null +++ b/ereuse_devicehub/templates/inventory/removeDevicestag.html @@ -0,0 +1,69 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
    +

    {{ title }}

    + +
    + +
    +
    +
    + +
    +
    + +
    +
    Unlink tag of device
    +

    Please enter a code for the tag.

    + {% if form.form_errors %} +

    + {% for error in form.form_errors %} + {{ error }}
    + {% endfor %} +

    + {% endif %} +
    + +
    + {{ form.csrf_token }} + +
    + +
    + {{ form.tag(class_="form-control") }} +
    Please select tag.
    +
    + {% if form.tag.errors %} +

    + {% for error in form.tag.errors %} + {{ error }}
    + {% endfor %} +

    + {% endif %} +
    + +
    + Cancel + +
    +
    + +
    + +
    + +
    + +
    +
    +
    +
    +{% endblock main %}