From 4ca6c37e54b247c962fa872d409fd4b6a4145da9 Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Thu, 20 Jul 2017 13:09:49 -0400 Subject: [PATCH] Add data interface for globalmessages API --- endpoints/api/globalmessages.py | 17 ++------ .../api/globalmessages_models_interface.py | 39 +++++++++++++++++++ .../api/globalmessages_models_pre_oci.py | 25 ++++++++++++ 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 endpoints/api/globalmessages_models_interface.py create mode 100644 endpoints/api/globalmessages_models_pre_oci.py diff --git a/endpoints/api/globalmessages.py b/endpoints/api/globalmessages.py index d6b491d2f..0ed868836 100644 --- a/endpoints/api/globalmessages.py +++ b/endpoints/api/globalmessages.py @@ -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, - } diff --git a/endpoints/api/globalmessages_models_interface.py b/endpoints/api/globalmessages_models_interface.py new file mode 100644 index 000000000..b3b556ffc --- /dev/null +++ b/endpoints/api/globalmessages_models_interface.py @@ -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 + + \ No newline at end of file diff --git a/endpoints/api/globalmessages_models_pre_oci.py b/endpoints/api/globalmessages_models_pre_oci.py new file mode 100644 index 000000000..9a91ee0e7 --- /dev/null +++ b/endpoints/api/globalmessages_models_pre_oci.py @@ -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() \ No newline at end of file