add html and form for transfers

This commit is contained in:
Cayo Puigdefabregas 2022-05-27 16:32:22 +02:00
parent 0998adc956
commit a8368e92b8
4 changed files with 166 additions and 0 deletions

View file

@ -26,6 +26,7 @@ from wtforms import (
from wtforms.fields import FormField
from ereuse_devicehub.db import db
from ereuse_devicehub.inventory.models import Transfer
from ereuse_devicehub.resources.action.models import RateComputer, Snapshot, Trade
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
@ -1098,3 +1099,56 @@ class TradeDocumentForm(FlaskForm):
db.session.commit()
return self._obj
class TransferForm(FlaskForm):
code = StringField(
'Code',
[validators.Optional()],
render_kw={'class': "form-control"},
description="You need put a code for transfer the external user",
)
date = DateField(
'Date',
[validators.Optional()],
render_kw={'class': "form-control"},
description="""Date when the transfer is closed""",
)
description = TextAreaField(
'Description',
render_kw={'class': "form-control"},
)
type = HiddenField()
def __init__(self, *args, **kwargs):
lot_id = kwargs.pop('lot_id')
super().__init__(*args, **kwargs)
self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one()
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if self.type.data not in ['incoming', 'outgoing']:
is_valid = False
return is_valid
def save(self, commit=True):
self.newlot = Lot(name=self._tmp_lot.name)
self.newlot.devices = self._tmp_lot.devices
db.session.add(self.newlot)
self._obj = Transfer(lot=self.newlot)
self.populate_obj(self._obj)
if self.type.data == 'incoming':
self._obj.user_to = g.user
elif self.type.data == 'outgoing':
self._obj.user_from = g.user
db.session.add(self._obj)
if commit:
db.session.commit()
return self._obj

View file

@ -22,6 +22,7 @@ from ereuse_devicehub.inventory.forms import (
TagDeviceForm,
TradeDocumentForm,
TradeForm,
TransferForm,
UploadSnapshotForm,
)
from ereuse_devicehub.labels.forms import PrintLabelsForm
@ -400,6 +401,29 @@ class NewTradeDocumentView(View):
return flask.render_template(self.template_name, **self.context)
class NewTransferView(GenericMixView):
methods = ['POST', 'GET']
template_name = 'inventory/new_transfer.html'
form_class = TransferForm
title = "Add new transfer"
def dispatch_request(self, lot_id, type_id):
self.form = self.form_class(lot_id=lot_id, type=type_id)
self.get_context()
if self.form.validate_on_submit():
self.form.save()
new_lot_id = lot_id
if self.form.newlot.id:
new_lot_id = self.form.newlot.id
messages.success('Transfer created successfully!')
next_url = url_for('inventory.lotdevicelist', lot_id=new_lot_id)
return flask.redirect(next_url)
self.context.update({'form': self.form, 'title': self.title})
return flask.render_template(self.template_name, **self.context)
class ExportsView(View):
methods = ['GET']
decorators = [login_required]
@ -557,3 +581,7 @@ devices.add_url_rule(
devices.add_url_rule(
'/export/<string:export_id>/', view_func=ExportsView.as_view('export')
)
devices.add_url_rule(
'/lot/<string:lot_id>/transfer/<string:type_id>/',
view_func=NewTransferView.as_view('new_transfer'),
)

View file

@ -78,6 +78,17 @@
</ul>
{% endif %}
<div class="tab-content pt-1">
{% if lot and lot.is_temporary %}
<div class="tab-pane active show mb-5">
<a type="button" href="{{ url_for('inventory.new_transfer', lot_id=lot.id, type_id='outgoing') }}" class="btn btn-primary" style="float: right;">
Outgoing Transfer
</a>
<a type="button" href="{{ url_for('inventory.new_transfer', lot_id=lot.id, type_id='incoming') }}" class="btn btn-primary" style="float: right; margin-right: 15px;">
Incoming Transfer
</a>
<div style="display: block;"></div>
</div>
{% endif %}
<div id="devices-list" class="tab-pane fade devices-list active show">
<label class="btn btn-primary " for="SelectAllBTN"><input type="checkbox" id="SelectAllBTN" autocomplete="off"></label>
<div class="btn-group dropdown ml-1">

View file

@ -0,0 +1,73 @@
{% extends "ereuse_devicehub/base_site.html" %}
{% block main %}
<div class="pagetitle">
<h1>{{ title }}</h1>
<nav>
<ol class="breadcrumb">
<!-- TODO@slamora replace with lot list URL when exists -->
<li class="breadcrumb-item"><a href="#TODO-lot-list">Lots</a></li>
<li class="breadcrumb-item">Transfer</li>
</ol>
</nav>
</div><!-- End Page Title -->
<section class="section profile">
<div class="row">
<div class="col-xl-4">
<div class="card">
<div class="card-body">
<div class="pt-4 pb-2">
<h5 class="card-title text-center pb-0 fs-4">{{ title }}</h5>
{% if form.form_errors %}
<p class="text-danger">
{% for error in form.form_errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
<form method="post" class="row g-3 needs-validation" novalidate>
{{ form.csrf_token }}
{% for field in form %}
{% if field != form.csrf_token %}
<div class="col-12">
{% if field != form.type %}
{{ field.label(class_="form-label") }}
{% if field == form.code %}
<span class="text-danger">*</span>
{% endif %}
{{ field }}
{% if field.errors %}
<p class="text-danger">
{% for error in field.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
{% endif %}
</div>
{% endif %}
{% endfor %}
<div>
<a href="{{ url_for('inventory.lotdevicelist', lot_id=form._tmp_lot.id) }}" class="btn btn-danger">Cancel</a>
<button class="btn btn-primary" type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-xl-8">
</div>
</div>
</section>
{% endblock main %}