Moved code related to post creation to MVP

This commit is contained in:
Manuel Cortez 2019-01-07 13:16:20 -06:00
parent 0d7bae5be8
commit b4625335b6
11 changed files with 156 additions and 105 deletions

View File

@ -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):

View File

@ -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

View File

@ -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"))

View File

@ -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]

View File

@ -1,4 +1,5 @@
from .attach import *
from . audioRecorder import *
from .configuration import *
from .postCreation import *
from .profiles import *

View File

@ -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()

View File

@ -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.")

View File

@ -13,5 +13,6 @@
"""
from .attach import *
from .audioRecorder import *
from .postCreation import *
from .configuration import *
from .profiles import *

View File

@ -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

View File

@ -5,5 +5,6 @@
"""
from .dialogs.attach import *
from .dialogs.audioRecorder import *
from .dialogs.postCreation import *
from .dialogs.configuration import *
from .dialogs.profiles import *

View File

@ -53,10 +53,11 @@ 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)
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)
@ -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__()