This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/inventory/views.py

53 lines
1.8 KiB
Python
Raw Normal View History

2021-12-28 11:36:02 +00:00
import flask
from flask.views import View
2021-12-30 12:53:28 +00:00
from flask import Blueprint, url_for, g
2021-12-28 12:55:56 +00:00
from flask_login import login_required, current_user
2021-12-28 11:36:02 +00:00
2021-12-30 12:53:28 +00:00
from ereuse_devicehub.db import db
2021-12-29 12:44:28 +00:00
from ereuse_devicehub.resources.lot.models import Lot
2021-12-30 12:53:28 +00:00
from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.inventory.forms import LotDeviceAddForm
2021-12-28 11:36:02 +00:00
2021-12-28 12:55:56 +00:00
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
2021-12-28 11:36:02 +00:00
class DeviceListView(View):
2021-12-28 12:55:56 +00:00
decorators = [login_required]
2021-12-28 11:36:02 +00:00
template_name = 'inventory/device_list.html'
def dispatch_request(self):
2021-12-29 09:13:34 +00:00
# TODO @cayop adding filter
2021-12-28 12:55:56 +00:00
filter_types = ['Desktop', 'Laptop', 'Server']
devices = Device.query.filter(
2021-12-29 09:13:34 +00:00
Device.owner_id == current_user.id).filter(
2021-12-28 12:55:56 +00:00
Device.type.in_(filter_types))
2021-12-29 12:44:28 +00:00
lots = Lot.query.filter(Lot.owner_id == current_user.id)
2021-12-30 12:53:28 +00:00
context = {'devices': devices, 'lots': lots, 'form_lot_device': LotDeviceAddForm()}
2021-12-28 11:36:02 +00:00
return flask.render_template(self.template_name, **context)
2021-12-30 12:53:28 +00:00
class LotDeviceAddView(View):
methods = ['POST']
decorators = [login_required]
template_name = 'inventory/device_list.html'
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()
next_url = url_for('inventory.devices.devicelist')
# next_url = url_for('inventory.devices.lot')
return flask.redirect(next_url)
2021-12-29 09:13:34 +00:00
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
2021-12-30 12:53:28 +00:00
devices.add_url_rule('/lot/devices/add', view_func=LotDeviceAddView.as_view('lot_devices_add'))