manager better the error messages in trade form

This commit is contained in:
Cayo Puigdefabregas 2022-02-18 13:37:45 +01:00
parent b72f9f37c6
commit 791d2a5894
6 changed files with 45 additions and 29 deletions

View File

@ -539,6 +539,14 @@ class NewActionForm(FlaskForm):
return self.instance 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): class AllocateForm(NewActionForm):
start_time = DateField(u'Start time') start_time = DateField(u'Start time')
@ -654,36 +662,36 @@ class TradeForm(NewActionForm):
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = self.generic_validation(extra_validators=extra_validators) 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: if not self.confirm.data and not self.code.data:
self.code.errors = ["If you don't want confirm, you need a code"] self.code.errors = ["If you don't want confirm, you need a code"]
is_valid = False is_valid = False
if self.confirm.data and not (self.user_to.data or self.user_to.data): if self.confirm.data and not (email_from and email_to) or email_to == email_from or \
errors = ["If you want confirm, you need a email"] g.user.email not in [email_from, email_to]:
if not self.user_to.data:
self.user_to.errors = errors
if not self.user_from.data: errors = ["If you want confirm, you need a correct email"]
self.user_to.errors = errors
self.user_from.errors = errors self.user_from.errors = errors
is_valid = False is_valid = False
if self.confirm.data and is_valid: if self.confirm.data and is_valid:
user_to = User.query.filter_by(email=self.user_to.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=self.user_from.data).first() or g.user user_from = User.query.filter_by(email=email_from).first() or g.user
if user_to == user_from: if user_to == user_from:
is_valid = False is_valid = False
else: else:
self.user_to = user_to self.db_user_to = user_to
self.user_from = user_from self.db_user_from = user_from
return is_valid return is_valid
def save(self): def save(self):
self.create_phantom_account() self.create_phantom_account()
self.prepare_instance() self.prepare_instance()
# import pdb; pdb.set_trace()
self.create_automatic_trade() self.create_automatic_trade()
db.session.commit() db.session.commit()
@ -693,9 +701,8 @@ class TradeForm(NewActionForm):
def prepare_instance(self): def prepare_instance(self):
Model = db.Model._decl_class_registry.data[self.type.data]() Model = db.Model._decl_class_registry.data[self.type.data]()
self.instance = Model() self.instance = Model()
# import pdb; pdb.set_trace() self.instance.user_from = self.db_user_from
self.instance.user_from = self.user_from self.instance.user_to = self.db_user_to
self.instance.user_to = self.user_to
self.instance.lot_id = self._lot.id self.instance.lot_id = self._lot.id
self.instance.devices = self._lot.devices self.instance.devices = self._lot.devices
self.instance.code = self.code.data self.instance.code = self.code.data
@ -757,4 +764,14 @@ class TradeForm(NewActionForm):
# Change the owner for every devices # Change the owner for every devices
for dev in self._lot.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'

View File

