From 791d2a5894f5c0418f27566cfad8bd8610f95fb2 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 18 Feb 2022 13:37:45 +0100 Subject: [PATCH] manager better the error messages in trade form --- ereuse_devicehub/inventory/forms.py | 47 +++++++++++++------ ereuse_devicehub/inventory/views.py | 7 ++- ereuse_devicehub/static/js/main_inventory.js | 7 ++- .../templates/inventory/allocate.html | 2 +- .../templates/inventory/data_wipe.html | 2 +- .../templates/inventory/trade.html | 9 ++-- 6 files changed, 45 insertions(+), 29 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 68a2573c..7c1150be 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -539,6 +539,14 @@ class NewActionForm(FlaskForm): return self.instance + def check_valid(self): + # import pdb; pdb.set_trace() + if self.type.data in ['', None]: + return + + if not self.validate(): + return self.type.data + class AllocateForm(NewActionForm): start_time = DateField(u'Start time') @@ -654,36 +662,36 @@ class TradeForm(NewActionForm): def validate(self, extra_validators=None): is_valid = self.generic_validation(extra_validators=extra_validators) + email_from = self.user_from.data + email_to = self.user_to.data if not self.confirm.data and not self.code.data: self.code.errors = ["If you don't want confirm, you need a code"] is_valid = False - if self.confirm.data and not (self.user_to.data or self.user_to.data): - errors = ["If you want confirm, you need a email"] - if not self.user_to.data: - self.user_to.errors = errors + if self.confirm.data and not (email_from and email_to) or email_to == email_from or \ + g.user.email not in [email_from, email_to]: - if not self.user_from.data: - self.user_from.errors = errors + errors = ["If you want confirm, you need a correct email"] + self.user_to.errors = errors + self.user_from.errors = errors is_valid = False if self.confirm.data and is_valid: - user_to = User.query.filter_by(email=self.user_to.data).first() or g.user - user_from = User.query.filter_by(email=self.user_from.data).first() or g.user + user_to = User.query.filter_by(email=email_to).first() or g.user + user_from = User.query.filter_by(email=email_from).first() or g.user if user_to == user_from: is_valid = False else: - self.user_to = user_to - self.user_from = user_from + self.db_user_to = user_to + self.db_user_from = user_from return is_valid def save(self): self.create_phantom_account() self.prepare_instance() - # import pdb; pdb.set_trace() self.create_automatic_trade() db.session.commit() @@ -693,9 +701,8 @@ class TradeForm(NewActionForm): def prepare_instance(self): Model = db.Model._decl_class_registry.data[self.type.data]() self.instance = Model() - # import pdb; pdb.set_trace() - self.instance.user_from = self.user_from - self.instance.user_to = self.user_to + self.instance.user_from = self.db_user_from + self.instance.user_to = self.db_user_to self.instance.lot_id = self._lot.id self.instance.devices = self._lot.devices self.instance.code = self.code.data @@ -757,4 +764,14 @@ class TradeForm(NewActionForm): # Change the owner for every devices for dev in self._lot.devices: - dev.change_owner(self.user_to) + dev.change_owner(self.db_user_to) + + def check_valid(self): + if self.user_from.data == self.user_to.data: + return + + if self.user_from.data == g.user.email: + return 'user_to' + + if self.user_to.data == g.user.email: + return 'user_form' diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index ea684867..85650fe6 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -41,9 +41,8 @@ class DeviceListMix(View): form_new_datawipe = DataWipeForm(lot=lot.id) form_new_trade = TradeForm( lot=lot.id, - receiver=g.user.email, - supplier=g.user.email, - type='Trade' + user_to=g.user.email, + user_from=g.user.email, ) else: devices = Device.query.filter( @@ -379,7 +378,7 @@ class NewTradeView(NewActionView, DeviceListMix): lot_id = self.form.lot.data self.get_context(lot_id) - self.context['form_new_datawipe'] = self.form + self.context['form_new_trade'] = self.form return flask.render_template(self.template_name, **self.context) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 83ed993d..1147241d 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -1,12 +1,16 @@ $(document).ready(function() { var show_allocate_form = $("#allocateModal").data('show-action-form'); var show_datawipe_form = $("#datawipeModal").data('show-action-form'); + var show_trade_form = $("#tradeLotModal").data('show-action-form'); if (show_allocate_form != "None") { $("#allocateModal .btn-primary").show(); newAllocate(show_allocate_form); } else if (show_datawipe_form != "None") { $("#datawipeModal .btn-primary").show(); newDataWipe(show_datawipe_form); + } else if (show_trade_form != "None") { + $("#tradeLotModal .btn-primary").show(); + newTrade(show_trade_form); } else { $(".deviceSelect").on("change", deviceSelect); } @@ -58,7 +62,6 @@ 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; @@ -87,7 +90,7 @@ function newTrade(action) { $("#user_to").val(''); $("#user_from").val(user_from); } - $("#tradeLotModalModal #title-action").html(title); + $("#tradeLotModal #title-action").html(title); $("#activeTradeModal").click(); } diff --git a/ereuse_devicehub/templates/inventory/allocate.html b/ereuse_devicehub/templates/inventory/allocate.html index fb16ce86..989bc74b 100644 --- a/ereuse_devicehub/templates/inventory/allocate.html +++ b/ereuse_devicehub/templates/inventory/allocate.html @@ -1,5 +1,5 @@