refactor common context
This commit is contained in:
parent
9f8f5ec9e0
commit
ad0ced98e4
|
@ -7,10 +7,9 @@ import flask_weasyprint
|
||||||
from flask import Blueprint, g, make_response, request, url_for
|
from flask import Blueprint, g, make_response, request, url_for
|
||||||
from flask.views import View
|
from flask.views import View
|
||||||
from flask_login import current_user, login_required
|
from flask_login import current_user, login_required
|
||||||
from sqlalchemy import or_
|
|
||||||
from werkzeug.exceptions import NotFound
|
from werkzeug.exceptions import NotFound
|
||||||
|
|
||||||
from ereuse_devicehub import __version__, messages
|
from ereuse_devicehub import messages
|
||||||
from ereuse_devicehub.db import db
|
from ereuse_devicehub.db import db
|
||||||
from ereuse_devicehub.inventory.forms import (
|
from ereuse_devicehub.inventory.forms import (
|
||||||
AllocateForm,
|
AllocateForm,
|
||||||
|
@ -31,35 +30,21 @@ from ereuse_devicehub.resources.documents.device_row import ActionRow, DeviceRow
|
||||||
from ereuse_devicehub.resources.hash_reports import insert_hash
|
from ereuse_devicehub.resources.hash_reports import insert_hash
|
||||||
from ereuse_devicehub.resources.lot.models import Lot
|
from ereuse_devicehub.resources.lot.models import Lot
|
||||||
from ereuse_devicehub.resources.tag.model import Tag
|
from ereuse_devicehub.resources.tag.model import Tag
|
||||||
|
from ereuse_devicehub.views import GenericMixView
|
||||||
|
|
||||||
devices = Blueprint('inventory', __name__, url_prefix='/inventory')
|
devices = Blueprint('inventory', __name__, url_prefix='/inventory')
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class GenericMixView(View):
|
|
||||||
def get_lots(self):
|
|
||||||
return (
|
|
||||||
Lot.query.outerjoin(Trade)
|
|
||||||
.filter(
|
|
||||||
or_(
|
|
||||||
Trade.user_from == g.user,
|
|
||||||
Trade.user_to == g.user,
|
|
||||||
Lot.owner_id == g.user.id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.distinct()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DeviceListMix(GenericMixView):
|
class DeviceListMix(GenericMixView):
|
||||||
decorators = [login_required]
|
|
||||||
template_name = 'inventory/device_list.html'
|
template_name = 'inventory/device_list.html'
|
||||||
|
|
||||||
def get_context(self, lot_id):
|
def get_context(self, lot_id):
|
||||||
|
super().get_context()
|
||||||
|
lots = self.context['lots']
|
||||||
form_filter = FilterForm()
|
form_filter = FilterForm()
|
||||||
filter_types = form_filter.search()
|
filter_types = form_filter.search()
|
||||||
lots = self.get_lots()
|
|
||||||
lot = None
|
lot = None
|
||||||
tags = (
|
tags = (
|
||||||
Tag.query.filter(Tag.owner_id == current_user.id)
|
Tag.query.filter(Tag.owner_id == current_user.id)
|
||||||
|
@ -105,21 +90,21 @@ class DeviceListMix(GenericMixView):
|
||||||
if action_devices:
|
if action_devices:
|
||||||
list_devices.extend([int(x) for x in action_devices.split(",")])
|
list_devices.extend([int(x) for x in action_devices.split(",")])
|
||||||
|
|
||||||
self.context = {
|
self.context.update(
|
||||||
'devices': devices,
|
{
|
||||||
'lots': lots,
|
'devices': devices,
|
||||||
'form_tag_device': TagDeviceForm(),
|
'form_tag_device': TagDeviceForm(),
|
||||||
'form_new_action': form_new_action,
|
'form_new_action': form_new_action,
|
||||||
'form_new_allocate': form_new_allocate,
|
'form_new_allocate': form_new_allocate,
|
||||||
'form_new_datawipe': form_new_datawipe,
|
'form_new_datawipe': form_new_datawipe,
|
||||||
'form_new_trade': form_new_trade,
|
'form_new_trade': form_new_trade,
|
||||||
'form_filter': form_filter,
|
'form_filter': form_filter,
|
||||||
'form_print_labels': PrintLabelsForm(),
|
'form_print_labels': PrintLabelsForm(),
|
||||||
'lot': lot,
|
'lot': lot,
|
||||||
'tags': tags,
|
'tags': tags,
|
||||||
'list_devices': list_devices,
|
'list_devices': list_devices,
|
||||||
'version': __version__,
|
}
|
||||||
}
|
)
|
||||||
|
|
||||||
return self.context
|
return self.context
|
||||||
|
|
||||||
|
@ -135,20 +120,20 @@ class DeviceDetailView(GenericMixView):
|
||||||
template_name = 'inventory/device_detail.html'
|
template_name = 'inventory/device_detail.html'
|
||||||
|
|
||||||
def dispatch_request(self, id):
|
def dispatch_request(self, id):
|
||||||
lots = self.get_lots()
|
self.get_context()
|
||||||
device = (
|
device = (
|
||||||
Device.query.filter(Device.owner_id == current_user.id)
|
Device.query.filter(Device.owner_id == current_user.id)
|
||||||
.filter(Device.devicehub_id == id)
|
.filter(Device.devicehub_id == id)
|
||||||
.one()
|
.one()
|
||||||
)
|
)
|
||||||
|
|
||||||
context = {
|
self.context.update(
|
||||||
'device': device,
|
{
|
||||||
'lots': lots,
|
'device': device,
|
||||||
'page_title': 'Device {}'.format(device.devicehub_id),
|
'page_title': 'Device {}'.format(device.devicehub_id),
|
||||||
'version': __version__,
|
}
|
||||||
}
|
)
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class LotCreateView(GenericMixView):
|
class LotCreateView(GenericMixView):
|
||||||
|
@ -164,14 +149,14 @@ class LotCreateView(GenericMixView):
|
||||||
next_url = url_for('inventory.lotdevicelist', lot_id=form.id)
|
next_url = url_for('inventory.lotdevicelist', lot_id=form.id)
|
||||||
return flask.redirect(next_url)
|
return flask.redirect(next_url)
|
||||||
|
|
||||||
lots = self.get_lots()
|
self.get_context()
|
||||||
context = {
|
self.context.update(
|
||||||
'form': form,
|
{
|
||||||
'title': self.title,
|
'form': form,
|
||||||
'lots': lots,
|
'title': self.title,
|
||||||
'version': __version__,
|
}
|
||||||
}
|
)
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class LotUpdateView(View):
|
class LotUpdateView(View):
|
||||||
|
@ -187,14 +172,14 @@ class LotUpdateView(View):
|
||||||
next_url = url_for('inventory.lotdevicelist', lot_id=id)
|
next_url = url_for('inventory.lotdevicelist', lot_id=id)
|
||||||
return flask.redirect(next_url)
|
return flask.redirect(next_url)
|
||||||
|
|
||||||
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
self.get_context()
|
||||||
context = {
|
self.context.update(
|
||||||
'form': form,
|
{
|
||||||
'title': self.title,
|
'form': form,
|
||||||
'lots': lots,
|
'title': self.title,
|
||||||
'version': __version__,
|
}
|
||||||
}
|
)
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class LotDeleteView(View):
|
class LotDeleteView(View):
|
||||||
|
@ -221,24 +206,25 @@ class UploadSnapshotView(GenericMixView):
|
||||||
template_name = 'inventory/upload_snapshot.html'
|
template_name = 'inventory/upload_snapshot.html'
|
||||||
|
|
||||||
def dispatch_request(self, lot_id=None):
|
def dispatch_request(self, lot_id=None):
|
||||||
lots = self.get_lots()
|
self.get_context()
|
||||||
form = UploadSnapshotForm()
|
form = UploadSnapshotForm()
|
||||||
context = {
|
self.context.update(
|
||||||
'page_title': 'Upload Snapshot',
|
{
|
||||||
'lots': lots,
|
'page_title': 'Upload Snapshot',
|
||||||
'form': form,
|
'form': form,
|
||||||
'lot_id': lot_id,
|
'lot_id': lot_id,
|
||||||
'version': __version__,
|
}
|
||||||
}
|
)
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
snapshot = form.save(commit=False)
|
snapshot = form.save(commit=False)
|
||||||
if lot_id:
|
if lot_id:
|
||||||
|
lots = self.context['lots']
|
||||||
lot = lots.filter(Lot.id == lot_id).one()
|
lot = lots.filter(Lot.id == lot_id).one()
|
||||||
lot.devices.add(snapshot.device)
|
lot.devices.add(snapshot.device)
|
||||||
db.session.add(lot)
|
db.session.add(lot)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class DeviceCreateView(GenericMixView):
|
class DeviceCreateView(GenericMixView):
|
||||||
|
@ -247,20 +233,21 @@ class DeviceCreateView(GenericMixView):
|
||||||
template_name = 'inventory/device_create.html'
|
template_name = 'inventory/device_create.html'
|
||||||
|
|
||||||
def dispatch_request(self, lot_id=None):
|
def dispatch_request(self, lot_id=None):
|
||||||
lots = self.get_lots()
|
self.get_context()
|
||||||
form = NewDeviceForm()
|
form = NewDeviceForm()
|
||||||
context = {
|
self.context.update(
|
||||||
'page_title': 'New Device',
|
{
|
||||||
'lots': lots,
|
'page_title': 'New Device',
|
||||||
'form': form,
|
'form': form,
|
||||||
'lot_id': lot_id,
|
'lot_id': lot_id,
|
||||||
'version': __version__,
|
}
|
||||||
}
|
)
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
snapshot = form.save(commit=False)
|
snapshot = form.save(commit=False)
|
||||||
next_url = url_for('inventory.devicelist')
|
next_url = url_for('inventory.devicelist')
|
||||||
if lot_id:
|
if lot_id:
|
||||||
next_url = url_for('inventory.lotdevicelist', lot_id=lot_id)
|
next_url = url_for('inventory.lotdevicelist', lot_id=lot_id)
|
||||||
|
lots = self.context['lots']
|
||||||
lot = lots.filter(Lot.id == lot_id).one()
|
lot = lots.filter(Lot.id == lot_id).one()
|
||||||
lot.devices.add(snapshot.device)
|
lot.devices.add(snapshot.device)
|
||||||
db.session.add(lot)
|
db.session.add(lot)
|
||||||
|
@ -269,7 +256,7 @@ class DeviceCreateView(GenericMixView):
|
||||||
messages.success('Device "{}" created successfully!'.format(form.type.data))
|
messages.success('Device "{}" created successfully!'.format(form.type.data))
|
||||||
return flask.redirect(next_url)
|
return flask.redirect(next_url)
|
||||||
|
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class TagLinkDeviceView(View):
|
class TagLinkDeviceView(View):
|
||||||
|
@ -285,13 +272,13 @@ class TagLinkDeviceView(View):
|
||||||
return flask.redirect(request.referrer)
|
return flask.redirect(request.referrer)
|
||||||
|
|
||||||
|
|
||||||
class TagUnlinkDeviceView(View):
|
class TagUnlinkDeviceView(GenericMixView):
|
||||||
methods = ['POST', 'GET']
|
methods = ['POST', 'GET']
|
||||||
decorators = [login_required]
|
decorators = [login_required]
|
||||||
template_name = 'inventory/tag_unlink_device.html'
|
template_name = 'inventory/tag_unlink_device.html'
|
||||||
|
|
||||||
def dispatch_request(self, id):
|
def dispatch_request(self, id):
|
||||||
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
self.get_context()
|
||||||
form = TagDeviceForm(delete=True, device=id)
|
form = TagDeviceForm(delete=True, device=id)
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
form.remove()
|
form.remove()
|
||||||
|
@ -299,14 +286,15 @@ class TagUnlinkDeviceView(View):
|
||||||
next_url = url_for('inventory.devicelist')
|
next_url = url_for('inventory.devicelist')
|
||||||
return flask.redirect(next_url)
|
return flask.redirect(next_url)
|
||||||
|
|
||||||
return flask.render_template(
|
self.context.update(
|
||||||
self.template_name,
|
{
|
||||||
form=form,
|
'form': form,
|
||||||
lots=lots,
|
'referrer': request.referrer,
|
||||||
referrer=request.referrer,
|
}
|
||||||
version=__version__,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class NewActionView(View):
|
class NewActionView(View):
|
||||||
methods = ['POST']
|
methods = ['POST']
|
||||||
|
@ -409,6 +397,7 @@ class NewTradeDocumentView(View):
|
||||||
|
|
||||||
def dispatch_request(self, lot_id):
|
def dispatch_request(self, lot_id):
|
||||||
self.form = self.form_class(lot=lot_id)
|
self.form = self.form_class(lot=lot_id)
|
||||||
|
self.get_context()
|
||||||
|
|
||||||
if self.form.validate_on_submit():
|
if self.form.validate_on_submit():
|
||||||
self.form.save()
|
self.form.save()
|
||||||
|
@ -416,9 +405,8 @@ class NewTradeDocumentView(View):
|
||||||
next_url = url_for('inventory.lotdevicelist', lot_id=lot_id)
|
next_url = url_for('inventory.lotdevicelist', lot_id=lot_id)
|
||||||
return flask.redirect(next_url)
|
return flask.redirect(next_url)
|
||||||
|
|
||||||
return flask.render_template(
|
self.context.update({'form': self.form, 'title': self.title})
|
||||||
self.template_name, form=self.form, title=self.title, version=__version__
|
return flask.render_template(self.template_name, **self.context)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ExportsView(View):
|
class ExportsView(View):
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
import flask
|
import flask
|
||||||
from flask import Blueprint
|
from flask import Blueprint, g
|
||||||
from flask.views import View
|
from flask.views import View
|
||||||
from flask_login import current_user, login_required, login_user, logout_user
|
from flask_login import current_user, login_required, login_user, logout_user
|
||||||
|
from sqlalchemy import or_
|
||||||
|
|
||||||
from ereuse_devicehub import __version__, messages
|
from ereuse_devicehub import __version__, messages
|
||||||
from ereuse_devicehub.db import db
|
from ereuse_devicehub.db import db
|
||||||
from ereuse_devicehub.forms import LoginForm, PasswordForm
|
from ereuse_devicehub.forms import LoginForm, PasswordForm
|
||||||
|
from ereuse_devicehub.resources.action.models import Trade
|
||||||
|
from ereuse_devicehub.resources.lot.models import Lot
|
||||||
from ereuse_devicehub.resources.user.models import User
|
from ereuse_devicehub.resources.user.models import User
|
||||||
from ereuse_devicehub.utils import is_safe_url
|
from ereuse_devicehub.utils import is_safe_url
|
||||||
|
|
||||||
|
@ -46,18 +49,45 @@ class LogoutView(View):
|
||||||
return flask.redirect(flask.url_for('core.login'))
|
return flask.redirect(flask.url_for('core.login'))
|
||||||
|
|
||||||
|
|
||||||
class UserProfileView(View):
|
class GenericMixView(View):
|
||||||
|
decorators = [login_required]
|
||||||
|
|
||||||
|
def get_lots(self):
|
||||||
|
return (
|
||||||
|
Lot.query.outerjoin(Trade)
|
||||||
|
.filter(
|
||||||
|
or_(
|
||||||
|
Trade.user_from == g.user,
|
||||||
|
Trade.user_to == g.user,
|
||||||
|
Lot.owner_id == g.user.id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.distinct()
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_context(self):
|
||||||
|
self.context = {
|
||||||
|
'lots': self.get_lots(),
|
||||||
|
'version': __version__,
|
||||||
|
}
|
||||||
|
|
||||||
|
return self.context
|
||||||
|
|
||||||
|
|
||||||
|
class UserProfileView(GenericMixView):
|
||||||
decorators = [login_required]
|
decorators = [login_required]
|
||||||
template_name = 'ereuse_devicehub/user_profile.html'
|
template_name = 'ereuse_devicehub/user_profile.html'
|
||||||
|
|
||||||
def dispatch_request(self):
|
def dispatch_request(self):
|
||||||
context = {
|
self.get_context()
|
||||||
'current_user': current_user,
|
self.context.update(
|
||||||
'version': __version__,
|
{
|
||||||
'password_form': PasswordForm(),
|
'current_user': current_user,
|
||||||
}
|
'password_form': PasswordForm(),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return flask.render_template(self.template_name, **context)
|
return flask.render_template(self.template_name, **self.context)
|
||||||
|
|
||||||
|
|
||||||
class UserPasswordView(View):
|
class UserPasswordView(View):
|
||||||
|
|
Reference in New Issue