@ -41,9 +41,8 @@ class DeviceListMix(View):
form_new_datawipe = DataWipeForm(lot=lot.id) form_new_datawipe = DataWipeForm(lot=lot.id)
form_new_trade = TradeForm( form_new_trade = TradeForm(
lot=lot.id, lot=lot.id,
receiver=g.user.email, user_to=g.user.email,
supplier=g.user.email, user_from=g.user.email,
type='Trade'
) )
else: else:
devices = Device.query.filter( devices = Device.query.filter(
@ -379,7 +378,7 @@ class NewTradeView(NewActionView, DeviceListMix):
lot_id = self.form.lot.data lot_id = self.form.lot.data
self.get_context(lot_id) 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) return flask.render_template(self.template_name, **self.context)

View File

@ -1,12 +1,16 @@
$(document).ready(function() { $(document).ready(function() {
var show_allocate_form = $("#allocateModal").data('show-action-form'); var show_allocate_form = $("#allocateModal").data('show-action-form');
var show_datawipe_form = $("#datawipeModal").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") { if (show_allocate_form != "None") {
$("#allocateModal .btn-primary").show(); $("#allocateModal .btn-primary").show();
newAllocate(show_allocate_form); newAllocate(show_allocate_form);
} else if (show_datawipe_form != "None") { } else if (show_datawipe_form != "None") {
$("#datawipeModal .btn-primary").show(); $("#datawipeModal .btn-primary").show();
newDataWipe(show_datawipe_form); newDataWipe(show_datawipe_form);
} else if (show_trade_form != "None") {
$("#tradeLotModal .btn-primary").show();
newTrade(show_trade_form);
} else { } else {
$(".deviceSelect").on("change", deviceSelect); $(".deviceSelect").on("change", deviceSelect);
} }
@ -58,7 +62,6 @@ function deviceSelect() {
function removeTag() { function removeTag() {
var devices = $(".deviceSelect").filter(':checked'); var devices = $(".deviceSelect").filter(':checked');
var devices_id = $.map(devices, function(x) { return $(x).attr('data')}); var devices_id = $.map(devices, function(x) { return $(x).attr('data')});
console.log(devices_id);
if (devices_id.length > 0) { if (devices_id.length > 0) {
var url = "/inventory/tag/devices/"+devices_id[0]+"/del/"; var url = "/inventory/tag/devices/"+devices_id[0]+"/del/";
window.location.href = url; window.location.href = url;
@ -87,7 +90,7 @@ function newTrade(action) {
$("#user_to").val(''); $("#user_to").val('');
$("#user_from").val(user_from); $("#user_from").val(user_from);
} }
$("#tradeLotModalModal #title-action").html(title); $("#tradeLotModal #title-action").html(title);
$("#activeTradeModal").click(); $("#activeTradeModal").click();
} }

View File

@ -1,5 +1,5 @@
<div class="modal fade" id="allocateModal" tabindex="-1" style="display: none;" aria-hidden="true" <div class="modal fade" id="allocateModal" tabindex="-1" style="display: none;" aria-hidden="true"
data-show-action-form="{{ form_new_allocate.type.data }}"> data-show-action-form="{{ form_new_allocate.check_valid() }}">
<div class="modal-dialog modal-lg"> <div class="modal-dialog modal-lg">
<div class="modal-content"> <div class="modal-content">

View File

@ -1,5 +1,5 @@
<div class="modal fade" id="datawipeModal" tabindex="-1" style="display: none;" aria-hidden="true" <div class="modal fade" id="datawipeModal" tabindex="-1" style="display: none;" aria-hidden="true"
data-show-action-form="{{ form_new_datawipe.type.data }}"> data-show-action-form="{{ form_new_datawipe.check_valid() }}">
<div class="modal-dialog modal-lg"> <div class="modal-dialog modal-lg">
<div class="modal-content"> <div class="modal-content">

View File

@ -1,4 +1,5 @@
<div class="modal fade" id="tradeLotModal" tabindex="-1" style="display: none;" aria-hidden="true"> <div class="modal fade" id="tradeLotModal" tabindex="-1" style="display: none;" aria-hidden="true"
data-show-action-form="{{ form_new_trade.check_valid() }}">
<div class="modal-dialog modal-lg"> <div class="modal-dialog modal-lg">
<div class="modal-content"> <div class="modal-content">
@ -13,11 +14,7 @@
{% for field in form_new_trade %} {% for field in form_new_trade %}
{% if field != form_new_trade.csrf_token %} {% if field != form_new_trade.csrf_token %}
{% if field == form_new_trade.devices %} {% if field == form_new_trade.devices %}
<div class="col-12"> {{ field }}
{{ field.label(class_="form-label") }}: <span class="devices-count"></span>
{{ field(class_="devicesList") }}
<p class="enumeration-devices"></p>
</div>
{% elif field == form_new_trade.lot %} {% elif field == form_new_trade.lot %}
{{ field }} {{ field }}
{% elif field == form_new_trade.type %} {% elif field == form_new_trade.type %}