Skip to content

Commit

Permalink
Added edit metadata GUI elements
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 authored and drew2a committed Nov 1, 2022
1 parent 77932e2 commit 86cc5bc
Show file tree
Hide file tree
Showing 18 changed files with 590 additions and 154 deletions.
2 changes: 1 addition & 1 deletion src/tribler/core/components/knowledge/db/knowledge_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from dataclasses import dataclass
from enum import IntEnum
from typing import Callable, Iterable, List, Optional, Set
from typing import Callable, Iterable, List, Optional, Set, Dict

from pony import orm
from pony.orm.core import Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def rqc(self) -> RemoteQueryCommunity:

@patch.object(RemoteQueryCommunity, 'knowledge_db', new=PropertyMock(return_value=None), create=True)
async def test_search_for_tags_no_db(self):
# test that in case of missed `tags_db`, function `search_for_tags` returns None
# test that in case of missed `knowledge_db`, function `search_for_tags` returns None
assert self.rqc.search_for_tags(tags=['tag']) is None

@patch.object(KnowledgeDatabase, 'get_subjects_intersection')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async def get_channel_contents(self, request):
contents_list.append(entry.to_simple_dict())
total = self.mds.get_total_count(**sanitized) if include_total else None
self.add_download_progress_to_metadata_list(contents_list)
self.add_tags_to_metadata_list(contents_list, hide_xxx=sanitized["hide_xxx"])
self.add_statements_to_metadata_list(contents_list, hide_xxx=sanitized["hide_xxx"])
response_dict = {
"results": contents_list,
"first": sanitized['first'],
Expand Down Expand Up @@ -504,7 +504,7 @@ async def get_popular_torrents_channel(self, request):
contents_list.append(entry.to_simple_dict())

self.add_download_progress_to_metadata_list(contents_list)
self.add_tags_to_metadata_list(contents_list, hide_xxx=sanitized["hide_xxx"])
self.add_statements_to_metadata_list(contents_list, hide_xxx=sanitized["hide_xxx"])
response_dict = {
"results": contents_list,
"first": sanitized['first'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import asdict
from typing import Optional

from pony.orm import db_session
Expand Down Expand Up @@ -38,11 +39,11 @@


class MetadataEndpointBase(RESTEndpoint):
def __init__(self, metadata_store: MetadataStore, *args, tags_db: KnowledgeDatabase = None,
def __init__(self, metadata_store: MetadataStore, *args, knowledge_db: KnowledgeDatabase = None,
tag_rules_processor: KnowledgeRulesProcessor = None, **kwargs):
super().__init__(*args, **kwargs)
self.mds = metadata_store
self.tags_db: Optional[KnowledgeDatabase] = tags_db
self.knowledge_db: Optional[KnowledgeDatabase] = knowledge_db
self.tag_rules_processor: Optional[KnowledgeRulesProcessor] = tag_rules_processor

@classmethod
Expand Down Expand Up @@ -84,13 +85,15 @@ def extract_tags(self, entry):
self._logger.info(f'Generated {generated} tags for {hexlify(entry.infohash)}')

@db_session
def add_tags_to_metadata_list(self, contents_list, hide_xxx=False):
if self.tags_db is None:
self._logger.error(f'Cannot add tags to metadata list: tags_db is not set in {self.__class__.__name__}')
def add_statements_to_metadata_list(self, contents_list, hide_xxx=False):
if self.knowledge_db is None:
self._logger.error(f'Cannot add statements to metadata list: '
f'knowledge_db is not set in {self.__class__.__name__}')
return
for torrent in contents_list:
if torrent['type'] == REGULAR_TORRENT:
tags = self.tags_db.get_objects(torrent["infohash"], predicate=ResourceType.TAG)
statements = [asdict(stmt) for stmt in self.knowledge_db.get_statements(torrent["infohash"])]
if hide_xxx:
tags = [tag.lower() for tag in tags if not default_xxx_filter.isXXX(tag, isFilename=False)]
torrent["tags"] = tags
statements = [stmt for stmt in statements if not default_xxx_filter.isXXX(stmt["object"],
isFilename=False)]
torrent["statements"] = statements
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def build_snippets(self, search_results: List[Dict]) -> List[Dict]:
content_to_torrents: Dict[str, list] = defaultdict(list)
for search_result in search_results:
with db_session:
content_items: List[str] = self.tags_db.get_objects(search_result["infohash"],
predicate=ResourceType.TITLE)
content_items: List[str] = self.knowledge_db.get_objects(search_result["infohash"],
predicate=ResourceType.TITLE)
if content_items:
for content_id in content_items:
content_to_torrents[content_id].append(search_result)
Expand Down Expand Up @@ -131,8 +131,8 @@ def search_db():
try:
with db_session:
if tags:
infohash_set = self.tags_db.get_subjects_intersection(set(tags), predicate=ResourceType.TAG,
case_sensitive=False)
infohash_set = self.knowledge_db.get_subjects_intersection(set(tags), predicate=ResourceType.TAG,
case_sensitive=False)
if infohash_set:
sanitized['infohash_set'] = {bytes.fromhex(s) for s in infohash_set}

Expand All @@ -141,7 +141,7 @@ def search_db():
self._logger.exception("Error while performing DB search: %s: %s", type(e).__name__, e)
return RESTResponse(status=HTTP_BAD_REQUEST)

self.add_tags_to_metadata_list(search_results, hide_xxx=sanitized["hide_xxx"])
self.add_statements_to_metadata_list(search_results, hide_xxx=sanitized["hide_xxx"])

if sanitized["first"] == 1: # Only show a snippet on top
search_results = self.build_snippets(search_results)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pytest

from tribler.core.components.gigachannel.community.gigachannel_community import NoChannelSourcesException
from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.components.libtorrent.torrentdef import TorrentDef
from tribler.core.components.metadata_store.category_filter.family_filter import default_xxx_filter
from tribler.core.components.metadata_store.db.orm_bindings.channel_node import NEW
Expand Down Expand Up @@ -49,7 +50,7 @@ def return_exc(*args, **kwargs):

mock_gigachannel_community.remote_select_channel_contents = return_exc
ep_args = [mock_dlmgr, mock_gigachannel_manager, mock_gigachannel_community, metadata_store]
ep_kwargs = {'tags_db': knowledge_db}
ep_kwargs = {'knowledge_db': knowledge_db}
collections_endpoint = ChannelsEndpoint(*ep_args, **ep_kwargs)
channels_endpoint = ChannelsEndpoint(*ep_args, **ep_kwargs)

Expand Down Expand Up @@ -713,7 +714,7 @@ async def test_get_my_channel_tags(metadata_store, mock_dlmgr_get_download, my_c

assert len(json_dict['results']) == 9
for item in json_dict['results']:
assert len(item["tags"]) >= 2
assert len(item["statements"]) >= 2


async def test_get_my_channel_tags_xxx(metadata_store, knowledge_db, mock_dlmgr_get_download, my_channel,
Expand All @@ -739,4 +740,6 @@ async def test_get_my_channel_tags_xxx(metadata_store, knowledge_db, mock_dlmgr_
)

assert len(json_dict['results']) == 1
assert len(json_dict['results'][0]["tags"]) == 1
print(json_dict)
tag_statements = [s for s in json_dict["results"][0]["statements"] if s["predicate"] == ResourceType.TAG]
assert len(tag_statements) == 1
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_extract_tags():
# see: https://github.com/Tribler/tribler/issues/6986
mds_endpoint = MetadataEndpointBase(
MagicMock(),
tags_db=MagicMock(),
knowledge_db=MagicMock(),
tag_rules_processor=MagicMock(
version=1
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def needle_in_haystack_mds(metadata_store):

@pytest.fixture
def rest_api(loop, needle_in_haystack_mds, aiohttp_client, knowledge_db):
channels_endpoint = SearchEndpoint(needle_in_haystack_mds, tags_db=knowledge_db)
channels_endpoint = SearchEndpoint(needle_in_haystack_mds, knowledge_db=knowledge_db)
app = Application()
app.add_subapp('/search', channels_endpoint.app)
return loop.run_until_complete(aiohttp_client(app))
Expand Down
19 changes: 15 additions & 4 deletions src/tribler/core/components/metadata_store/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def tag_torrent(infohash, tags_db, tags=None, suggested_tags=None):
if tag not in suggested_tags:
suggested_tags.append(tag)

def _add_operation(_tag, _op, _key):
operation = StatementOperation(subject_type=ResourceType.TORRENT, subject=infohash, predicate=ResourceType.TAG,
object=_tag, operation=_op, clock=0, creator_public_key=_key.pub().key_to_bin())
def _add_operation(_obj, _op, _key, _predicate=ResourceType.TAG):
operation = StatementOperation(subject_type=ResourceType.TORRENT, subject=infohash, predicate=_predicate,
object=_obj, operation=_op, clock=0, creator_public_key=_key.pub().key_to_bin())
operation.clock = tags_db.get_clock(operation) + 1
tags_db.add_operation(operation, b"")

# Give each torrent some tags
# Give the torrent some tags
for tag in tags:
for key in [random_key_1, random_key_2]: # Each tag should be proposed by two unique users
_add_operation(tag, Operation.ADD, key)
Expand All @@ -73,6 +73,17 @@ def _add_operation(_tag, _op, _key):
_add_operation(tag, Operation.ADD, random_key_3)
_add_operation(tag, Operation.REMOVE, random_key_2)

# Give the torrent some simple attributes
random_title = generate_title(2)
random_year = f"{random.randint(1990, 2040)}"
random_description = generate_title(5)
random_lang = random.choice(["english", "russian", "dutch", "klingon", "valyerian"])
for key in [random_key_1, random_key_2]: # Each statement should be proposed by two unique users
_add_operation(random_title, Operation.ADD, key, _predicate=ResourceType.TITLE)
_add_operation(random_year, Operation.ADD, key, _predicate=ResourceType.DATE)
_add_operation(random_description, Operation.ADD, key, _predicate=ResourceType.DESCRIPTION)
_add_operation(random_lang, Operation.ADD, key, _predicate=ResourceType.LANGUAGE)


@db_session
def generate_torrent(metadata_store, tags_db, parent):
Expand Down
8 changes: 4 additions & 4 deletions src/tribler/core/components/restapi/restapi_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ async def run(self):
self.maybe_add('/libtorrent', LibTorrentEndpoint, libtorrent_component.download_manager)
self.maybe_add('/torrentinfo', TorrentInfoEndpoint, libtorrent_component.download_manager)
self.maybe_add('/metadata', MetadataEndpoint, torrent_checker, metadata_store_component.mds,
tags_db=knowledge_component.knowledge_db,
knowledge_db=knowledge_component.knowledge_db,
tag_rules_processor=knowledge_component.rules_processor)
self.maybe_add('/channels', ChannelsEndpoint, libtorrent_component.download_manager, gigachannel_manager,
gigachannel_component.community, metadata_store_component.mds,
tags_db=knowledge_component.knowledge_db,
knowledge_db=knowledge_component.knowledge_db,
tag_rules_processor=knowledge_component.rules_processor)
self.maybe_add('/collections', ChannelsEndpoint, libtorrent_component.download_manager, gigachannel_manager,
gigachannel_component.community, metadata_store_component.mds,
tags_db=knowledge_component.knowledge_db,
knowledge_db=knowledge_component.knowledge_db,
tag_rules_processor=knowledge_component.rules_processor)
self.maybe_add('/search', SearchEndpoint, metadata_store_component.mds,
tags_db=knowledge_component.knowledge_db)
knowledge_db=knowledge_component.knowledge_db)
self.maybe_add('/remote_query', RemoteQueryEndpoint, gigachannel_component.community,
metadata_store_component.mds)
self.maybe_add('/knowledge', KnowledgeEndpoint, db=knowledge_component.knowledge_db,
Expand Down
92 changes: 0 additions & 92 deletions src/tribler/gui/dialogs/addtagsdialog.py

This file was deleted.

Loading

0 comments on commit 86cc5bc

Please sign in to comment.