Attaching documents is now supported

This commit is contained in:
Manuel Cortez 2019-01-28 05:36:51 -06:00
parent 94902c661f
commit ac268c0672
5 changed files with 65 additions and 12 deletions

View File

@ -21,6 +21,7 @@
* The audio search feature has received improvements: Now it is possible to indicate wether the search will be performed by title or artist, and select to sort the results by duration, popularity or date of addition to VK. * The audio search feature has received improvements: Now it is possible to indicate wether the search will be performed by title or artist, and select to sort the results by duration, popularity or date of addition to VK.
* Now it is possible to create and delete audio albums again. * Now it is possible to create and delete audio albums again.
* Fixed errors when moving songs to albums. Now everything works as expected. * Fixed errors when moving songs to albums. Now everything works as expected.
* Added documents to the list of supported files when adding attachments to a wall post or private message.
## Changes in version 0.18 (21.01.2019) ## Changes in version 0.18 (21.01.2019)

View File

@ -147,8 +147,7 @@ class baseBuffer(object):
def upload_attachments(self, attachments): def upload_attachments(self, attachments):
""" Upload attachments to VK before posting them. """ Upload attachments to VK before posting them.
Returns attachments formatted as string, as required by VK API. Returns attachments formatted as string, as required by VK API."""
Currently this function only supports photos and audios."""
# To do: Check the caption and description fields for this kind of attachments. # To do: Check the caption and description fields for this kind of attachments.
local_attachments = "" local_attachments = ""
uploader = upload.VkUpload(self.session.vk.session_object) uploader = upload.VkUpload(self.session.vk.session_object)
@ -174,6 +173,13 @@ class baseBuffer(object):
id = r["id"] id = r["id"]
owner_id = r["owner_id"] owner_id = r["owner_id"]
local_attachments += "audio{0}_{1},".format(owner_id, id) local_attachments += "audio{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "document":
document = i["file"]
title = i["title"]
r = uploader.document(document, title=title, to_wall=True)
id = r["doc"]["id"]
owner_id = r["doc"]["owner_id"]
local_attachments += "doc{0}_{1},".format(owner_id, id)
return local_attachments return local_attachments
def connect_events(self): def connect_events(self):
@ -931,6 +937,13 @@ class chatBuffer(baseBuffer):
id = r["audio_message"]["id"] id = r["audio_message"]["id"]
owner_id = r["audio_message"]["owner_id"] owner_id = r["audio_message"]["owner_id"]
local_attachments += "audio_message{0}_{1},".format(owner_id, id) local_attachments += "audio_message{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "document":
document = i["file"]
title = i["title"]
r = uploader.document(document, title=title, message_peer_id=self.kwargs["peer_id"])
id = r["doc"]["id"]
owner_id = r["doc"]["owner_id"]
local_attachments += "doc{0}_{1},".format(owner_id, id)
return local_attachments return local_attachments
def _send_message(self, text, attachments=[]): def _send_message(self, text, attachments=[]):

View File

@ -18,6 +18,7 @@ class attachInteractor(base.baseInteractor):
super(attachInteractor, self).install(*args, **kwargs) super(attachInteractor, self).install(*args, **kwargs)
widgetUtils.connect_event(self.view.photo, widgetUtils.BUTTON_PRESSED, self.on_image) widgetUtils.connect_event(self.view.photo, widgetUtils.BUTTON_PRESSED, self.on_image)
widgetUtils.connect_event(self.view.audio, widgetUtils.BUTTON_PRESSED, self.on_audio) widgetUtils.connect_event(self.view.audio, widgetUtils.BUTTON_PRESSED, self.on_audio)
widgetUtils.connect_event(self.view.document, widgetUtils.BUTTON_PRESSED, self.on_document)
if hasattr(self.view, "voice_message"): if hasattr(self.view, "voice_message"):
widgetUtils.connect_event(self.view.voice_message, widgetUtils.BUTTON_PRESSED, self.on_upload_voice_message) widgetUtils.connect_event(self.view.voice_message, widgetUtils.BUTTON_PRESSED, self.on_upload_voice_message)
widgetUtils.connect_event(self.view.remove, widgetUtils.BUTTON_PRESSED, self.on_remove_attachment) widgetUtils.connect_event(self.view.remove, widgetUtils.BUTTON_PRESSED, self.on_remove_attachment)
@ -44,6 +45,14 @@ class attachInteractor(base.baseInteractor):
widgetUtils.connect_event(m, widgetUtils.MENU, self.on_add_audio, menuitem=m.add) widgetUtils.connect_event(m, widgetUtils.MENU, self.on_add_audio, menuitem=m.add)
self.view.PopupMenu(m, self.view.audio.GetPosition()) self.view.PopupMenu(m, self.view.audio.GetPosition())
def on_document(self, *args, **kwargs):
""" display menu for adding document attachments. """
m = attachMenu()
# disable add from VK as it is not supported in documents, yet.
m.add.Enable(False)
widgetUtils.connect_event(m, widgetUtils.MENU, self.on_upload_document, menuitem=m.upload)
self.view.PopupMenu(m, self.view.photo.GetPosition())
def on_upload_image(self, *args, **kwargs): def on_upload_image(self, *args, **kwargs):
""" allows uploading an image from the computer. """ allows uploading an image from the computer.
""" """
@ -57,6 +66,16 @@ class attachInteractor(base.baseInteractor):
if audio != None: if audio != None:
self.presenter.upload_audio(audio) self.presenter.upload_audio(audio)
def on_upload_document(self, *args, **kwargs):
""" allows uploading a document from the computer.
"""
document = self.view.get_document()
if document != None:
if document.endswith(".mp3") or document.endswith(".exe"):
self.view.invalid_attachment()
return
self.presenter.upload_document(document)
def on_upload_voice_message(self, *args, **kwargs): def on_upload_voice_message(self, *args, **kwargs):
self.presenter.upload_voice_message() self.presenter.upload_voice_message()

