view for create an edit the a lot

This commit is contained in:
Cayo Puigdefabregas 2021-12-30 21:35:54 +01:00
parent a0ef40328a
commit d3dc5c8986
3 changed files with 61 additions and 13 deletions

View File

@ -1,7 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import StringField, validators
from flask_login import current_user
from flask import g
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.lot.models import Lot
@ -27,3 +29,36 @@ class LotDeviceAddForm(FlaskForm):
return False
return True
def save(self):
self.lot.devices.update(self.devices)
g.user = current_user
db.session.add(self.lot)
db.session.commit()
class LotForm(FlaskForm):
name = StringField(u'Name', [validators.length(min=1)])
def __init__(self, *args, **kwargs):
id = kwargs.pop('id', None)
self.lot = None
if id:
self.lot = Lot.query.filter(Lot.id == id).filter(
Lot.owner_id == current_user.id).one()
super().__init__(*args, **kwargs)
if self.lot and not self.name.data:
self.name.data = self.lot.name
def save(self):
name = self.name.data.strip()
if self.lot:
if self.lot.name == name:
return
self.lot.name = name
else:
self.lot = Lot(name=name)
g.user = current_user
db.session.add(self.lot)
db.session.commit()

View File

@ -1,12 +1,11 @@
import flask
from flask.views import View
from flask import Blueprint, url_for, g
from flask import Blueprint, url_for
from flask_login import login_required, current_user
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.inventory.forms import LotDeviceAddForm
from ereuse_devicehub.inventory.forms import LotDeviceAddForm, LotForm
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
@ -34,7 +33,6 @@ class LotDeviceListView(View):
def dispatch_request(self, id):
# TODO @cayop adding filter
# import pdb; pdb.set_trace()
lots = Lot.query.filter(Lot.owner_id == current_user.id)
lot = lots.filter(Lot.id == id).one()
filter_types = ['Desktop', 'Laptop', 'Server']
@ -55,17 +53,32 @@ class LotDeviceAddView(View):
def dispatch_request(self):
form = LotDeviceAddForm()
if form.validate_on_submit():
lot = form.lot
devices = form.devices
lot.devices.update(devices)
g.user = current_user
db.session.add(lot)
db.session().final_flush()
form.save()
next_url = url_for('inventory.devices.lotdevicelist', id=lot.id)
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.id)
return flask.redirect(next_url)
class LotView(View):
methods = ['GET', 'POST']
decorators = [login_required]
template_name = 'inventory/lot.html'
title = "Add a new lot"
def dispatch_request(self, id=None):
if id:
self.title = "Edit lot"
form = LotForm(id=id)
if form.validate_on_submit():
form.save()
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.id)
return flask.redirect(next_url)
return flask.render_template(self.template_name, form=form, title=self.title)
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
devices.add_url_rule('/lot/<string:id>/device/', view_func=LotDeviceListView.as_view('lotdevicelist'))
devices.add_url_rule('/lot/devices/add', view_func=LotDeviceAddView.as_view('lot_devices_add'))
devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add'))
devices.add_url_rule('/lot/add', view_func=LotView.as_view('lot_add'))
devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit'))

View File

@ -290,7 +290,7 @@
</a>
<ul id="temporal-lots-nav" class="nav-content collapse " data-bs-parent="#sidebar-nav">
<li>
<a href="#TODO">
<a href="{{ url_for('inventory.devices.lot_add')}}">
<i class="bi bi-plus" style="font-size: larger;"></i><span>New temporal lot</span>
</a>
</li>