mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Merge with master.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
from flask import request, jsonify, _request_ctx_stack, current_app
|
||||
from notifications_python_client.authentication import decode_jwt_token, get_token_issuer
|
||||
from notifications_python_client.errors import TokenDecodeError, TokenRequestError, TokenExpiredError, TokenPayloadError
|
||||
from werkzeug.exceptions import abort
|
||||
from app.dao.api_key_dao import get_unsigned_secrets
|
||||
from app import api_user
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def authentication_response(message, code):
|
||||
@@ -68,3 +71,14 @@ def fetch_client(client):
|
||||
"client": client,
|
||||
"secret": get_unsigned_secrets(client)
|
||||
}
|
||||
|
||||
|
||||
def require_admin():
|
||||
def wrap(func):
|
||||
@wraps(func)
|
||||
def wrap_func(*args, **kwargs):
|
||||
if not api_user['client'] == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
abort(403)
|
||||
return func(*args, **kwargs)
|
||||
return wrap_func
|
||||
return wrap
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from flask import current_app
|
||||
from app import db
|
||||
from app.models import Notification
|
||||
from sqlalchemy import desc
|
||||
|
||||
|
||||
def dao_create_notification(notification):
|
||||
@@ -16,9 +18,23 @@ 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):
|
||||
return Notification.query.filter_by(service_id=service_id, job_id=job_id).all()
|
||||
def get_notifications_for_job(service_id, job_id, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id, job_id=job_id)\
|
||||
.order_by(desc(Notification.created_at))\
|
||||
.paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
)
|
||||
return query
|
||||
|
||||
|
||||
def get_notification(service_id, notification_id):
|
||||
return Notification.query.filter_by(service_id=service_id, id=notification_id).one()
|
||||
|
||||
|
||||
def get_notifications_for_service(service_id, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id).order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
)
|
||||
return query
|
||||
|
||||
@@ -18,7 +18,6 @@ default_service_permissions = [
|
||||
MANAGE_API_KEYS,
|
||||
MANAGE_TEMPLATES]
|
||||
|
||||
|
||||
class PermissionDAO(DAOClass):
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -42,14 +42,33 @@ def get_invited_users_by_service(service_id):
|
||||
|
||||
@invite.route('/<invited_user_id>', methods=['GET'])
|
||||
def get_invited_user_by_service_and_id(service_id, invited_user_id):
|
||||
invited_user = get_invited_user(service_id, invited_user_id)
|
||||
invited_user = get_invited_user(service_id=service_id, invited_user_id=invited_user_id)
|
||||
if not invited_user:
|
||||
message = 'Invited user not found for service id: {} and invited user id: {}'.format(service_id,
|
||||
invited_user_id)
|
||||
return jsonify(result='error', message=message), 404
|
||||
return _invited_user_not_found(service_id, invited_user_id)
|
||||
return jsonify(data=invited_user_schema.dump(invited_user).data), 200
|
||||
|
||||
|
||||
@invite.route('/<invited_user_id>', methods=['POST'])
|
||||
def update_invited_user(service_id, invited_user_id):
|
||||
fetched = get_invited_user(service_id=service_id, invited_user_id=invited_user_id)
|
||||
if not fetched:
|
||||
return _invited_user_not_found(service_id=service_id, invited_user_id=invited_user_id)
|
||||
|
||||
current_data = dict(invited_user_schema.dump(fetched).data.items())
|
||||
current_data.update(request.get_json())
|
||||
update_dict, errors = invited_user_schema.load(current_data)
|
||||
if errors:
|
||||
return jsonify(result='error', message=errors), 400
|
||||
save_invited_user(update_dict)
|
||||
return jsonify(data=invited_user_schema.dump(fetched).data), 200
|
||||
|
||||
|
||||
def _invited_user_not_found(service_id, invited_user_id):
|
||||
message = 'Invited user not found for service id: {} and invited user id: {}'.format(service_id,
|
||||
invited_user_id)
|
||||
return jsonify(result='error', message=message), 404
|
||||
|
||||
|
||||
def _create_invitation(invited_user):
|
||||
from utils.url_safe_token import generate_token
|
||||
token = generate_token(str(invited_user.id), current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
||||
|
||||
@@ -277,13 +277,17 @@ MANAGE_SERVICE = 'manage_service'
|
||||
SEND_MESSAGES = 'send_messages'
|
||||
MANAGE_API_KEYS = 'manage_api_keys'
|
||||
MANAGE_TEMPLATES = 'manage_templates'
|
||||
MANAGE_TEAM = 'manage_team'
|
||||
VIEW_ACTIVITY = 'view_activity'
|
||||
|
||||
# List of permissions
|
||||
PERMISSION_LIST = [
|
||||
MANAGE_SERVICE,
|
||||
SEND_MESSAGES,
|
||||
MANAGE_API_KEYS,
|
||||
MANAGE_TEMPLATES]
|
||||
MANAGE_TEMPLATES,
|
||||
MANAGE_TEAM,
|
||||
VIEW_ACTIVITY]
|
||||
|
||||
|
||||
class Permission(db.Model):
|
||||
@@ -295,10 +299,11 @@ class Permission(db.Model):
|
||||
service = db.relationship('Service')
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True, nullable=False)
|
||||
user = db.relationship('User')
|
||||
permission = db.Column(db.Enum(*PERMISSION_LIST, name='permission_types'),
|
||||
index=False,
|
||||
unique=False,
|
||||
nullable=False)
|
||||
permission = db.Column(
|
||||
db.Enum(*PERMISSION_LIST, name='permission_types'),
|
||||
index=False,
|
||||
unique=False,
|
||||
nullable=False)
|
||||
created_at = db.Column(
|
||||
db.DateTime,
|
||||
index=False,
|
||||
|
||||
@@ -4,10 +4,12 @@ from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request,
|
||||
current_app
|
||||
current_app,
|
||||
url_for
|
||||
)
|
||||
|
||||
from app import api_user, encryption, create_uuid
|
||||
from app.authentication.auth import require_admin
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
services_dao,
|
||||
@@ -40,6 +42,86 @@ def get_notifications(notification_id):
|
||||
return jsonify(result="error", message="not found"), 404
|
||||
|
||||
|
||||
@notifications.route('/notifications', methods=['GET'])
|
||||
def get_all_notifications():
|
||||
page = get_page_from_request()
|
||||
|
||||
if not page:
|
||||
return jsonify(result="error", message="Invalid page"), 400
|
||||
|
||||
all_notifications = notifications_dao.get_notifications_for_service(api_user['client'], page)
|
||||
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(all_notifications.items, many=True).data,
|
||||
links=pagination_links(
|
||||
all_notifications,
|
||||
'.get_all_notifications',
|
||||
request.args
|
||||
)
|
||||
), 200
|
||||
|
||||
|
||||
@notifications.route('/service/<service_id>/notifications', methods=['GET'])
|
||||
@require_admin()
|
||||
def get_all_notifications_for_service(service_id):
|
||||
page = get_page_from_request()
|
||||
|
||||
if not page:
|
||||
return jsonify(result="error", message="Invalid page"), 400
|
||||
|
||||
all_notifications = notifications_dao.get_notifications_for_service(service_id, page)
|
||||
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(all_notifications.items, many=True).data,
|
||||
links=pagination_links(
|
||||
all_notifications,
|
||||
'.get_all_notifications_for_service',
|
||||
request.args
|
||||
)
|
||||
), 200
|
||||
|
||||
|
||||
@notifications.route('/service/<service_id>/job/<job_id>/notifications', methods=['GET'])
|
||||
@require_admin()
|
||||
def get_all_notifications_for_service_job(service_id, job_id):
|
||||
page = get_page_from_request()
|
||||
|
||||
if not page:
|
||||
return jsonify(result="error", message="Invalid page"), 400
|
||||
|
||||
all_notifications = notifications_dao.get_notifications_for_job(service_id, job_id, page)
|
||||
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(all_notifications.items, many=True).data,
|
||||
links=pagination_links(
|
||||
all_notifications,
|
||||
'.get_all_notifications_for_service_job',
|
||||
request.args
|
||||
)
|
||||
), 200
|
||||
|
||||
|
||||
def get_page_from_request():
|
||||
if 'page' in request.args:
|
||||
try:
|
||||
return int(request.args['page'])
|
||||
|
||||
except ValueError:
|
||||
return None
|
||||
else:
|
||||
return 1
|
||||
|
||||
|
||||
def pagination_links(pagination, endpoint, args):
|
||||
links = dict()
|
||||
if pagination.has_prev:
|
||||
links['prev'] = url_for(endpoint, **dict(list(args.items()) + [('page', pagination.prev_num)]))
|
||||
if pagination.has_next:
|
||||
links['next'] = url_for(endpoint, **dict(list(args.items()) + [('page', pagination.next_num)]))
|
||||
links['last'] = url_for(endpoint, **dict(list(args.items()) + [('page', pagination.pages)]))
|
||||
return links
|
||||
|
||||
|
||||
@notifications.route('/notifications/sms', methods=['POST'])
|
||||
def create_sms_notification():
|
||||
return send_notification(notification_type=SMS_NOTIFICATION)
|
||||
|
||||
Reference in New Issue
Block a user