View File

@ -63,6 +63,16 @@ class attachPresenter(base.basePresenter):
self.send_message("insert_attachment", attachment=info) self.send_message("insert_attachment", attachment=info)
self.send_message("enable_control", control="remove") self.send_message("enable_control", control="remove")
def upload_document(self, document):
""" allows uploading a document from the computer.
"""
doc_info = {"type": "document", "file": document, "from": "local", "title": os.path.basename(os.path.splitext(document)[0])}
self.attachments.append(doc_info)
# Translators: This is the text displayed in the attachments dialog, when the user adds a document.
info = [_("Document"), os.path.basename(document)]
self.send_message("insert_attachment", attachment=info)
self.send_message("enable_control", control="remove")
def upload_voice_message(self): def upload_voice_message(self):
a = audioRecorder.audioRecorderPresenter(view=views.audioRecorderDialog(), interactor=interactors.audioRecorderInteractor()) a = audioRecorder.audioRecorderPresenter(view=views.audioRecorderDialog(), interactor=interactors.audioRecorderInteractor())
if a.file != None and a.duration != 0: if a.file != None and a.duration != 0:

View File

@ -14,19 +14,20 @@ class attachDialog(widgetUtils.BaseDialog):
box.Add(lbl1, 0, wx.ALL, 5) box.Add(lbl1, 0, wx.ALL, 5)
box.Add(self.attachments.list, 0, wx.ALL, 5) box.Add(self.attachments.list, 0, wx.ALL, 5)
sizer.Add(box, 0, wx.ALL, 5) sizer.Add(box, 0, wx.ALL, 5)
static = wx.StaticBox(panel, label=_("Add attachments")) static = wx.StaticBoxSizer(parent=panel, orient=wx.HORIZONTAL, label=_("Add attachments"))
self.photo = wx.Button(panel, wx.NewId(), _("&Photo")) self.photo = wx.Button(static.GetStaticBox(), wx.NewId(), _("&Photo"))
self.audio = wx.Button(panel, wx.NewId(), _("Audio file")) self.audio = wx.Button(static.GetStaticBox(), wx.NewId(), _("Audio file"))
self.document = wx.Button(static.GetStaticBox(), wx.NewId(), _("Document"))
if voice_messages: if voice_messages:
self.voice_message = wx.Button(panel, wx.NewId(), _("Voice message")) self.voice_message = wx.Button(static.GetStaticBox(), wx.NewId(), _("Voice message"))
self.remove = wx.Button(panel, wx.NewId(), _("Remove attachment")) self.remove = wx.Button(static.GetStaticBox(), wx.NewId(), _("Remove attachment"))
self.remove.Enable(False) self.remove.Enable(False)
btnsizer = wx.StaticBoxSizer(static, wx.HORIZONTAL) static.Add(self.photo, 0, wx.ALL, 5)
btnsizer.Add(self.photo, 0, wx.ALL, 5) static.Add(self.audio, 0, wx.ALL, 5)
btnsizer.Add(self.audio, 0, wx.ALL, 5) static.Add(self.document, 0, wx.ALL, 5)
if voice_messages: if voice_messages:
btnsizer.Add(self.voice_message, 0, wx.ALL, 5) static.Add(self.voice_message, 0, wx.ALL, 5)
sizer.Add(btnsizer, 0, wx.ALL, 5) sizer.Add(static, 0, wx.ALL, 5)
ok = wx.Button(panel, wx.ID_OK) ok = wx.Button(panel, wx.ID_OK)
ok.SetDefault() ok.SetDefault()
cancelBtn = wx.Button(panel, wx.ID_CANCEL) cancelBtn = wx.Button(panel, wx.ID_CANCEL)
@ -56,3 +57,12 @@ class attachDialog(widgetUtils.BaseDialog):
if openFileDialog.ShowModal() == wx.ID_CANCEL: if openFileDialog.ShowModal() == wx.ID_CANCEL:
return None return None
return openFileDialog.GetPath() return openFileDialog.GetPath()
def get_document(self):
openFileDialog = wx.FileDialog(self, _("Select the file to be uploaded. All extensions are allowed except .mp3 and .exe."), "", "", _("All files (*.*)|*.*"), wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if openFileDialog.ShowModal() == wx.ID_CANCEL:
return None
return openFileDialog.GetPath()
def invalid_attachment(self):
return wx.MessageDialog(None, _("The file you are trying to upload contains an extension that is not allowed by VK."), _("Error"), style=wx.ICON_ERROR).ShowModal()