From 8794bdf888aa1d2065edc8dc91f1ed24a6618c4b Mon Sep 17 00:00:00 2001 From: "V.G. Bulavintsev" Date: Thu, 13 Dec 2018 17:31:51 +0100 Subject: [PATCH] enable chant edit by default --- Tribler/Core/Config/tribler_config.py | 6 -- .../channels/channels_discovered_endpoint.py | 47 ++++----- .../restapi/channels/my_channel_endpoint.py | 96 ++++++------------- .../Test/Core/Config/test_tribler_config.py | 2 - .../Channels/test_channels_endpoint.py | 1 - 5 files changed, 48 insertions(+), 104 deletions(-) diff --git a/Tribler/Core/Config/tribler_config.py b/Tribler/Core/Config/tribler_config.py index 0aa3eb383f0..1b1596f7526 100644 --- a/Tribler/Core/Config/tribler_config.py +++ b/Tribler/Core/Config/tribler_config.py @@ -138,12 +138,6 @@ def get_chant_channels_dir(self): path = self.config['chant']['channels_dir'] return path if os.path.isabs(path) else os.path.join(self.get_state_dir(), path) - def set_chant_channel_edit(self, value): - self.config['chant']['channel_edit'] = bool(value) - - def get_chant_channel_edit(self): - return self.config['chant']['channel_edit'] - # General def set_family_filter_enabled(self, value): self.config['general']['family_filter'] = bool(value) diff --git a/Tribler/Core/Modules/restapi/channels/channels_discovered_endpoint.py b/Tribler/Core/Modules/restapi/channels/channels_discovered_endpoint.py index eae9a8ab8c4..76a54ee2f07 100644 --- a/Tribler/Core/Modules/restapi/channels/channels_discovered_endpoint.py +++ b/Tribler/Core/Modules/restapi/channels/channels_discovered_endpoint.py @@ -1,20 +1,21 @@ +from __future__ import absolute_import + from pony.orm import db_session from twisted.web import http +import Tribler.Core.Utilities.json_util as json from Tribler.Core.Modules.restapi.channels.base_channels_endpoint import BaseChannelsEndpoint from Tribler.Core.Modules.restapi.channels.channels_playlists_endpoint import ChannelsPlaylistsEndpoint from Tribler.Core.Modules.restapi.channels.channels_rss_endpoint import ChannelsRssFeedsEndpoint, \ ChannelsRecheckFeedsEndpoint from Tribler.Core.Modules.restapi.channels.channels_torrents_endpoint import ChannelsTorrentsEndpoint -from Tribler.Core.Modules.restapi.util import convert_db_channel_to_json, convert_channel_metadata_to_tuple -from Tribler.Core.exceptions import DuplicateChannelNameError -import Tribler.Core.Utilities.json_util as json class ChannelsDiscoveredEndpoint(BaseChannelsEndpoint): """ This class is responsible for requests regarding the discovered channels. """ + def getChild(self, path, request): return ChannelsDiscoveredSpecificEndpoint(self.session, path) @@ -51,35 +52,21 @@ def render_PUT(self, request): if 'description' not in parameters or len(parameters['description']) == 0: description = u'' else: - description = unicode(parameters['description'][0], 'utf-8') - - if self.session.config.get_chant_channel_edit(): - my_key = self.session.trustchain_keypair - my_channel_id = my_key.pub().key_to_bin() - - # Do not allow to add a channel twice - if self.session.lm.mds.get_my_channel(): - request.setResponseCode(http.INTERNAL_SERVER_ERROR) - return json.dumps({"error": "channel already exists"}) - - title = unicode(parameters['name'][0], 'utf-8') - self.session.lm.mds.ChannelMetadata.create_channel(title, description) - return json.dumps({ - "added": str(my_channel_id).encode("hex"), - }) - - if 'mode' not in parameters or len(parameters['mode']) == 0: - # By default, the mode of the new channel is closed. - mode = u'closed' - else: - mode = unicode(parameters['mode'][0], 'utf-8') + description = str(parameters['description'][0]).encode('utf-8') + + my_key = self.session.trustchain_keypair + my_channel_id = my_key.pub().key_to_bin() - try: - channel_id = self.session.create_channel(unicode(parameters['name'][0], 'utf-8'), description, mode) - except DuplicateChannelNameError as ex: - return BaseChannelsEndpoint.return_500(self, request, ex) + # Do not allow to add a channel twice + if self.session.lm.mds.get_my_channel(): + request.setResponseCode(http.INTERNAL_SERVER_ERROR) + return json.dumps({"error": "channel already exists"}) - return json.dumps({"added": channel_id}) + title = str(parameters['name'][0]).encode('utf-8') + self.session.lm.mds.ChannelMetadata.create_channel(title, description) + return json.dumps({ + "added": str(my_channel_id).encode("hex"), + }) class ChannelsDiscoveredSpecificEndpoint(BaseChannelsEndpoint): diff --git a/Tribler/Core/Modules/restapi/channels/my_channel_endpoint.py b/Tribler/Core/Modules/restapi/channels/my_channel_endpoint.py index 449a1910ffb..a6d002c9eab 100644 --- a/Tribler/Core/Modules/restapi/channels/my_channel_endpoint.py +++ b/Tribler/Core/Modules/restapi/channels/my_channel_endpoint.py @@ -1,4 +1,5 @@ import os +from binascii import hexlify from pony.orm import db_session from twisted.web import http @@ -42,33 +43,20 @@ def render_GET(self, request): :statuscode 404: if your channel has not been created (yet). """ - if self.session.config.get_chant_channel_edit(): - my_channel_id = self.session.trustchain_keypair.pub().key_to_bin() - with db_session: - my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id) - - if not my_channel: - request.setResponseCode(http.NOT_FOUND) - return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG}) - - my_channel = my_channel.to_dict() - return json.dumps({ - 'mychannel': { - 'identifier': str(my_channel["public_key"]).encode('hex'), - 'name': my_channel["title"], - 'description': my_channel["tags"], - 'chant': True - }}) - else: - my_channel_id = self.channel_db_handler.getMyChannelId() - if my_channel_id is None: + my_channel_id = self.session.trustchain_keypair.pub().key_to_bin() + with db_session: + my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id) + + if not my_channel: request.setResponseCode(http.NOT_FOUND) return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG}) - my_channel = self.channel_db_handler.getChannel(my_channel_id) - - return json.dumps({'mychannel': {'identifier': my_channel[1].encode('hex'), 'name': my_channel[2], - 'description': my_channel[3]}}) + my_channel = my_channel.to_dict() + return json.dumps({ + 'mychannel': { + 'identifier': hexlify(str(my_channel["public_key"])), + 'name': my_channel["title"], + 'description': my_channel["tags"]}}) def render_POST(self, request): """ @@ -100,50 +88,28 @@ def render_POST(self, request): request.setResponseCode(http.BAD_REQUEST) return json.dumps({"error": 'channel name cannot be empty'}) - if self.session.config.get_chant_channel_edit(): - with db_session: - modified = False - my_key = self.session.trustchain_keypair - my_channel_id = my_key.pub().key_to_bin() - my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id) - - if not my_channel: - request.setResponseCode(http.NOT_FOUND) - return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG}) - - if get_parameter(parameters, 'name'): - my_channel.update_metadata(update_dict={ - "tags": unicode(get_parameter(parameters, 'description'), 'utf-8'), - "title": unicode(get_parameter(parameters, 'name'), 'utf-8') - }) - modified = True - - if get_parameter(parameters, 'commit_changes') and my_channel.staged_entries_list: - # Update torrent if we have uncommitted content in the channel - my_channel.commit_channel_torrent() - torrent_path = os.path.join(self.session.lm.mds.channels_dir, my_channel.dir_name + ".torrent") - self.session.lm.updated_my_channel(torrent_path) - modified = True + with db_session: + modified = False + my_key = self.session.trustchain_keypair + my_channel_id = my_key.pub().key_to_bin() + my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id) - return json.dumps({'modified': modified}) - else: - my_channel_id = self.channel_db_handler.getMyChannelId() - if my_channel_id is None: + if not my_channel: request.setResponseCode(http.NOT_FOUND) return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG}) - channel_community = self.get_community_for_channel_id(my_channel_id) - if channel_community is None: - return BaseChannelsEndpoint.return_404(request, - message="the community for the your channel cannot be found") + if get_parameter(parameters, 'name'): + my_channel.update_metadata(update_dict={ + "tags": str(get_parameter(parameters, 'description')).encode('utf-8'), + "title": str(get_parameter(parameters, 'name')).encode('utf-8') + }) + modified = True - my_channel = self.channel_db_handler.getChannel(my_channel_id) - changes = {} - if my_channel[2] != get_parameter(parameters, 'name'): - changes['name'] = unicode(get_parameter(parameters, 'name'), 'utf-8') - if my_channel[3] != get_parameter(parameters, 'description'): - changes['description'] = unicode(get_parameter(parameters, 'description'), 'utf-8') + if get_parameter(parameters, 'commit_changes') and my_channel.staged_entries_list: + # Update torrent if we have uncommitted content in the channel + my_channel.commit_channel_torrent() + torrent_path = os.path.join(self.session.lm.mds.channels_dir, my_channel.dir_name + ".torrent") + self.session.lm.updated_my_channel(torrent_path) + modified = True - channel_community.modifyChannel(changes) - - return json.dumps({'modified': True}) + return json.dumps({'modified': modified}) diff --git a/Tribler/Test/Core/Config/test_tribler_config.py b/Tribler/Test/Core/Config/test_tribler_config.py index ffb0ca4b406..ac3eafedbd3 100644 --- a/Tribler/Test/Core/Config/test_tribler_config.py +++ b/Tribler/Test/Core/Config/test_tribler_config.py @@ -282,8 +282,6 @@ def test_get_set_chant_methods(self): """ self.tribler_config.set_chant_enabled(False) self.assertFalse(self.tribler_config.get_chant_enabled()) - self.tribler_config.set_chant_channel_edit(True) - self.assertTrue(self.tribler_config.get_chant_channel_edit()) self.tribler_config.set_chant_channels_dir('test') self.assertEqual(self.tribler_config.get_chant_channels_dir(), os.path.join(self.tribler_config.get_state_dir(), 'test')) diff --git a/Tribler/Test/Core/Modules/RestApi/Channels/test_channels_endpoint.py b/Tribler/Test/Core/Modules/RestApi/Channels/test_channels_endpoint.py index b70c87b6fa5..80dcfbb02b0 100644 --- a/Tribler/Test/Core/Modules/RestApi/Channels/test_channels_endpoint.py +++ b/Tribler/Test/Core/Modules/RestApi/Channels/test_channels_endpoint.py @@ -44,7 +44,6 @@ def setUpPreSession(self): super(AbstractTestChantEndpoint, self).setUpPreSession() self.config.set_libtorrent_enabled(True) self.config.set_chant_enabled(True) - self.config.set_chant_channel_edit(True) @db_session def create_my_channel(self, name, description):