From 83bdf795147ad66606a9c08206cb93d092181981 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 6 Jun 2022 13:37:58 +0200 Subject: [PATCH] add render views for delivery and receiver notes --- ereuse_devicehub/inventory/forms.py | 53 +++++++++++++++++++ ereuse_devicehub/inventory/views.py | 26 ++++++--- .../templates/inventory/device_list.html | 11 +++- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 33247728..5c671eb7 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1181,6 +1181,14 @@ class EditTransferForm(TransferForm): self.description.data = self._obj.description self.date.data = self._obj.date + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + date = self.date.data + if date and date > datetime.datetime.now().date(): + self.date.errors = ["You have to choose a date before today."] + is_valid = False + return is_valid + def set_obj(self, commit=True): self.populate_obj(self._obj) @@ -1239,13 +1247,58 @@ class NotesForm(FlaskForm): """You can put a number for tracer of receiber note.""" ) + if self.is_editable(): + self.number.render_kw.pop('disabled', None) + self.date.render_kw.pop('disabled', None) + self.units.render_kw.pop('disabled', None) + self.weight.render_kw.pop('disabled', None) + else: + disabled = {'disabled': "disabled"} + self.number.render_kw.update(disabled) + self.date.render_kw.update(disabled) + self.units.render_kw.update(disabled) + self.weight.render_kw.update(disabled) + if self._obj and not self.data['csrf_token']: self.number.data = self._obj.number self.date.data = self._obj.date self.units.data = self._obj.units self.weight.data = self._obj.weight + def is_editable(self): + if not self._tmp_lot.transfer: + return False + + if self._tmp_lot.transfer.closed: + return False + + if self._tmp_lot.transfer.code: + return True + + if self._tmp_lot.transfer.user_from == g.user and self.type == 'Receiver': + return False + + if self._tmp_lot.transfer.user_to == g.user and self.type == 'Delivery': + return False + + return True + + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + date = self.date.data + if date and date > datetime.datetime.now().date(): + self.date.errors = ["You have to choose a date before today."] + is_valid = False + + if not self.is_editable(): + is_valid = False + + return is_valid + def save(self, commit=True): + if self._tmp_lot.transfer.closed: + return self._obj + self.populate_obj(self._obj) db.session.add(self._obj) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 4569a13a..b7a8298b 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -53,6 +53,8 @@ class DeviceListMixin(GenericMixin): devices = form_filter.search() lot = None form_transfer = '' + form_delivery = '' + form_receiver = '' if lot_id: lot = lots.filter(Lot.id == lot_id).one() @@ -458,6 +460,10 @@ class EditTransferView(GenericMixin): return flask.redirect(next_url) messages.error('Transfer updated error!') + for k, v in form.errors.items(): + value = ';'.join(v) + key = form[k].label.text + messages.error('Error {key}: {value}!'.format(key=key, value=value)) return flask.redirect(next_url) @@ -647,10 +653,14 @@ class DeliveryNoteView(GenericMixin): if form.validate_on_submit(): form.save() - messages.success('Transfer updated successfully!') + messages.success('Delivery Note updated successfully!') return flask.redirect(next_url) - messages.error('Transfer updated error!') + messages.error('Delivery Note updated error!') + for k, v in form.errors.items(): + value = ';'.join(v) + key = form[k].label.text + messages.error('Error {key}: {value}!'.format(key=key, value=value)) return flask.redirect(next_url) @@ -665,10 +675,14 @@ class ReceiverNoteView(GenericMixin): if form.validate_on_submit(): form.save() - messages.success('Transfer updated successfully!') + messages.success('Receiver Note updated successfully!') return flask.redirect(next_url) - messages.error('Transfer updated error!') + messages.error('Receiver Note updated error!') + for k, v in form.errors.items(): + value = ';'.join(v) + key = form[k].label.text + messages.error('Error {key}: {value}!'.format(key=key, value=value)) return flask.redirect(next_url) @@ -736,9 +750,9 @@ devices.add_url_rule( ) devices.add_url_rule( '/lot//deliverynote/', - view_func=DeliveryNoteView.as_view('deliverynote'), + view_func=DeliveryNoteView.as_view('delivery_note'), ) devices.add_url_rule( '/lot//receivernote/', - view_func=ReceiverNoteView.as_view('receivernote'), + view_func=ReceiverNoteView.as_view('receiver_note'), ) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index d3f71ffe..38628a7c 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -490,6 +490,7 @@ * {% endif %} {{ field }} + {{ field.description }} {% if field.errors %}

{% for error in field.errors %} @@ -510,7 +511,7 @@

Delivery Note
-
+ {{ form_delivery.csrf_token }} {% for field in form_delivery %} @@ -519,6 +520,7 @@ {% if field != form_delivery.type %} {{ field.label(class_="form-label") }} {{ field }} + {{ field.description }} {% if field.errors %}

{% for error in field.errors %} @@ -531,15 +533,17 @@ {% endif %} {% endfor %} + {% if lot.transfer and form_receiver.is_editable() %}

Cancel
+ {% endif %}
Receiver Note
-
+ {{ form_receiver.csrf_token }} {% for field in form_receiver %} @@ -548,6 +552,7 @@ {% if field != form_receiver.type %} {{ field.label(class_="form-label") }} {{ field }} + {{ field.description }} {% if field.errors %}

{% for error in field.errors %} @@ -560,10 +565,12 @@ {% endif %} {% endfor %} + {% if lot.transfer and form_receiver.is_editable() %}

Cancel
+ {% endif %}
{% endif %}