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