add raw query

This commit is contained in:
Cayo Puigdefabregas 2022-11-23 19:22:26 +01:00
parent 1efc650bea
commit ade9fbd018
1 changed files with 20 additions and 6 deletions

View File

@ -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