mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-20 14:29:25 -04:00
Added page_size parameter for notifications api. All tests passing.
Add page_size and total parameters to all calls for notifications.
This commit is contained in:
@@ -198,12 +198,14 @@ def get_notification_for_job(service_id, job_id, notification_id):
|
||||
return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one()
|
||||
|
||||
|
||||
def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1):
|
||||
def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1, page_size=None):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
query = Notification.query.filter_by(service_id=service_id, job_id=job_id)
|
||||
query = filter_query(query, filter_dict)
|
||||
pagination = query.order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
per_page=page_size
|
||||
)
|
||||
return pagination
|
||||
|
||||
@@ -216,12 +218,14 @@ def get_notification_by_id(notification_id):
|
||||
return Notification.query.filter_by(id=notification_id).first()
|
||||
|
||||
|
||||
def get_notifications_for_service(service_id, filter_dict=None, page=1):
|
||||
def get_notifications_for_service(service_id, filter_dict=None, page=1, page_size=None):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
query = Notification.query.filter_by(service_id=service_id)
|
||||
query = filter_query(query, filter_dict)
|
||||
pagination = query.order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
per_page=page_size
|
||||
)
|
||||
return pagination
|
||||
|
||||
|
||||
@@ -190,14 +190,17 @@ def get_all_notifications():
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
page = data['page'] if 'page' in data else 1
|
||||
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
||||
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
api_user['client'],
|
||||
filter_dict=data,
|
||||
page=page)
|
||||
|
||||
page=page,
|
||||
page_size=page_size)
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
||||
page_size=page_size,
|
||||
total=pagination.total,
|
||||
links=pagination_links(
|
||||
pagination,
|
||||
'.get_all_notifications',
|
||||
@@ -214,15 +217,19 @@ def get_all_notifications_for_service(service_id):
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
page = data['page'] if 'page' in data else 1
|
||||
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
||||
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
service_id,
|
||||
filter_dict=data,
|
||||
page=page)
|
||||
page=page,
|
||||
page_size=page_size)
|
||||
kwargs = request.args.to_dict()
|
||||
kwargs['service_id'] = service_id
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
||||
page_size=page_size,
|
||||
total=pagination.total,
|
||||
links=pagination_links(
|
||||
pagination,
|
||||
'.get_all_notifications_for_service',
|
||||
@@ -239,17 +246,21 @@ def get_all_notifications_for_service_job(service_id, job_id):
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
page = data['page'] if 'page' in data else 1
|
||||
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
||||
|
||||
pagination = notifications_dao.get_notifications_for_job(
|
||||
service_id,
|
||||
job_id,
|
||||
filter_dict=data,
|
||||
page=page)
|
||||
page=page,
|
||||
page_size=page_size)
|
||||
kwargs = request.args.to_dict()
|
||||
kwargs['service_id'] = service_id
|
||||
kwargs['job_id'] = job_id
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
||||
page_size=page_size,
|
||||
total=pagination.total,
|
||||
links=pagination_links(
|
||||
pagination,
|
||||
'.get_all_notifications_for_service_job',
|
||||
|
||||
@@ -234,6 +234,7 @@ class NotificationsFilterSchema(ma.Schema):
|
||||
template_type = fields.Nested(BaseTemplateSchema, only=['template_type'], many=True)
|
||||
status = fields.Nested(NotificationModelSchema, only=['status'], many=True)
|
||||
page = fields.Int(required=False)
|
||||
page_size = fields.Int(required=False)
|
||||
|
||||
@pre_load
|
||||
def handle_multidict(self, in_data):
|
||||
@@ -254,8 +255,7 @@ class NotificationsFilterSchema(ma.Schema):
|
||||
in_data['status'] = [x.status for x in in_data['status']]
|
||||
return in_data
|
||||
|
||||
@validates('page')
|
||||
def validate_page(self, value):
|
||||
def _validate_positive_number(self, value):
|
||||
try:
|
||||
page_int = int(value)
|
||||
if page_int < 1:
|
||||
@@ -263,6 +263,14 @@ class NotificationsFilterSchema(ma.Schema):
|
||||
except:
|
||||
raise ValidationError("Not a positive integer")
|
||||
|
||||
@validates('page')
|
||||
def validate_page(self, value):
|
||||
self._validate_positive_number(value)
|
||||
|
||||
@validates('page_size')
|
||||
def validate_page_size(self, value):
|
||||
self._validate_positive_number(value)
|
||||
|
||||
|
||||
class TemplateStatisticsSchema(BaseSchema):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user