mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2024-11-22 19:28:09 -06:00
Mastodon: Added experimental support for voting in polls
This commit is contained in:
parent
f3fd1087b4
commit
e31369e49a
@ -20,6 +20,7 @@ from extra import ocr
|
||||
from wxUI import buffers, dialogs, commonMessageDialogs
|
||||
from wxUI.dialogs.mastodon import menus
|
||||
from wxUI.dialogs.mastodon import dialogs as mastodon_dialogs
|
||||
from wxUI.dialogs.mastodon.postDialogs import attachedPoll
|
||||
|
||||
log = logging.getLogger("controller.buffers.mastodon.base")
|
||||
|
||||
@ -538,4 +539,29 @@ class BaseBuffer(base.Buffer):
|
||||
def ocr_image(self):
|
||||
post = self.get_item()
|
||||
media_list = []
|
||||
pass
|
||||
pass
|
||||
|
||||
def vote(self):
|
||||
post = self.get_item()
|
||||
if not hasattr(post, "poll") or post.poll == None:
|
||||
return
|
||||
poll = post.poll
|
||||
try:
|
||||
poll = self.session.api.poll(id=poll.id)
|
||||
except MastodonNotFoundError:
|
||||
output.speak(_("this poll no longer exists."))
|
||||
return
|
||||
if poll.expired:
|
||||
output.speak(_("This poll has already expired."))
|
||||
return
|
||||
if poll.voted:
|
||||
output.speak(_("You have already voted on this poll."))
|
||||
return
|
||||
options = poll.options
|
||||
dlg = attachedPoll(poll_options=[option.title for option in options], multiple=poll.multiple)
|
||||
answer = dlg.ShowModal()
|
||||
options = dlg.get_selected()
|
||||
dlg.Destroy()
|
||||
if answer != wx.ID_OK:
|
||||
return
|
||||
poll = self.session.api_call(call_name="poll_vote", id=poll.id, choices=options, preexec_message=_("Sending vote..."))
|
@ -42,6 +42,9 @@ class NotificationsBuffer(BaseBuffer):
|
||||
def unfav(self):
|
||||
pass
|
||||
|
||||
def vote(self):
|
||||
pass
|
||||
|
||||
def can_share(self):
|
||||
return False
|
||||
|
||||
|
@ -593,6 +593,11 @@ class Controller(object):
|
||||
if hasattr(buffer, "toggle_favorite"):
|
||||
return buffer.toggle_favorite()
|
||||
|
||||
def vote(self, *args, **kwargs):
|
||||
buffer = self.get_current_buffer()
|
||||
if hasattr(buffer, "vote"):
|
||||
return buffer.vote()
|
||||
|
||||
def view_item(self, *args, **kwargs):
|
||||
buffer = self.get_current_buffer()
|
||||
if hasattr(buffer, "view_item"):
|
||||
|
@ -297,7 +297,7 @@ class poll(wx.Dialog):
|
||||
self.option4 = wx.TextCtrl(self, wx.ID_ANY, "")
|
||||
self.option4.SetMaxLength(25)
|
||||
option4_sizer.Add(self.option4, 0, 0, 0)
|
||||
self.multiple = wx.CheckBox(self, wx.ID_ANY, _("Allow multiple votes per user"))
|
||||
self.multiple = wx.CheckBox(self, wx.ID_ANY, _("Allow multiple choices per user"))
|
||||
self.multiple.SetValue(False)
|
||||
sizer_1.Add(self.multiple, 0, wx.ALL, 5)
|
||||
self.hide_votes = wx.CheckBox(self, wx.ID_ANY, _("Hide votes count until the poll expires"))
|
||||
@ -327,4 +327,42 @@ class poll(wx.Dialog):
|
||||
options = self.get_options()
|
||||
if len(options) < 2:
|
||||
return wx.MessageDialog(self, _("Please make sure you have provided at least two options for the poll."), _("Not enough information"), wx.ICON_ERROR).ShowModal()
|
||||
self.EndModal(wx.ID_OK)
|
||||
self.EndModal(wx.ID_OK)
|
||||
|
||||
class attachedPoll(wx.Dialog):
|
||||
def __init__(self, poll_options, multiple=False, *args, **kwds):
|
||||
super(attachedPoll, self).__init__(parent=None, id=wx.NewId(), title=_("Vote in this poll"))
|
||||
self.poll_options = poll_options
|
||||
sizer_1 = wx.BoxSizer(wx.VERTICAL)
|
||||
sizer_2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, _("Options")), wx.VERTICAL)
|
||||
sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
|
||||
if multiple == False:
|
||||
for option in range(len(self.poll_options)):
|
||||
if option == 0:
|
||||
setattr(self, "option{}".format(option), wx.RadioButton(self, wx.ID_ANY, poll_options[option], style=wx.RB_GROUP))
|
||||
else:
|
||||
setattr(self, "option{}".format(option), wx.RadioButton(self, wx.ID_ANY, poll_options[option]))
|
||||
else:
|
||||
for option in range(len(self.poll_options)):
|
||||
setattr(self, "option{}".format(option), wx.CheckBox(self, wx.ID_ANY, poll_options[option]))
|
||||
sizer_2.Add(getattr(self, "option{}".format(option)), 1, wx.EXPAND, 0)
|
||||
btn_sizer = wx.StdDialogButtonSizer()
|
||||
sizer_1.Add(btn_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 4)
|
||||
self.button_OK = wx.Button(self, wx.ID_OK)
|
||||
self.button_OK.SetDefault()
|
||||
btn_sizer.AddButton(self.button_OK)
|
||||
self.button_CANCEL = wx.Button(self, wx.ID_CANCEL, "")
|
||||
btn_sizer.AddButton(self.button_CANCEL)
|
||||
btn_sizer.Realize()
|
||||
self.SetSizer(sizer_1)
|
||||
sizer_1.Fit(self)
|
||||
self.SetAffirmativeId(self.button_OK.GetId())
|
||||
self.SetEscapeId(self.button_CANCEL.GetId())
|
||||
self.Layout()
|
||||
|
||||
def get_selected(self):
|
||||
options = []
|
||||
for option in range(len(self.poll_options)):
|
||||
if getattr(self, "option{}".format(option)).GetValue() == True:
|
||||
options.append(option)
|
||||
return options
|
||||
|
Loading…
Reference in New Issue
Block a user