Add data interface for globalmessages API
This commit is contained in:
parent
fe6760749a
commit
4ca6c37e54
3 changed files with 68 additions and 13 deletions
|
@ -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,7 @@ class GlobalUserMessages(ApiResource):
|
|||
abort(404)
|
||||
|
||||
if SuperUserPermission().can():
|
||||
model.message.create([request.get_json()['message']])
|
||||
model.create_message(request.get_json()['message'])
|
||||
return make_response('', 201)
|
||||
|
||||
abort(403)
|
||||
|
@ -119,16 +119,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,
|
||||
}
|
||||
|
|
39
endpoints/api/globalmessages_models_interface.py
Normal file
39
endpoints/api/globalmessages_models_interface.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
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):
|
||||
|
||||
@abstractmethod
|
||||
def get_all_messages(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def create_message(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def delete_message(self):
|
||||
pass
|
||||
|
||||
|
25
endpoints/api/globalmessages_models_pre_oci.py
Normal file
25
endpoints/api/globalmessages_models_pre_oci.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
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, content):
|
||||
model.message.create([content])
|
||||
|
||||
def delete_message(self, uuid):
|
||||
model.message.delete_message([uuid])
|
||||
|
||||
def _message(self, message_obj):
|
||||
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()
|
Reference in a new issue