From 4ca6c37e54b247c962fa872d409fd4b6a4145da9 Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Thu, 20 Jul 2017 13:09:49 -0400 Subject: [PATCH 1/2] 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 From 0d239e08c2bc5912eddcd8a066cc98a5f9f70636 Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Wed, 26 Jul 2017 14:38:56 -0400 Subject: [PATCH 2/2] Add docstrings to globalmessages data interface --- endpoints/api/globalmessages.py | 5 +++- .../api/globalmessages_models_interface.py | 25 +++++++++++++++---- .../api/globalmessages_models_pre_oci.py | 12 +++++++-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/endpoints/api/globalmessages.py b/endpoints/api/globalmessages.py index 0ed868836..43ea58083 100644 --- a/endpoints/api/globalmessages.py +++ b/endpoints/api/globalmessages.py @@ -102,7 +102,10 @@ class GlobalUserMessages(ApiResource): abort(404) if SuperUserPermission().can(): - model.create_message(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) diff --git a/endpoints/api/globalmessages_models_interface.py b/endpoints/api/globalmessages_models_interface.py index b3b556ffc..679462c1d 100644 --- a/endpoints/api/globalmessages_models_interface.py +++ b/endpoints/api/globalmessages_models_interface.py @@ -23,17 +23,32 @@ class GlobalMessage( @add_metaclass(ABCMeta) class GlobalMessageDataInterface(object): + """ + Data interface for globalmessages API + """ @abstractmethod def get_all_messages(self): - pass + """ + + Returns: + list(GlobalMessage) + """ @abstractmethod - def create_message(self): - pass + def create_message(self, severity, media_type_name, content): + """ + + Returns: + GlobalMessage or None + """ @abstractmethod - def delete_message(self): - pass + def delete_message(self, uuid): + """ + + Returns: + void + """ \ No newline at end of file diff --git a/endpoints/api/globalmessages_models_pre_oci.py b/endpoints/api/globalmessages_models_pre_oci.py index 9a91ee0e7..d9a623f1b 100644 --- a/endpoints/api/globalmessages_models_pre_oci.py +++ b/endpoints/api/globalmessages_models_pre_oci.py @@ -8,13 +8,21 @@ class GlobalMessagePreOCI(GlobalMessageDataInterface): messages = model.message.get_messages() return [self._message(m) for m in messages] - def create_message(self, content): - model.message.create([content]) + 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,