add EditTransfer

This commit is contained in:
Cayo Puigdefabregas 2022-05-30 16:33:01 +02:00
parent 9ca5ce48d1
commit b0c58595c1
2 changed files with 72 additions and 23 deletions

View File

@ -1104,41 +1104,54 @@ class TradeDocumentForm(FlaskForm):
class TransferForm(FlaskForm): class TransferForm(FlaskForm):
code = StringField( code = StringField(
'Code', 'Code',
[validators.Optional()], [validators.DataRequired()],
render_kw={'class': "form-control"}, render_kw={'class': "form-control"},
description="You need put a code for transfer the external user", 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 = TextAreaField(
'Description', 'Description',
[validators.Optional()],
render_kw={'class': "form-control"}, render_kw={'class': "form-control"},
) )
type = HiddenField() type = HiddenField()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
lot_id = kwargs.pop('lot_id') self._type = kwargs.get('type')
super().__init__(*args, **kwargs) lot_id = kwargs.pop('lot_id', None)
self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one() self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one()
super().__init__(*args, **kwargs)
self._obj = None
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
if not self._tmp_lot:
return False
if self.type.data not in ['incoming', 'outgoing']: if self._type and self.type.data not in ['incoming', 'outgoing']:
is_valid = False return False
if self._obj and self.date.data:
if self.date.data > datetime.datetime.now().date():
return False
return is_valid return is_valid
def save(self, commit=True): def save(self, commit=True):
self.set_obj()
db.session.add(self._obj)
if commit:
db.session.commit()
return self._obj
def set_obj(self):
self.newlot = Lot(name=self._tmp_lot.name) self.newlot = Lot(name=self._tmp_lot.name)
self.newlot.devices = self._tmp_lot.devices self.newlot.devices = self._tmp_lot.devices
db.session.add(self.newlot) db.session.add(self.newlot)
self._obj = Transfer(lot=self.newlot) self._obj = Transfer(lot=self.newlot)
self.populate_obj(self._obj) self.populate_obj(self._obj)
if self.type.data == 'incoming': if self.type.data == 'incoming':
@ -1146,9 +1159,25 @@ class TransferForm(FlaskForm):
elif self.type.data == 'outgoing': elif self.type.data == 'outgoing':
self._obj.user_from = g.user self._obj.user_from = g.user
db.session.add(self._obj)
if commit: class EditTransferForm(TransferForm):
db.session.commit() date = DateField(
'Date',
[validators.Optional()],
render_kw={'class': "form-control"},
description="""Date when the transfer is closed""",
)
return self._obj def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
del self.type
self._obj = self._tmp_lot.transfer
if not self.data['csrf_token']:
self.code.data = self._obj.code
self.description.data = self._obj.description
self.date.data = self._obj.date
def set_obj(self, commit=True):
self.populate_obj(self._obj)

View File

@ -15,6 +15,7 @@ from ereuse_devicehub.db import db
from ereuse_devicehub.inventory.forms import ( from ereuse_devicehub.inventory.forms import (
AllocateForm, AllocateForm,
DataWipeForm, DataWipeForm,
EditTransferForm,
FilterForm, FilterForm,
LotForm, LotForm,
NewActionForm, NewActionForm,
@ -48,16 +49,12 @@ class DeviceListMix(GenericMixView):
form_filter = FilterForm(lots, lot_id, only_unassigned=only_unassigned) form_filter = FilterForm(lots, lot_id, only_unassigned=only_unassigned)
devices = form_filter.search() devices = form_filter.search()
lot = None lot = None
form_transfer = ''
if lot_id: if lot_id:
lot = lots.filter(Lot.id == lot_id).one() lot = lots.filter(Lot.id == lot_id).one()
form_new_trade = TradeForm( if not lot.is_temporary and lot.transfer:
lot=lot.id, form_transfer = EditTransferForm(lot_id=lot.id, id=lot.transfer.id)
user_to=g.user.email,
user_from=g.user.email,
)
else:
form_new_trade = ''
form_new_action = NewActionForm(lot=lot_id) form_new_action = NewActionForm(lot=lot_id)
self.context.update( self.context.update(
@ -67,7 +64,7 @@ class DeviceListMix(GenericMixView):
'form_new_action': form_new_action, 'form_new_action': form_new_action,
'form_new_allocate': AllocateForm(lot=lot_id), 'form_new_allocate': AllocateForm(lot=lot_id),
'form_new_datawipe': DataWipeForm(lot=lot_id), 'form_new_datawipe': DataWipeForm(lot=lot_id),
'form_new_trade': form_new_trade, 'form_transfer': form_transfer,
'form_filter': form_filter, 'form_filter': form_filter,
'form_print_labels': PrintLabelsForm(), 'form_print_labels': PrintLabelsForm(),
'lot': lot, 'lot': lot,
@ -416,6 +413,7 @@ class NewTransferView(GenericMixView):
new_lot_id = lot_id new_lot_id = lot_id
if self.form.newlot.id: if self.form.newlot.id:
new_lot_id = self.form.newlot.id new_lot_id = self.form.newlot.id
Lot.query.filter(Lot.id == new_lot_id).one()
messages.success('Transfer created successfully!') messages.success('Transfer created successfully!')
next_url = url_for('inventory.lotdevicelist', lot_id=new_lot_id) next_url = url_for('inventory.lotdevicelist', lot_id=new_lot_id)
return flask.redirect(next_url) return flask.redirect(next_url)
@ -424,6 +422,24 @@ class NewTransferView(GenericMixView):
return flask.render_template(self.template_name, **self.context) return flask.render_template(self.template_name, **self.context)
class EditTransferView(GenericMixView):
methods = ['POST']
form_class = EditTransferForm
def dispatch_request(self, lot_id):
self.get_context()
form = self.form_class(request.form, lot_id=lot_id)
next_url = url_for('inventory.lotdevicelist', lot_id=lot_id)
if form.validate_on_submit():
form.save()
messages.success('Transfer updated successfully!')
return flask.redirect(next_url)
messages.error('Transfer updated error!')
return flask.redirect(next_url)
class ExportsView(View): class ExportsView(View):
methods = ['GET'] methods = ['GET']
decorators = [login_required] decorators = [login_required]
@ -585,3 +601,7 @@ devices.add_url_rule(
'/lot/<string:lot_id>/transfer/<string:type_id>/', '/lot/<string:lot_id>/transfer/<string:type_id>/',
view_func=NewTransferView.as_view('new_transfer'), view_func=NewTransferView.as_view('new_transfer'),
) )
devices.add_url_rule(
'/lot/<string:lot_id>/transfer/',
view_func=EditTransferView.as_view('edit_transfer'),
)