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
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'

View file

@ -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)

View file

@ -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();
}

View file

@ -1,5 +1,5 @@
<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-content">

View file

@ -1,5 +1,5 @@
<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-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-content">
@ -13,11 +14,7 @@
{% for field in form_new_trade %}
{% if field != form_new_trade.csrf_token %}
{% if field == form_new_trade.devices %}
<div class="col-12">
{{ field.label(class_="form-label") }}: <span class="devices-count"></span>
{{ field(class_="devicesList") }}
<p class="enumeration-devices"></p>
</div>
{{ field }}
{% elif field == form_new_trade.lot %}
{{ field }}
{% elif field == form_new_trade.type %}