From ade9fbd018760b554ee3e807c3dc5db7d31bde5a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 23 Nov 2022 19:22:26 +0100 Subject: [PATCH] add raw query --- ereuse_devicehub/inventory/views.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 56eaab1d..20b0663b 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -118,19 +118,33 @@ class ErasureListView(DeviceListMixin): def dispatch_request(self, orphans=0): self.get_context() self.get_devices(orphans) - if orphans: - self.context['orphans'] = True return flask.render_template(self.template_name, **self.context) def get_devices(self, orphans): + page = int(request.args.get('page', 1)) + per_page = int(request.args.get('per_page', 5)) + erasure = EraseBasic.query.filter_by(author=g.user).order_by( EraseBasic.created.desc() ) if orphans: - erasure = [e for e in erasure if e.device.orphan] - - page = int(request.args.get('page', 1)) - per_page = int(request.args.get('per_page', 5)) + schema = app.config.get('SCHEMA') + sql = f""" + select action.id from {schema}.action as action + inner join {schema}.erase_basic as erase + on action.id=erase.id + inner join {schema}.device as device + on device.id=action.parent_id + inner join {schema}.placeholder as placeholder + on placeholder.binding_id=device.id + where action.parent_id is null or placeholder.kangaroo=true + """ + ids = (e[0] for e in db.session.execute(sql)) + erasure = EraseBasic.query.filter(EraseBasic.id.in_(ids)).order_by( + EraseBasic.created.desc() + ) + self.context['orphans'] = True + # import pdb; pdb.set_trace() erasure = erasure.paginate(page=page, per_page=per_page) erasure.first = per_page * erasure.page - per_page + 1