flake8 and style fixes and docstrings in helper_search

This commit is contained in:
Dmitri Bogomolov 2019-05-17 18:43:06 +03:00
parent ef6be53702
commit aba61e57a8
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
2 changed files with 86 additions and 66 deletions

View File

@ -1213,8 +1213,7 @@ class MyForm(settingsmixin.SMainWindow):
queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False) queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False)
for row in queryreturn: for row in queryreturn:
toAddress, fromAddress, subject, status, ackdata, lastactiontime = row self.addMessageListItemSent(tableWidget, *row)
self.addMessageListItemSent(tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime)
tableWidget.horizontalHeader().setSortIndicator( tableWidget.horizontalHeader().setSortIndicator(
3, QtCore.Qt.DescendingOrder) 3, QtCore.Qt.DescendingOrder)
@ -1246,7 +1245,7 @@ class MyForm(settingsmixin.SMainWindow):
queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly) queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly)
for row in queryreturn: for row in queryreturn:
msgfolder, msgid, toAddress, fromAddress, subject, received, read = row toAddress, fromAddress, subject, msgfolder, msgid, received, read = row
self.addMessageListItemInbox(tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read) self.addMessageListItemInbox(tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read)
tableWidget.horizontalHeader().setSortIndicator( tableWidget.horizontalHeader().setSortIndicator(

View File

@ -1,92 +1,113 @@
"""Additional SQL helper for searching messages""" """
Additional SQL helper for searching messages.
Used by :mod:`.bitmessageqt`.
"""
from helper_sql import sqlQuery from helper_sql import sqlQuery
from tr import _translate
try:
from PyQt4 import QtGui
haveQt = True
except ImportError:
haveQt = False
def search_translate(context, text): def search_sql(
"""Translation wrapper""" xAddress='toaddress', account=None, folder='inbox', where=None,
if haveQt: what=None, unreadOnly=False
return QtGui.QApplication.translate(context, text) ):
return text.lower() """
Search for messages from given account and folder having search term
in one of it's fields.
:param str xAddress: address field checked
def search_sql(xAddress="toaddress", account=None, folder="inbox", where=None, what=None, unreadOnly=False): ('fromaddress', 'toaddress' or 'both')
"""Perform a search in mailbox tables""" :param account: the account which is checked
:type account: :class:`.bitmessageqt.account.BMAccount`
instance
:param str folder: the folder which is checked
:param str where: message field which is checked ('toaddress',
'fromaddress', 'subject' or 'message'), by default check any field
:param str what: the search term
:param bool unreadOnly: if True, search only for unread messages
:return: all messages where <where> field contains <what>
:rtype: list[list]
"""
# pylint: disable=too-many-arguments, too-many-branches # pylint: disable=too-many-arguments, too-many-branches
if what is not None and what != "": if what:
what = "%" + what + "%" what = '%' + what + '%'
if where == search_translate("MainWindow", "To"): if where == _translate("MainWindow", "To"):
where = "toaddress" where = 'toaddress'
elif where == search_translate("MainWindow", "From"): elif where == _translate("MainWindow", "From"):
where = "fromaddress" where = 'fromaddress'
elif where == search_translate("MainWindow", "Subject"): elif where == _translate("MainWindow", "Subject"):
where = "subject" where = 'subject'
elif where == search_translate("MainWindow", "Message"): elif where == _translate("MainWindow", "Message"):
where = "message" where = 'message'
else: else:
where = "toaddress || fromaddress || subject || message" where = 'toaddress || fromaddress || subject || message'
else:
what = None
if folder == "sent": sqlStatementBase = 'SELECT toaddress, fromaddress, subject, ' + (
sqlStatementBase = ''' 'status, ackdata, lastactiontime FROM sent ' if folder == 'sent'
SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime else 'folder, msgid, received, read FROM inbox '
FROM sent ''' )
else:
sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read
FROM inbox '''
sqlStatementParts = [] sqlStatementParts = []
sqlArguments = [] sqlArguments = []
if account is not None: if account is not None:
if xAddress == 'both': if xAddress == 'both':
sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)") sqlStatementParts.append('(fromaddress = ? OR toaddress = ?)')
sqlArguments.append(account) sqlArguments.append(account)
sqlArguments.append(account) sqlArguments.append(account)
else: else:
sqlStatementParts.append(xAddress + " = ? ") sqlStatementParts.append(xAddress + ' = ? ')
sqlArguments.append(account) sqlArguments.append(account)
if folder is not None: if folder is not None:
if folder == "new": if folder == 'new':
folder = "inbox" folder = 'inbox'
unreadOnly = True unreadOnly = True
sqlStatementParts.append("folder = ? ") sqlStatementParts.append('folder = ? ')
sqlArguments.append(folder) sqlArguments.append(folder)
else: else:
sqlStatementParts.append("folder != ?") sqlStatementParts.append('folder != ?')
sqlArguments.append("trash") sqlArguments.append('trash')
if what is not None: if what:
sqlStatementParts.append("%s LIKE ?" % (where)) sqlStatementParts.append('%s LIKE ?' % (where))
sqlArguments.append(what) sqlArguments.append(what)
if unreadOnly: if unreadOnly:
sqlStatementParts.append("read = 0") sqlStatementParts.append('read = 0')
if sqlStatementParts: if sqlStatementParts:
sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts) sqlStatementBase += 'WHERE ' + ' AND '.join(sqlStatementParts)
if folder == "sent": if folder == 'sent':
sqlStatementBase += " ORDER BY lastactiontime" sqlStatementBase += ' ORDER BY lastactiontime'
return sqlQuery(sqlStatementBase, sqlArguments) return sqlQuery(sqlStatementBase, sqlArguments)
def check_match(toAddress, fromAddress, subject, message, where=None, what=None): def check_match(
"""Check if a single message matches a filter (used when new messages are added to messagelists)""" toAddress, fromAddress, subject, message, where=None, what=None):
"""
Check if a single message matches a filter (used when new messages
are added to messagelists)
"""
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
if what is not None and what != "": if not what:
if where in (search_translate("MainWindow", "To"), search_translate("MainWindow", "All")): return True
if what.lower() not in toAddress.lower():
return False if where in (
elif where in (search_translate("MainWindow", "From"), search_translate("MainWindow", "All")): _translate("MainWindow", "To"), _translate("MainWindow", "All")
if what.lower() not in fromAddress.lower(): ):
return False if what.lower() not in toAddress.lower():
elif where in (search_translate("MainWindow", "Subject"), search_translate("MainWindow", "All")): return False
if what.lower() not in subject.lower(): elif where in (
return False _translate("MainWindow", "From"), _translate("MainWindow", "All")
elif where in (search_translate("MainWindow", "Message"), search_translate("MainWindow", "All")): ):
if what.lower() not in message.lower(): if what.lower() not in fromAddress.lower():
return False return False
elif where in (
_translate("MainWindow", "Subject"),
_translate("MainWindow", "All")
):
if what.lower() not in subject.lower():
return False
elif where in (
_translate("MainWindow", "Message"),
_translate("MainWindow", "All")
):
if what.lower() not in message.lower():
return False
return True return True