Added basic photo uploader in wall posts. Description is not supported

This commit is contained in:
2016-04-11 11:48:35 -05:00
parent c7874759b1
commit 64b4b5573a
7 changed files with 109 additions and 44 deletions

18
src/controller/attach.py Normal file
View File

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

View File

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

View File

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