From 64b4b5573a35e9ba9bbc5cfb9ebd4e5b6189e393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Mon, 11 Apr 2016 11:48:35 -0500 Subject: [PATCH] Added basic photo uploader in wall posts. Description is not supported --- src/controller/attach.py | 18 ++++++++++++++++ src/controller/buffers.py | 41 ++++++++++++++++++++++++++--------- src/controller/messages.py | 32 +++++++-------------------- src/vk/upload.py | 3 +-- src/wxUI/dialogs/attach.py | 43 +++++++++++++++++++++++++++++++++++++ src/wxUI/dialogs/message.py | 10 ++------- src/wxUI/menus.py | 6 ++++++ 7 files changed, 109 insertions(+), 44 deletions(-) create mode 100644 src/controller/attach.py create mode 100644 src/wxUI/dialogs/attach.py diff --git a/src/controller/attach.py b/src/controller/attach.py new file mode 100644 index 0000000..f272844 --- /dev/null +++ b/src/controller/attach.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +import os +import widgetUtils +from wxUI.dialogs import attach as gui + +class attach(object): + def __init__(self): + self.attachments = list() + self.dialog = gui.attachDialog() + widgetUtils.connect_event(self.dialog.photo, widgetUtils.BUTTON_PRESSED, self.upload_image) + self.dialog.get_response() + + def upload_image(self, *args, **kwargs): + image, description = self.dialog.get_image() + if image != None: + self.attachments.append({"type": "photo", "file": image, "description": os.path.basename(image)}) + info = [_(u"Photo"), os.path.basename(image)] + self.dialog.attachments.insert_item(False, *info) \ No newline at end of file diff --git a/src/controller/buffers.py b/src/controller/buffers.py index cfc6ea5..16c818e 100644 --- a/src/controller/buffers.py +++ b/src/controller/buffers.py @@ -11,6 +11,7 @@ from pubsub import pub from sessionmanager import session from mysc.thread_utils import call_threaded from wxUI import commonMessages +from vk import upload class baseBuffer(object): """ a basic representation of a buffer. Other buffers should be derived from this class""" @@ -63,18 +64,38 @@ class baseBuffer(object): def post(self, *args, **kwargs): p = messages.post(title=_(u"Write your post"), caption="", text="") if p.message.get_response() == widgetUtils.OK: - msg = p.message.get_text().encode("utf-8") - privacy_opts = p.get_privacy_options() - attachments = "" - urls = utils.find_urls_in_text(msg) - if len(urls) != 0: - if len(attachments) == 0: attachments = urls[0] - else: attachments += urls[0] - msg = msg.replace(urls[0], "") - self.session.post_wall_status(message=msg, friends_only=privacy_opts, attachments=attachments) - pub.sendMessage("posted", buffer=self.name) + call_threaded(self.do_last, p=p) + + def do_last(self, p): + msg = p.message.get_text().encode("utf-8") + privacy_opts = p.get_privacy_options() + attachments = "" + if hasattr(p, "attachments"): + attachments = self.upload_attachments(p.attachments) + urls = utils.find_urls_in_text(msg) + if len(urls) != 0: + if len(attachments) == 0: attachments = urls[0] + else: attachments += urls[0] + msg = msg.replace(urls[0], "") + self.session.post_wall_status(message=msg, friends_only=privacy_opts, attachments=attachments) + pub.sendMessage("posted", buffer=self.name) p.message.Destroy() + def upload_attachments(self, attachments): + # To do: Check the caption and description fields for this kind of attachments. + local_attachments = "" + uploader = upload.VkUpload(self.session.vk.client) + for i in attachments: + if i["type"] == "photo": + photos = i["file"] + description = i["description"] + r = uploader.photo_wall(photos, caption=description) + id = r[0]["id"] + owner_id = r[0]["owner_id"] +# self.session.vk.client.photos.edit(photo_id=id, owner_id=owner_id, caption=description) + local_attachments += "photo{0}_{1},".format(owner_id, id) + return local_attachments + def connect_events(self): widgetUtils.connect_event(self.tab.post, widgetUtils.BUTTON_PRESSED, self.post) widgetUtils.connect_event(self.tab.list.list, widgetUtils.KEYPRESS, self.get_event) diff --git a/src/controller/messages.py b/src/controller/messages.py index 84f5e03..711639c 100644 --- a/src/controller/messages.py +++ b/src/controller/messages.py @@ -2,6 +2,7 @@ import widgetUtils import output from pubsub import pub +import attach from wxUI.dialogs import message from extra import SpellChecker, translator @@ -13,8 +14,9 @@ class post(object): 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) - self.image = None -# widgetUtils.connect_event(self.message.upload_image, widgetUtils.BUTTON_PRESSED, self.upload_image) + 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") @@ -43,28 +45,10 @@ class post(object): self.message.set_text(checker.fixed_text) checker.clean() -# def attach(self, *args, **kwargs): -# def completed_callback(): -# url = dlg.uploaderFunction.get_url() -# pub.unsubscribe(dlg.uploaderDialog.update, "uploading") -# dlg.uploaderDialog.destroy() -# if url != 0: -# self.message.set_text(self.message.get_text()+url+" #audio") -# else: -# output.speak(_(u"Unable to upload the audio")) -# dlg.cleanup() -# dlg = audioUploader.audioUploader(self.session.settings, completed_callback) - - def upload_image(self, *args, **kwargs): - if self.message.get("upload_image") == _(u"Discard image"): - del self.image - self.image = None - output.speak(_(u"Discarded")) - self.message.set("upload_image", _(u"Upload a picture")) - else: - self.image = self.message.get_image() - if self.image != None: - self.message.set("upload_image", _(u"Discard image")) + def show_attach_dialog(self, *args, **kwargs): + a = attach.attach() + if len(a.attachments) != 0: + self.attachments = a.attachments class comment(post): def __init__(self, title, caption, text): diff --git a/src/vk/upload.py b/src/vk/upload.py index 0947f2e..37c0a9d 100644 --- a/src/vk/upload.py +++ b/src/vk/upload.py @@ -78,7 +78,7 @@ class VkUpload(object): return response - def photo_wall(self, photos, user_id=None, group_id=None): + def photo_wall(self, photos, user_id=None, group_id=None, caption="No description"): """ Загрузка изображений на стену пользователя или в группу :param photos: список путей к изображениям, либо путь к изображению @@ -93,7 +93,6 @@ class VkUpload(object): elif group_id: values['group_id'] = group_id response = self.vk.photos.getWallUploadServer(**values) - url = response['upload_url'] photos_files = open_photos(photos) response = self.session.post(url, files=photos_files) diff --git a/src/wxUI/dialogs/attach.py b/src/wxUI/dialogs/attach.py new file mode 100644 index 0000000..1a758cf --- /dev/null +++ b/src/wxUI/dialogs/attach.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import wx +import widgetUtils + +class attachDialog(widgetUtils.BaseDialog): + def __init__(self): + super(attachDialog, self).__init__(None, title=_(u"Add an attachment")) + panel = wx.Panel(self) + sizer = wx.BoxSizer(wx.VERTICAL) + lbl1 = wx.StaticText(panel, wx.NewId(), _(u"Attachments")) + self.attachments = widgetUtils.list(panel, _(u"Type"), _(u"Title"), style=wx.LC_REPORT) + box = wx.BoxSizer(wx.HORIZONTAL) + box.Add(lbl1, 0, wx.ALL, 5) + box.Add(self.attachments.list, 0, wx.ALL, 5) + sizer.Add(box, 0, wx.ALL, 5) + static = wx.StaticBox(panel, label=_(u"Add attachments")) + self.photo = wx.Button(panel, wx.NewId(), _(u"&Photo")) + btnsizer = wx.StaticBoxSizer(static, wx.HORIZONTAL) + btnsizer.Add(self.photo, 0, wx.ALL, 5) + sizer.Add(btnsizer, 0, wx.ALL, 5) + ok = wx.Button(panel, wx.ID_OK) + ok.SetDefault() + cancelBtn = wx.Button(panel, wx.ID_CANCEL) + btnSizer = wx.BoxSizer() + btnSizer.Add(ok, 0, wx.ALL, 5) + btnSizer.Add(cancelBtn, 0, wx.ALL, 5) + sizer.Add(btnSizer, 0, wx.ALL, 5) + panel.SetSizer(sizer) + self.SetClientSize(sizer.CalcMin()) + + def get_image(self): + openFileDialog = wx.FileDialog(self, _(u"Select the picture to be uploaded"), "", "", _("Image files (*.png, *.jpg, *.gif)|*.png; *.jpg; *.gif"), wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) + if openFileDialog.ShowModal() == wx.ID_CANCEL: + return None + dsc = "" + return (openFileDialog.GetPath(), dsc) + + def ask_description(self): + dlg = wx.TextEntryDialog(self, _(u"please provide a description"), _(u"Description"), defaultValue="") + dlg.ShowModal() + result = dlg.GetValue() + dlg.Destroy() + return result diff --git a/src/wxUI/dialogs/message.py b/src/wxUI/dialogs/message.py index 9394975..910c94c 100644 --- a/src/wxUI/dialogs/message.py +++ b/src/wxUI/dialogs/message.py @@ -58,15 +58,14 @@ class post(textMessage): self.mainBox.Add(self.textBox, 0, wx.ALL, 5) self.create_privacy_box() self.mainBox.Add(self.privacyBox, 0, wx.ALL, 5) - self.upload_image = wx.Button(self.panel, -1, _(u"Upload a &picture"), size=wx.DefaultSize) - self.upload_image.Enable(False) + self.attach = wx.Button(self.panel, -1, _(u"Attach"), size=wx.DefaultSize) self.spellcheck = wx.Button(self.panel, -1, _("Spelling &correction"), size=wx.DefaultSize) self.translateButton = wx.Button(self.panel, -1, _(u"&Translate message"), size=wx.DefaultSize) self.okButton = wx.Button(self.panel, wx.ID_OK, _(u"Send"), size=wx.DefaultSize) self.okButton.SetDefault() cancelButton = wx.Button(self.panel, wx.ID_CANCEL, _(u"Close"), size=wx.DefaultSize) self.buttonsBox1 = wx.BoxSizer(wx.HORIZONTAL) - self.buttonsBox1.Add(self.upload_image, 0, wx.ALL, 10) + self.buttonsBox1.Add(self.attach, 0, wx.ALL, 10) self.buttonsBox1.Add(self.spellcheck, 0, wx.ALL, 10) self.buttonsBox1.Add(self.translateButton, 0, wx.ALL, 10) self.mainBox.Add(self.buttonsBox1, 0, wx.ALL, 10) @@ -86,11 +85,6 @@ class post(textMessage): self.createControls(message, title, text) self.SetClientSize(self.mainBox.CalcMin()) - def get_image(self): - openFileDialog = wx.FileDialog(self, _(u"Select the picture to be uploaded"), "", "", _("Image files (*.png, *.jpg, *.gif)|*.png; *.jpg; *.gif"), wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) - if openFileDialog.ShowModal() == wx.ID_CANCEL: - return None - return openFileDialog.GetPath() class comment(textMessage): def createControls(self, title, message, text): diff --git a/src/wxUI/menus.py b/src/wxUI/menus.py index 6ebb6f7..00a65ea 100644 --- a/src/wxUI/menus.py +++ b/src/wxUI/menus.py @@ -52,3 +52,9 @@ class toolsMenu(wx.Menu): self.AppendItem(self.translate) self.CheckSpelling = wx.MenuItem(self, -1, _(u"Check Spelling")) self.AppendItem(self.CheckSpelling) + +class attachMenu(wx.Menu): + def __init__(self): + super(attachMenu, self).__init__() + self.photo = wx.MenuItem(self, wx.NewId(), _(u"Picture")) + self.AppendItem(self.photo)