This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/endpoints/api/globalmessages.py
Joseph Schorr b1c17b1a45 Fix messages API to not NPE
show_if does not work on a method route
2016-10-17 17:43:12 -04:00

107 lines
2.7 KiB
Python

""" Messages API. """
from flask import abort
from flask import make_response
from flask import request
import features
from auth import scopes
from auth.permissions import SuperUserPermission
from data import model
from endpoints.api import (ApiResource, resource, nickname,
require_fresh_login, verify_not_prod, validate_json_request,
require_scope, show_if,)
@resource('/v1/messages')
class GlobalUserMessages(ApiResource):
""" Resource for getting a list of super user messages """
schemas = {
'GetMessage': {
'id': 'GetMessage',
'type': 'object',
'description': 'Messages that a super user has saved in the past',
'properties': {
'message': {
'type': 'array',
'description': 'A list of messages',
'itemType': {
'type': 'object',
'properties': {
'uuid': {
'type': 'string',
'description': 'The message id',
},
'content': {
'type': 'string',
'description': 'The actual message',
},
},
},
},
},
},
'CreateMessage': {
'id': 'CreateMessage',
'type': 'object',
'description': 'Create a new message',
'properties': {
'message': {
'type': 'object',
'description': 'A single message',
'properties': {
'content': {
'type': 'string',
'description': 'The actual message',
},
},
},
},
}
}
@nickname('getGlobalMessages')
def get(self):
""" Return a super users messages """
return {
'messages': [message_view(m) for m in model.message.get_messages()],
}
@require_fresh_login
@verify_not_prod
@nickname('createGlobalMessage')
@validate_json_request('CreateMessage')
@require_scope(scopes.SUPERUSER)
def post(self):
""" Create a message """
if not features.SUPER_USERS:
abort(404)
if SuperUserPermission().can():
model.message.create([request.get_json()['message']])
return make_response('', 201)
abort(403)
@resource('/v1/message/<uuid>')
@show_if(features.SUPER_USERS)
class GlobalUserMessage(ApiResource):
""" Resource for managing individual messages """
@require_fresh_login
@verify_not_prod
@nickname('deleteGlobalMessage')
@require_scope(scopes.SUPERUSER)
def delete(self, uuid):
""" Delete a message """
if SuperUserPermission().can():
model.message.delete_message([uuid])
return make_response('', 204)
abort(403)
def message_view(message):
return {
'uuid': message.uuid,
'content': message.content,
}