Skip to content

Commit

Permalink
Merge pull request #5910 from drew2a/fix/5909
Browse files Browse the repository at this point in the history
Add check on infohash key existing
  • Loading branch information
drew2a authored Jan 11, 2021
2 parents cf289e7 + 5e07056 commit 47dbcfd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/tribler-gui/tribler_gui/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from urllib.parse import unquote_plus

from tribler_gui.utilities import compose_magnetlink, quote_plus_unicode, unicode_quoter
from tribler_gui.utilities import compose_magnetlink, dict_item_is_any_of, quote_plus_unicode, unicode_quoter


def test_quoter_char():
Expand Down Expand Up @@ -105,3 +105,22 @@ def test_compose_magnetlink():
assert composed_link1 == expected_link1
assert composed_link2 == expected_link2
assert composed_link3 == expected_link3


def test_is_dict_has():
assert not dict_item_is_any_of(None, None, None)
assert not dict_item_is_any_of({}, None, None)

d = {
'k': 'v',
'k1': 'v1'
}

assert not dict_item_is_any_of(d, 'missed_key', None)
assert not dict_item_is_any_of(d, 'missed_key', ['any_value'])
assert not dict_item_is_any_of(d, 'k', ['missed_value'])
assert not dict_item_is_any_of(d, 'k', ['missed_value', 'missed_value1'])

assert dict_item_is_any_of(d, 'k', ['v'])
assert dict_item_is_any_of(d, 'k', ['v', 'a'])
assert dict_item_is_any_of(d, 'k', ['a', 'v'])
6 changes: 6 additions & 0 deletions src/tribler-gui/tribler_gui/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,9 @@ def disconnect(signal: pyqtSignal, callback: Callable):

class CreationTraceback(Exception):
pass


def dict_item_is_any_of(d, key, values):
if not d or not key or not values:
return False
return key in d and d[key] in values
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from tribler_gui.defs import HEALTH_CHECKING, HEALTH_UNCHECKED
from tribler_gui.tribler_action_menu import TriblerActionMenu
from tribler_gui.tribler_request_manager import TriblerNetworkRequest
from tribler_gui.utilities import connect, get_health
from tribler_gui.utilities import connect, dict_item_is_any_of, get_health

HEALTHCHECK_DELAY_MS = 500

Expand Down Expand Up @@ -115,7 +115,7 @@ def _on_selection_changed(self, selected, deselected):
return

data_item = selected_indices[-1].model().data_items[selected_indices[-1].row()]
if 'type' in data_item and data_item['type'] != REGULAR_TORRENT:
if not dict_item_is_any_of(data_item, 'type', [REGULAR_TORRENT]):
return

# Trigger health check if necessary
Expand All @@ -132,6 +132,9 @@ def _on_selection_changed(self, selected, deselected):
class HealthCheckerMixin:
def check_torrent_health(self, data_item, forced=False):
# TODO: stop triggering multiple checks over a single infohash by e.g. selection and click signals
if not dict_item_is_any_of(data_item, 'type', [REGULAR_TORRENT]):
return

infohash = data_item[u'infohash']

if u'health' not in self.model.column_position:
Expand Down Expand Up @@ -287,7 +290,7 @@ def add_menu_item(self, menu, name, item_index, callback):
def selection_can_be_added_to_channel(self):
for row in self.table_view.selectionModel().selectedRows():
data_item = row.model().data_items[row.row()]
if 'type' in data_item and data_item['type'] in (REGULAR_TORRENT, CHANNEL_TORRENT, COLLECTION_NODE):
if dict_item_is_any_of(data_item, 'type', [REGULAR_TORRENT, CHANNEL_TORRENT, COLLECTION_NODE]):
return True
return False

Expand Down

0 comments on commit 47dbcfd

Please sign in to comment.