From b4625335b6dc976cc28437f1adf518a2e750cbd8 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Mon, 7 Jan 2019 13:16:20 -0600 Subject: [PATCH] Moved code related to post creation to MVP --- src/controller/buffers.py | 19 ++--- src/controller/mainController.py | 1 - src/controller/messages.py | 83 ------------------- src/controller/posts.py | 10 ++- src/interactors/__init__.py | 1 + src/interactors/postCreation.py | 70 ++++++++++++++++ src/interactors/profiles.py | 2 +- src/presenters/__init__.py | 1 + src/presenters/postCreation.py | 59 +++++++++++++ src/views/__init__.py | 1 + .../dialogs/postCreation.py} | 14 ++-- 11 files changed, 156 insertions(+), 105 deletions(-) delete mode 100644 src/controller/messages.py create mode 100644 src/interactors/postCreation.py create mode 100644 src/presenters/postCreation.py rename src/{wxUI/dialogs/message.py => views/dialogs/postCreation.py} (93%) diff --git a/src/controller/buffers.py b/src/controller/buffers.py index ff07339..12ccb99 100644 --- a/src/controller/buffers.py +++ b/src/controller/buffers.py @@ -11,7 +11,6 @@ import views import interactors import languageHandler import widgetUtils -from . import messages from presenters import player import output from . import selector @@ -114,14 +113,14 @@ class baseBuffer(object): """ Create a post in the current user's wall. This process is handled in two parts. This is the first part, where the GUI is created and user can send the post. During the second part (threaded), the post will be sent to the API.""" - p = messages.post(session=self.session, title=_("Write your post"), caption="", text="") - if p.message.get_response() == widgetUtils.OK: + p = presenters.postPresenter(session=self.session, interactor=interactors.postInteractor(), view=views.post(title=_("Write your post"), message="", text="")) + if hasattr(p, "text") or hasattr(p, "privacy"): call_threaded(self.do_last, p=p) def do_last(self, p, parent_endpoint="wall", child_endpoint="post", *args, **kwargs): """ Second part of post function. Here everything is going to be sent to the API""" - msg = p.message.get_text() - privacy_opts = p.get_privacy_options() + msg = p.text + privacy_opts = p.privacy attachments = "" if hasattr(p, "attachments"): attachments = self.upload_attachments(p.attachments) @@ -259,9 +258,9 @@ class baseBuffer(object): post = self.get_post() if post == None: return - comment = messages.comment(title=_("Add a comment"), caption="", text="") - if comment.message.get_response() == widgetUtils.OK: - msg = comment.message.get_text().encode("utf-8") + comment = presenters.postPresenter(session=self.session, interactor=interactors.postInteractor(), view=views.post(title=_("Add a comment"), message="", text="", mode="comment")) + if hasattr(comment, "text") or hasattr(comment, "privacy"): + msg = comment.text try: user = post[self.user_key] id = post[self.post_key] @@ -435,8 +434,8 @@ class feedBuffer(baseBuffer): return super(feedBuffer, self).post() owner_id = self.kwargs["owner_id"] user = self.session.get_user_name(owner_id) - p = messages.post(session=self.session, title=_("Post to {user}'s wall").format(user=user,), caption="", text="") - if p.message.get_response() == widgetUtils.OK: + p = presenters.postPresenter(session=self.session, interactor=interactors.postInteractor(), view=views.post(title=_("Write your post"), message="", text="")) + if hasattr(p, "text") or hasattr(p, "privacy"): call_threaded(self.do_last, p=p, owner_id=owner_id) class communityBuffer(feedBuffer): diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 81e3a21..6c6df0e 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -23,7 +23,6 @@ from wxUI.dialogs import search as searchDialogs from wxUI.dialogs import timeline, creation from update import updater from issueReporter import issueReporter -from . import messages from . import buffers from presenters import player from . import posts diff --git a/src/controller/messages.py b/src/controller/messages.py deleted file mode 100644 index 26def37..0000000 --- a/src/controller/messages.py +++ /dev/null @@ -1,83 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals -import time -import widgetUtils -import presenters -import views -import interactors -import output -from pubsub import pub -from wxUI.dialogs import message, selector -from extra import SpellChecker, translator -from logging import getLogger - -log = getLogger("controller.message") - -class post(object): - def __init__(self, session, title, caption, text, post_type="post"): - super(post, self).__init__() - self.session = session - self.title = title - self.message = getattr(message, post_type)(title, caption, text) - self.message.set_title(title) - widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck) - widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate) - widgetUtils.connect_event(self.message.mention, widgetUtils.BUTTON_PRESSED, self.mention) - self.images = [] - if hasattr(self.message, "attach"): - widgetUtils.connect_event(self.message.attach, widgetUtils.BUTTON_PRESSED, self.show_attach_dialog) - - def get_privacy_options(self): - p = self.message.get("privacy") - if p == _("Friends of friends"): - privacy = 0 - elif p == _("All users"): - privacy = 1 - return privacy - - def mention(self, *args, **kwargs): - try: - fields = "id, first_name, last_name" - friends = self.session.vk.client.friends.get(count=5000, fields=fields) - except AttributeError: - time.sleep(2) - log.exception("Error retrieving friends...") - return self.mention(*args, **kwargs) - users = [] - for i in friends["items"]: - users.append("{0} {1}".format(i["first_name"], i["last_name"])) - select = selector.selectPeople(users) - if select.get_response() == widgetUtils.OK and select.users.GetCount() > 0: - self.tagged_people = [] - tagged_users = select.get_all_users() - for i in tagged_users: - self.tagged_people.append("[id%s|%s]" % (str(friends["items"][i]["id"]), friends["items"][i]["first_name"])) - self.message.text.SetValue(self.message.text.GetValue()+ ", ".join(self.tagged_people)) - - def translate(self, *args, **kwargs): - dlg = translator.gui.translateDialog() - if dlg.get_response() == widgetUtils.OK: - text_to_translate = self.message.get_text() - dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")] - msg = translator.translator.translate(text_to_translate, dest) - self.message.set_text(msg) - self.message.text_focus() - output.speak(_("Translated")) - dlg.Destroy() - - def spellcheck(self, event=None): - text = self.message.get_text() - checker = SpellChecker.spellchecker.spellChecker(text, "") - if hasattr(checker, "fixed_text"): - self.message.set_text(checker.fixed_text) - checker.clean() - - def show_attach_dialog(self, *args, **kwargs): - a = presenters.attachPresenter(session=self.session, view=views.attachDialog(), interactor=interactors.attachInteractor()) - if len(a.attachments) != 0: - self.attachments = a.attachments - -class comment(post): - def __init__(self, session, title, caption, text): - super(comment, self).__init__(session, title, caption, text, "comment") - self.message.set_title(_("New comment")) \ No newline at end of file diff --git a/src/controller/posts.py b/src/controller/posts.py index 1b3a32d..7dc0f30 100644 --- a/src/controller/posts.py +++ b/src/controller/posts.py @@ -7,10 +7,12 @@ import os import six import threading import arrow -from . import messages import requests import languageHandler import widgetUtils +import views +import presenters +import interactors import output import wx import webbrowser @@ -266,9 +268,9 @@ class postController(object): pass def add_comment(self, *args, **kwargs): - comment = messages.comment(session=self.session, title=_("Add a comment"), caption="", text="") - if comment.message.get_response() == widgetUtils.OK: - msg = comment.message.get_text().encode("utf-8") + comment = presenters.postPresenter(session=self.session, interactor=interactors.postInteractor(), view=views.post(title=_("Add a comment"), message="", text="", mode="comment")) + if hasattr(comment, "text") or hasattr(comment, "privacy"): + msg = comment.text try: user = self.post[self.user_identifier] id = self.post[self.post_identifier] diff --git a/src/interactors/__init__.py b/src/interactors/__init__.py index 789ef95..6860927 100644 --- a/src/interactors/__init__.py +++ b/src/interactors/__init__.py @@ -1,4 +1,5 @@ from .attach import * from . audioRecorder import * from .configuration import * +from .postCreation import * from .profiles import * \ No newline at end of file diff --git a/src/interactors/postCreation.py b/src/interactors/postCreation.py new file mode 100644 index 0000000..7865028 --- /dev/null +++ b/src/interactors/postCreation.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import widgetUtils +from wxUI.dialogs import selector +from pubsub import pub +from .import base + +class postInteractor(base.baseInteractor): + + def set(self, control, value): + if not hasattr(self.view, control): + raise AttributeError("The control is not present in the view.") + getattr(self.view, control).SetValue(value) + + def add_tagged_users(self, users): + self.view.text.SetValue(self.view.text.GetValue()+", ".join(users)) + + def install(self, *args, **kwargs): + super(postInteractor, self).install(*args, **kwargs) + widgetUtils.connect_event(self.view.spellcheck, widgetUtils.BUTTON_PRESSED, self.on_spellcheck) + widgetUtils.connect_event(self.view.translateButton, widgetUtils.BUTTON_PRESSED, self.on_translate) + widgetUtils.connect_event(self.view.mention, widgetUtils.BUTTON_PRESSED, self.on_mention) + if hasattr(self.view, "attach"): + widgetUtils.connect_event(self.view.attach, widgetUtils.BUTTON_PRESSED, self.on_add_attachments) + pub.subscribe(self.set, self.modulename+"_set") + pub.subscribe(self.add_tagged_users, self.modulename+"_add_tagged_users") + + def uninstall(self): + super(postInteractor, self).uninstall() + pub.unsubscribe(self.set, self.modulename+"_set") + pub.unsubscribe(self.add_tagged_users, self.modulename+"_add_tagged_users") + + def start(self): + self.result = self.view.get_response() + if self.result == widgetUtils.OK: + self.presenter.text = self.view.get_text() + if hasattr(self.view, "privacy"): + self.presenter.privacy = self.get_privacy_options() + else: + self.presenter.privacy = 0 + + def get_privacy_options(self): + p = self.view.get("privacy") + if p == _("Friends of friends"): + privacy = 0 + elif p == _("All users"): + privacy = 1 + return privacy + + def on_mention(self, *args, **kwargs): + users = self.presenter.get_friends() + select = selector.selectPeople(users) + if select.get_response() == widgetUtils.OK and select.users.GetCount() > 0: + tagged_users = select.get_all_users() + self.presenter.add_tagged_users(tagged_users) + + def on_translate(self, *args, **kwargs): + dlg = translator.gui.translateDialog() + if dlg.get_response() == widgetUtils.OK: + text_to_translate = self.message.get_text() + dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")] + self.presenter.translate(text_to_translate, dest) + dlg.Destroy() + + def on_spellcheck(self, event=None): + text = self.message.get_text() + self.presenter.spellcheck(text) + + def on_add_attachments(self, *args, **kwargs): + self.presenter.add_attachments() \ No newline at end of file diff --git a/src/interactors/profiles.py b/src/interactors/profiles.py index f1535bb..b33aa04 100644 --- a/src/interactors/profiles.py +++ b/src/interactors/profiles.py @@ -19,7 +19,7 @@ class userProfileInteractor(base.baseInteractor): def set(self, tab, control, value): if not hasattr(self.view, tab): - raise AttributeError("The viw does not contain the specified tab.") + raise AttributeError("The view does not contain the specified tab.") tab = getattr(self.view, tab) if not hasattr(tab, control): raise AttributeError("The control is not present in the tab.") diff --git a/src/presenters/__init__.py b/src/presenters/__init__.py index 9cf1470..cab6a51 100644 --- a/src/presenters/__init__.py +++ b/src/presenters/__init__.py @@ -13,5 +13,6 @@ """ from .attach import * from .audioRecorder import * +from .postCreation import * from .configuration import * from .profiles import * \ No newline at end of file diff --git a/src/presenters/postCreation.py b/src/presenters/postCreation.py new file mode 100644 index 0000000..96f928a --- /dev/null +++ b/src/presenters/postCreation.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import time +import views +import interactors +import output +from logging import getLogger +from pubsub import pub +from extra import SpellChecker, translator +from .import attach +from .import base + +log = getLogger("controller.message") + +class postPresenter(base.basePresenter): + + def __init__(self, session, view, interactor): + super(postPresenter, self).__init__(view=view, interactor=interactor, modulename="messages") + self.session = session + self.images = [] + self.tagged_people = [] + self.run() + + def get_friends(self): + try: + fields = "id, first_name, last_name" + self.friends = self.session.vk.client.friends.get(count=5000, fields=fields) + except AttributeError: + time.sleep(2) + log.exception("Error retrieving friends...") + return [] + self.users = [] + for i in self.friends["items"]: + self.users.append("{0} {1}".format(i["first_name"], i["last_name"])) + return self.users + + def add_tagged_users(self, tagged_users): + self.tagged_people = [] + for i in tagged_users: + self.tagged_people.append("[id%s|%s]" % (str(self.friends["items"][i]["id"]), self.friends["items"][i]["first_name"])) + self.send_message("add_tagged_users", users=self.tagged_people) + + def translate(self, text, language): + msg = translator.translator.translate(text, language) + self.send_message("set", control="text", value=msg) + self.send_message("focus_control", control="text") + output.speak(_("Translated")) + + def spellcheck(self, text): + checker = SpellChecker.spellchecker.spellChecker(text, "") + if hasattr(checker, "fixed_text"): + self.send_message("set", control="text", value=checker.fixed_text) + self.send_message("focus_control", control="text") + checker.clean() + + def add_attachments(self): + a = attach.attachPresenter(session=self.session, view=views.attachDialog(), interactor=interactors.attachInteractor()) + if len(a.attachments) != 0: + self.attachments = a.attachments diff --git a/src/views/__init__.py b/src/views/__init__.py index cfdf13c..93df602 100644 --- a/src/views/__init__.py +++ b/src/views/__init__.py @@ -5,5 +5,6 @@ """ from .dialogs.attach import * from .dialogs.audioRecorder import * +from .dialogs.postCreation import * from .dialogs.configuration import * from .dialogs.profiles import * \ No newline at end of file diff --git a/src/wxUI/dialogs/message.py b/src/views/dialogs/postCreation.py similarity index 93% rename from src/wxUI/dialogs/message.py rename to src/views/dialogs/postCreation.py index 1ce457a..e9f1026 100644 --- a/src/wxUI/dialogs/message.py +++ b/src/views/dialogs/postCreation.py @@ -53,12 +53,13 @@ class textMessage(widgetUtils.BaseDialog): return self.text.GetInsertionPoint() class post(textMessage): - def createControls(self, title, message, text): + def createControls(self, title, message, text, mode): self.mainBox = wx.BoxSizer(wx.VERTICAL) self.createTextArea(message, text) self.mainBox.Add(self.textBox, 0, wx.ALL, 5) - self.create_privacy_box() - self.mainBox.Add(self.privacyBox, 0, wx.ALL, 5) + if mode == "post": + self.create_privacy_box() + self.mainBox.Add(self.privacyBox, 0, wx.ALL, 5) self.attach = wx.Button(self.panel, -1, _("Attach"), size=wx.DefaultSize) self.mention = wx.Button(self.panel, wx.NewId(), _("Tag a friend")) self.spellcheck = wx.Button(self.panel, -1, _("Spelling &correction"), size=wx.DefaultSize) @@ -82,13 +83,13 @@ class post(textMessage): (wx.ACCEL_CTRL, ord('A'), selectId),]) self.SetAcceleratorTable(self.accel_tbl) self.panel.SetSizer(self.mainBox) + self.SetTitle(title) - def __init__(self, title, message, text): + def __init__(self, title, message, text, mode="post"): super(post, self).__init__() - self.createControls(message, title, text) + self.createControls(title, message, text, mode) self.SetClientSize(self.mainBox.CalcMin()) - class comment(textMessage): def createControls(self, title, message, text): self.mainBox = wx.BoxSizer(wx.VERTICAL) @@ -114,6 +115,7 @@ class comment(textMessage): self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('A'), selectId),]) self.SetAcceleratorTable(self.accel_tbl) self.panel.SetSizer(self.mainBox) + self.SetTitle(title) def __init__(self, title, message, text): super(comment, self).__init__()