From 94c860520f67a7d66c63c3c06522ecfcf73275f2 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 31 Jan 2017 14:34:47 +0300 Subject: [PATCH 1/2] Update message.py Remove repetitive code and add new static method in Post object --- message.py | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/message.py b/message.py index 2cae7e5..8401cdc 100644 --- a/message.py +++ b/message.py @@ -38,39 +38,14 @@ def __init__(self, post): self.post = Post(post, attachments=True) def create_attachments(self): + data_to_send = Post.post_data_prepare(self.post) try: if self.repost: - return json.dumps([{ - 'fallback': '', - 'color': self.post.color, - 'text': self.post.text, - 'ts': self.post.ts, - 'footer': self.post.footer, - 'footer_icon': self.post.footer_icon, - 'image_url': self.post.image_url, - 'thumb_url': self.post.thumb_url, - }, { - 'fallback': '', - 'color': self.repost.color, - 'text': self.repost.text, - 'ts': self.repost.ts, - 'footer': self.repost.footer, - 'footer_icon': self.repost.footer_icon, - 'image_url': self.repost.image_url, - 'thumb_url': self.repost.thumb_url, - }]) + data_repost_to_send = Post.post_data_prepare(self.repost) + return json.dumps([data_to_send, data_repost_to_send]) except AttributeError: - return json.dumps([{ - 'fallback': '', - 'color': self.post.color, - 'text': self.post.text, - 'ts': self.post.ts, - 'footer': self.post.footer, - 'footer_icon': self.post.footer_icon, - 'image_url': self.post.image_url, - 'thumb_url': self.post.thumb_url, - 'mrkdwn_in': ['text'], - }]) + data_to_send['mrkdwn_in'] = ['text'] + return json.dumps([data_to_send]) @staticmethod def send_message(auth, channel, text, attachments=None, as_user=True): @@ -87,7 +62,6 @@ def __init__(self, post, attachments=None): self.color = '#0093DA' self.footer = 'Lambda ФРЭЛА | Лямбда' self.footer_icon = 'http://lambda-it.ru/static/img/lambda_logo_mid.png' - # try: if attachments: try: if post['attachments']: @@ -114,6 +88,19 @@ def get_image(attachments): return image_url, thumb_url else: return None, None + + @staticmethod + def post_data_prepare(post): + return { + 'fallback': '', + 'color': post.color, + 'text': post.text, + 'ts': post.ts, + 'footer': post.footer, + 'footer_icon': post.footer_icon, + 'image_url': post.image_url, + 'thumb_url': post.thumb_url, + } class Repost(Post): From c32127be0cef77adea3a7f341991379a4853b103 Mon Sep 17 00:00:00 2001 From: Timur Date: Tue, 31 Jan 2017 15:48:41 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20`staticmethod`=20=D0=BD=D0=B0=20=D0=BE=D0=B1=D1=8B?= =?UTF-8?q?=D1=87=D0=BD=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/message.py b/message.py index 8401cdc..6f066d5 100644 --- a/message.py +++ b/message.py @@ -38,10 +38,10 @@ def __init__(self, post): self.post = Post(post, attachments=True) def create_attachments(self): - data_to_send = Post.post_data_prepare(self.post) + data_to_send = self.post.json_prepare() try: if self.repost: - data_repost_to_send = Post.post_data_prepare(self.repost) + data_repost_to_send = self.repost.json_prepare() return json.dumps([data_to_send, data_repost_to_send]) except AttributeError: data_to_send['mrkdwn_in'] = ['text'] @@ -88,18 +88,17 @@ def get_image(attachments): return image_url, thumb_url else: return None, None - - @staticmethod - def post_data_prepare(post): + + def json_prepare(self): return { 'fallback': '', - 'color': post.color, - 'text': post.text, - 'ts': post.ts, - 'footer': post.footer, - 'footer_icon': post.footer_icon, - 'image_url': post.image_url, - 'thumb_url': post.thumb_url, + 'color': self.color, + 'text': self.text, + 'ts': self.ts, + 'footer': self.footer, + 'footer_icon': self.footer_icon, + 'image_url': self.image_url, + 'thumb_url': self.thumb_url, }