Merge pull request #2808 from ecordell/QUAY-647/api-globalmessages-v22

Add data interface for globalmessages API
This commit is contained in:
Evan Cordell 2017-08-01 10:59:35 -04:00 committed by GitHub
commit 37ebfd9ab5
3 changed files with 94 additions and 13 deletions

View file

@ -6,10 +6,10 @@ 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,)
from globalmessages_models_pre_oci import pre_oci_model as model
@resource('/v1/messages')
@ -88,7 +88,7 @@ class GlobalUserMessages(ApiResource):
def get(self):
""" Return a super users messages """
return {
'messages': [message_view(m) for m in model.message.get_messages()],
'messages': [m.to_dict() for m in model.get_all_messages()],
}
@require_fresh_login
@ -102,7 +102,10 @@ class GlobalUserMessages(ApiResource):
abort(404)
if SuperUserPermission().can():
model.message.create([request.get_json()['message']])
message_req = request.get_json()['message']
message = model.create_message(message_req['severity'], message_req['media_type'], message_req['content'])
if message is None:
abort(400)
return make_response('', 201)
abort(403)
@ -119,16 +122,7 @@ class GlobalUserMessage(ApiResource):
def delete(self, uuid):
""" Delete a message """
if SuperUserPermission().can():
model.message.delete_message([uuid])
model.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,
}

View file

@ -0,0 +1,54 @@
from abc import ABCMeta, abstractmethod
from collections import namedtuple
from six import add_metaclass
class GlobalMessage(
namedtuple('GlobalMessage', [
'uuid',
'content',
'severity',
'media_type_name',
])):
def to_dict(self):
return {
'uuid': self.uuid,
'content': self.content,
'severity': self.severity,
'media_type': self.media_type_name,
}
@add_metaclass(ABCMeta)
class GlobalMessageDataInterface(object):
"""
Data interface for globalmessages API
"""
@abstractmethod
def get_all_messages(self):
"""
Returns:
list(GlobalMessage)
"""
@abstractmethod
def create_message(self, severity, media_type_name, content):
"""
Returns:
GlobalMessage or None
"""
@abstractmethod
def delete_message(self, uuid):
"""
Returns:
void
"""

View file

@ -0,0 +1,33 @@
from globalmessages_models_interface import GlobalMessageDataInterface, GlobalMessage
from data import model
class GlobalMessagePreOCI(GlobalMessageDataInterface):
def get_all_messages(self):
messages = model.message.get_messages()
return [self._message(m) for m in messages]
def create_message(self, severity, media_type_name, content):
message = {
'severity': severity,
'media_type': media_type_name,
'content': content
}
messages = model.message.create([message])
return self._message(messages[0])
def delete_message(self, uuid):
model.message.delete_message([uuid])
def _message(self, message_obj):
if message_obj is None:
return None
return GlobalMessage(
uuid=message_obj.uuid,
content=message_obj.content,
severity=message_obj.severity,
media_type_name=message_obj.media_type.name,
)
pre_oci_model = GlobalMessagePreOCI()