From cd16baaadbd581bb1048726fd757d0a78fd4e430 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 9 Feb 2022 14:06:28 +0100 Subject: [PATCH 01/31] skeleton for trade action --- ereuse_devicehub/inventory/forms.py | 11 ++++ ereuse_devicehub/inventory/views.py | 27 ++++++++- .../templates/inventory/device_list.html | 35 ++++++++--- .../templates/inventory/trade.html | 59 +++++++++++++++++++ 4 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 ereuse_devicehub/templates/inventory/trade.html diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index a90ecf1b..42f8507f 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -619,3 +619,14 @@ class DataWipeForm(NewActionForm): self.document = document return self.instance + + +class TradeForm(NewActionForm): + supplier = StringField(u'Supplier', [validators.DataRequired()], + description="Please enter the supplier's email address") + receiver = StringField(u'Receiver', [validators.DataRequired()], + description="Please enter the receiver's email address") + confirm = BooleanField(u'Confirm', [validators.Optional()], + description="I need confirmation from the other user for every device and document.") + code = StringField(u'Code', [validators.Optional()], + description="If you don't need confirm, you need put a code for trace the user in the statistics.") diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 1eae4de2..230cfb2a 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -8,7 +8,8 @@ from ereuse_devicehub.inventory.forms import (AllocateForm, LotDeviceForm, LotForm, NewActionForm, NewDeviceForm, TagDeviceForm, TagForm, TagUnnamedForm, - UploadSnapshotForm, DataWipeForm) + UploadSnapshotForm, DataWipeForm, + TradeForm) from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.tag.model import Tag @@ -37,6 +38,7 @@ class DeviceListMix(View): form_new_action = NewActionForm(lot=lot.id) form_new_allocate = AllocateForm(lot=lot.id) form_new_datawipe = DataWipeForm(lot=lot.id) + form_new_trade = TradeForm(lot=lot.id) else: devices = Device.query.filter( Device.owner_id == current_user.id).filter( @@ -45,6 +47,7 @@ class DeviceListMix(View): form_new_action = NewActionForm() form_new_allocate = AllocateForm() form_new_datawipe = DataWipeForm() + form_new_trade = TradeForm() action_devices = form_new_action.devices.data list_devices = [] @@ -59,6 +62,7 @@ class DeviceListMix(View): 'form_new_action': form_new_action, 'form_new_allocate': form_new_allocate, 'form_new_datawipe': form_new_datawipe, + 'form_new_trade': form_new_trade, 'lot': lot, 'tags': tags, 'list_devices': list_devices @@ -354,7 +358,28 @@ class NewDataWipeView(NewActionView, DeviceListMix): return flask.render_template(self.template_name, **self.context) +class NewTradeView(NewActionView, DeviceListMix): + methods = ['POST'] + form_class = TradeForm + + def dispatch_request(self): + self.form = self.form_class() + + if self.form.validate_on_submit(): + instance = self.form.save() + messages.success('Action "{}" created successfully!'.format(instance.type)) + + next_url = self.get_next_url() + return flask.redirect(next_url) + + lot_id = self.form.lot.data + self.get_context(lot_id) + self.context['form_new_datawipe'] = self.form + return flask.render_template(self.template_name, **self.context) + + devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add')) +devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add')) devices.add_url_rule('/action/allocate/add/', view_func=NewAllocateView.as_view('allocate_add')) devices.add_url_rule('/action/datawipe/add/', view_func=NewDataWipeView.as_view('datawipe_add')) devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index a165e387..ab69be49 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -44,20 +44,22 @@ @@ -282,6 +300,7 @@ {% include "inventory/actions.html" %} {% include "inventory/allocate.html" %} {% include "inventory/data_wipe.html" %} +{% include "inventory/trade.html" %} diff --git a/ereuse_devicehub/templates/inventory/trade.html b/ereuse_devicehub/templates/inventory/trade.html new file mode 100644 index 00000000..709c2831 --- /dev/null +++ b/ereuse_devicehub/templates/inventory/trade.html @@ -0,0 +1,59 @@ + From f3ffcd89ca4c341b6329d27e4b7895346c522f44 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 10 Feb 2022 13:22:47 +0100 Subject: [PATCH 02/31] fixing js form for trade --- ereuse_devicehub/inventory/forms.py | 26 ++++++++++++++++--- ereuse_devicehub/inventory/views.py | 15 +++++++++-- ereuse_devicehub/static/js/main_inventory.js | 21 +++++++++++++++ .../templates/inventory/device_list.html | 7 +++-- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 42f8507f..a0ffbde9 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -622,11 +622,29 @@ class DataWipeForm(NewActionForm): class TradeForm(NewActionForm): - supplier = StringField(u'Supplier', [validators.DataRequired()], - description="Please enter the supplier's email address") - receiver = StringField(u'Receiver', [validators.DataRequired()], - description="Please enter the receiver's email address") + supplier = StringField(u'Supplier', [validators.Optional()], + description="Please enter the supplier's email address", + render_kw={'data-email': ""}) + receiver = StringField(u'Receiver', [validators.Optional()], + description="Please enter the receiver's email address", + render_kw={'data-email': ""}) confirm = BooleanField(u'Confirm', [validators.Optional()], + default=True, description="I need confirmation from the other user for every device and document.") code = StringField(u'Code', [validators.Optional()], description="If you don't need confirm, you need put a code for trace the user in the statistics.") + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.supplier.render_kw['data-email'] = g.user.email + self.receiver.render_kw['data-email'] = g.user.email + + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + + if not self.confirm and not self.code: + self.code.errors = ["If you don't want confirm, you need a code"] + is_valid = False + + return is_valid + diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 230cfb2a..b2b413e5 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1,4 +1,5 @@ import flask +from flask import g from flask import Blueprint, request, url_for from flask.views import View from flask_login import current_user, login_required @@ -38,7 +39,12 @@ class DeviceListMix(View): form_new_action = NewActionForm(lot=lot.id) form_new_allocate = AllocateForm(lot=lot.id) form_new_datawipe = DataWipeForm(lot=lot.id) - form_new_trade = TradeForm(lot=lot.id) + form_new_trade = TradeForm( + lot=lot.id, + receiver=g.user.email, + supplier=g.user.email, + type='Trade' + ) else: devices = Device.query.filter( Device.owner_id == current_user.id).filter( @@ -47,7 +53,11 @@ class DeviceListMix(View): form_new_action = NewActionForm() form_new_allocate = AllocateForm() form_new_datawipe = DataWipeForm() - form_new_trade = TradeForm() + form_new_trade = TradeForm( + receiver=g.user.email, + supplier=g.user.email, + type='Trade' + ) action_devices = form_new_action.devices.data list_devices = [] @@ -301,6 +311,7 @@ class NewActionView(View): def dispatch_request(self): self.form = self.form_class() + # import pdb; pdb.set_trace() if self.form.validate_on_submit(): instance = self.form.save() diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 1eb680fd..1c054f99 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -63,6 +63,27 @@ function removeTag() { } } +function newTrade(action) { + var title = "Trade " + var receiver = $("#receiver").data("email"); + var supplier = $("#supplier").data("email"); + if (action == 'supplier') { + title = 'Trade Incoming'; + $("#receiver").attr('disabled', 'disabled'); + $("#supplier").prop('disabled', false); + $("#supplier").val(''); + $("#receiver").val(receiver); + } else if (action == 'receiver') { + title = 'Trade Outgoing'; + $("#supplier").attr('disabled', 'disabled'); + $("#receiver").prop('disabled', false); + $("#receiver").val(''); + $("#supplier").val(supplier); + } + $("#tradeLotModalModal #title-action").html(title); + $("#activeTradeModal").click(); +} + function newAction(action) { $("#actionModal #type").val(action); $("#actionModal #title-action").html(action); diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index ab69be49..fe389df4 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -49,6 +49,7 @@ Lots +