Merge pull request #444 from dod-ccpo/paginate-audit-log
Paginate audit log
This commit is contained in:
@@ -8,8 +8,9 @@ class AuditEventQuery(Query):
|
||||
model = AuditEvent
|
||||
|
||||
@classmethod
|
||||
def get_all(cls):
|
||||
return db.session.query(cls.model).order_by(cls.model.time_created.desc()).all()
|
||||
def get_all(cls, pagination_opts):
|
||||
query = db.session.query(cls.model).order_by(cls.model.time_created.desc())
|
||||
return cls.paginate(query, pagination_opts)
|
||||
|
||||
|
||||
class AuditLog(object):
|
||||
@@ -28,11 +29,11 @@ class AuditLog(object):
|
||||
return cls._log(resource=resource, action=action)
|
||||
|
||||
@classmethod
|
||||
def get_all_events(cls, user):
|
||||
def get_all_events(cls, user, pagination_opts=None):
|
||||
Authorization.check_atat_permission(
|
||||
user, Permissions.VIEW_AUDIT_LOG, "view audit log"
|
||||
)
|
||||
return AuditEventQuery.get_all()
|
||||
return AuditEventQuery.get_all(pagination_opts)
|
||||
|
||||
@classmethod
|
||||
def _resource_type(cls, resource):
|
||||
|
||||
@@ -5,6 +5,39 @@ from atst.domain.exceptions import NotFoundError
|
||||
from atst.database import db
|
||||
|
||||
|
||||
class Paginator(object):
|
||||
"""
|
||||
Uses the Flask-SQLAlchemy extension's pagination method to paginate
|
||||
a query set.
|
||||
|
||||
Also acts as a proxy object so that the results of the query set can be iterated
|
||||
over without needing to call `.items`.
|
||||
"""
|
||||
|
||||
def __init__(self, query_set):
|
||||
self.query_set = query_set
|
||||
|
||||
@classmethod
|
||||
def paginate(cls, query, pagination_opts=None):
|
||||
if pagination_opts is not None:
|
||||
return cls(
|
||||
query.paginate(
|
||||
page=pagination_opts["page"], per_page=pagination_opts["per_page"]
|
||||
)
|
||||
)
|
||||
else:
|
||||
return query.all()
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.query_set, name)
|
||||
|
||||
def __iter__(self):
|
||||
return self.items.__iter__()
|
||||
|
||||
def __len__(self):
|
||||
return self.items.__len__()
|
||||
|
||||
|
||||
class Query(object):
|
||||
|
||||
model = None
|
||||
@@ -35,3 +68,7 @@ class Query(object):
|
||||
db.session.add(resource)
|
||||
db.session.commit()
|
||||
return resource
|
||||
|
||||
@classmethod
|
||||
def paginate(cls, query, pagination_opts):
|
||||
return Paginator.paginate(query, pagination_opts)
|
||||
|
||||
Reference in New Issue
Block a user