Autocompletion improvements. Adding/removing users manually is now possible.

This commit is contained in:
Manuel Cortez 2014-12-09 17:56:43 -06:00
parent 21970f2c94
commit 2ce59cf208
5 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import storage
import wx
import wx_manage
class autocompletionManage(object):
def __init__(self, window):
super(autocompletionManage, self).__init__()
self.window = window
self.dialog = wx_manage.autocompletionManageDialog()
self.database = storage.storage()
self.users = self.database.get_all_users()
self.dialog.put_users(self.users)
self.dialog.add.Bind(wx.EVT_BUTTON, self.add_user)
self.dialog.remove.Bind(wx.EVT_BUTTON, self.remove_user)
self.dialog.ShowModal()
def update_list(self):
item = self.dialog.users.get_selected()
self.dialog.users.clear()
self.users = self.database.get_all_users()
self.dialog.put_users(self.users)
self.dialog.users.select_item(item)
def add_user(self, event=None):
usr = self.dialog.get_user()
if usr == False:
return
try:
data = self.window.twitter.twitter.show_user(screen_name=usr)
except:
self.dialog.show_invalid_user_error()
return
self.database.set_user(data["screen_name"], data["name"], 0)
self.update_list()
def remove_user(self, ev):
ask = wx.MessageDialog(None, _(u"Are you sure you want to delete this user from the database? This user will not appear on the autocomplete results anymore."), _(u"Confirm"), wx.YES_NO|wx.ICON_QUESTION)
if ask.ShowModal() == wx.ID_YES:
item = self.dialog.users.get_selected()
user = self.users[item]
self.database.remove_user(user[0])
self.update_list()

View File

@ -3,6 +3,7 @@ import storage
import wx
import config
import wx_settings
import manage
import output
from mysc.thread_utils import call_threaded
@ -13,6 +14,7 @@ class autocompletionSettings(object):
self.dialog = wx_settings.autocompletionSettingsDialog()
self.dialog.friends_buffer.SetValue(config.main["mysc"]["save_friends_in_autocompletion_db"])
self.dialog.followers_buffer.SetValue(config.main["mysc"]["save_followers_in_autocompletion_db"])
self.dialog.viewList.Bind(wx.EVT_BUTTON, self.view_list)
if self.dialog.ShowModal() == wx.ID_OK:
call_threaded(self.add_users_to_database)
@ -36,6 +38,10 @@ class autocompletionSettings(object):
wx_settings.show_success_dialog()
self.dialog.Destroy()
def view_list(self, ev):
q = manage.autocompletionManage(self.window)
def execute_at_startup(window):
database = storage.storage()
if config.main["mysc"]["save_followers_in_autocompletion_db"] == True:

View File

@ -17,6 +17,10 @@ class storage(object):
else:
return True
def get_all_users(self):
self.cursor.execute("""select * from users""")
return self.cursor.fetchall()
def get_users(self, term):
self.cursor.execute("""SELECT * FROM users WHERE user LIKE ?""", ('{}%'.format(term),))
return self.cursor.fetchall()

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
import wx
from multiplatform_widgets import widgets
class autocompletionManageDialog(wx.Dialog):
def __init__(self):
super(autocompletionManageDialog, self).__init__(parent=None, id=-1, title=_(u"Manage Autocomplete users database"))
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
label = wx.StaticText(panel, -1, _(u"Users in your database"))
self.users = widgets.list(panel, _(u"Username"), _(u"Name"), style=wx.LC_REPORT)
sizer.Add(label, 0, wx.ALL, 5)
sizer.Add(self.users.list, 0, wx.ALL, 5)
self.add = wx.Button(panel, -1, _(u"Add new user"))
self.remove = wx.Button(panel, -1, _(u"Remove user"))
optionsBox = wx.BoxSizer(wx.HORIZONTAL)
optionsBox.Add(self.add, 0, wx.ALL, 5)
optionsBox.Add(self.remove, 0, wx.ALL, 5)
sizer.Add(optionsBox, 0, wx.ALL, 5)
ok = wx.Button(panel, wx.ID_OK)
cancel = wx.Button(panel, wx.ID_CANCEL)
sizerBtn = wx.BoxSizer(wx.HORIZONTAL)
sizerBtn.Add(ok, 0, wx.ALL, 5)
sizer.Add(cancel, 0, wx.ALL, 5)
sizer.Add(sizerBtn, 0, wx.ALL, 5)
panel.SetSizer(sizer)
self.SetClientSize(sizer.CalcMin())
def put_users(self, users):
for i in users:
j = [i[0], i[1]]
self.users.insert_item(False, *j)
def get_user(self):
usr = False
userDlg = wx.TextEntryDialog(None, _(u"Twitter username"), _(u"Add user to database"))
if userDlg.ShowModal() == wx.ID_OK:
usr = userDlg.GetValue()
return usr
def show_invalid_user_error(self):
wx.MessageDialog(None, _(u"The user does not exist"), _(u"Error!"), wx.ICON_ERROR).ShowModal()

View File

@ -10,6 +10,8 @@ class autocompletionSettingsDialog(wx.Dialog):
self.friends_buffer = wx.CheckBox(panel, -1, _(u"Add users from friends buffer"))
sizer.Add(self.followers_buffer, 0, wx.ALL, 5)
sizer.Add(self.friends_buffer, 0, wx.ALL, 5)
self.viewList = wx.Button(panel, -1, _(u"See the user list"))
sizer.Add(self.viewList, 0, wx.ALL, 5)
ok = wx.Button(panel, wx.ID_OK)
cancel = wx.Button(panel, wx.ID_CANCEL)
sizerBtn = wx.BoxSizer(wx.HORIZONTAL)