3106504f39
[Delivers #133555165]
129 lines
3.5 KiB
Python
129 lines
3.5 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',
|
|
},
|
|
'media_type': {
|
|
'type': 'string',
|
|
'description': 'The media type of the message',
|
|
'enum': ['text/plain', 'text/markdown'],
|
|
},
|
|
'severity': {
|
|
'type': 'string',
|
|
'description': 'The severity of the message',
|
|
'enum': ['info', 'warning', 'error'],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
'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',
|
|
},
|
|
'media_type': {
|
|
'type': 'string',
|
|
'description': 'The media type of the message',
|
|
'enum': ['text/plain', 'text/markdown'],
|
|
},
|
|
'severity': {
|
|
'type': 'string',
|
|
'description': 'The severity of the message',
|
|
'enum': ['info', 'warning', 'error'],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
@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,
|
|
'severity': message.severity,
|
|
'media_type': message.media_type.name,
|
|
}
|