mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
try handmade pagination
This commit is contained in:
@@ -6,6 +6,7 @@ from flask import current_app
|
|||||||
from sqlalchemy import and_, asc, desc, func, select
|
from sqlalchemy import and_, asc, desc, func, select
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
|
from app.dao.pagination import Pagination
|
||||||
from app.enums import JobStatus
|
from app.enums import JobStatus
|
||||||
from app.models import (
|
from app.models import (
|
||||||
FactNotificationStatus,
|
FactNotificationStatus,
|
||||||
@@ -65,10 +66,18 @@ def dao_get_jobs_by_service_id(
|
|||||||
if statuses is not None and statuses != [""]:
|
if statuses is not None and statuses != [""]:
|
||||||
query_filter.append(Job.job_status.in_(statuses))
|
query_filter.append(Job.job_status.in_(statuses))
|
||||||
|
|
||||||
stmt = select(*query_filter).order_by(
|
total_items = db.session.execute(
|
||||||
Job.processing_started.desc(),
|
select(func.count()).select_from(*query_filter).scalar_one()
|
||||||
Job.created_at.desc()).limit(page_size).offset(page)
|
)
|
||||||
return db.session.execute(stmt).scalars().all()
|
|
||||||
|
stmt = (
|
||||||
|
select(*query_filter)
|
||||||
|
.order_by(Job.processing_started.desc(), Job.created_at.desc())
|
||||||
|
.limit(page_size)
|
||||||
|
.offset(page)
|
||||||
|
)
|
||||||
|
items = db.session.execute(stmt).scalars().all()
|
||||||
|
return Pagination(items, page, page_size, total_items)
|
||||||
|
|
||||||
|
|
||||||
def dao_get_scheduled_job_stats(
|
def dao_get_scheduled_job_stats(
|
||||||
|
|||||||
13
app/dao/pagination.py
Normal file
13
app/dao/pagination.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
class Pagination:
|
||||||
|
def __init__(self, items, page, per_page, total):
|
||||||
|
self.items = items
|
||||||
|
self.page = page
|
||||||
|
self.per_page = per_page
|
||||||
|
self.total = total
|
||||||
|
self.pages = (total + per_page - 1) // per_page
|
||||||
|
|
||||||
|
def has_next(self):
|
||||||
|
return self.page < self.pages
|
||||||
|
|
||||||
|
def has_prev(self):
|
||||||
|
return self.page > 1
|
||||||
Reference in New Issue
Block a user