diff --git a/src/_cutecoin_test/models/test_community.py b/src/_cutecoin_test/models/test_community.py index 14aaa75ef23578afe115db545bdf3a20c5425f04..bacc9808a262c22d2aaf835fe8e10f5a395cbee2 100644 --- a/src/_cutecoin_test/models/test_community.py +++ b/src/_cutecoin_test/models/test_community.py @@ -159,7 +159,7 @@ class Test_Community(): def test_community_jsonify(self, monkeypatch): monkeypatch.setattr(ucoin.hdc.amendments.Promoted, '__get__', patch_amendment_Promoted_get) - main_node = Node(trust=True, hoster=True, + main_node = Node.create(trust=True, hoster=True, server="192.168.100.10", port=3800) community = Community.create(main_node) wallets = Wallets() diff --git a/src/_cutecoin_test/models/test_node.py b/src/_cutecoin_test/models/test_node.py index eeac9ad1a280e4a9d551e46bd4d6eb7e3003b9aa..2fc097ae39038288c51cc829b44c8c4a16acb69c 100644 --- a/src/_cutecoin_test/models/test_node.py +++ b/src/_cutecoin_test/models/test_node.py @@ -38,7 +38,7 @@ def patch_peer_get(*args, **kwargs): "port": "3800", "signature": "-----BEGIN PGP SIGNATURE----- ... -----END PGP SIGNATURE-----" } - + def patch_downstream_get(*args, **kwargs): return { "peers": [ @@ -48,37 +48,36 @@ def patch_downstream_get(*args, **kwargs): } -class Test_Node(): +class Test_Node.create(): def test_peers(self, monkeypatch): monkeypatch.setattr(ucoin.ucg.peering.Peers, '__get__', patch_peers_get) - node = Node("192.168.100.12", 3800) + node = Node.create("192.168.100.12", 3800) assert (peer for peer in node.peers() if peer["ipv4"] == "192.168.100.10") assert (peer for peer in node.peers() if peer["ipv4"] == "192.168.100.11") def test_peering(self, monkeypatch): monkeypatch.setattr(ucoin.ucg.peering.Peer, '__get__', patch_peer_get) - node = Node("192.168.100.12", 3800) + node = Node.create("192.168.100.12", 3800) peering = node.peering() assert peering["ipv4"] == "192.168.100.10" assert peering["port"] == str(3800) def test_eq(self, monkeypatch): - node1 = Node("192.168.100.12", 3800) - node2 = Node("192.168.100.13", 3800) - node3 = Node("192.168.100.12", 3801) - node4 = Node("192.168.100.12", 3800) + node1 = Node.create("192.168.100.12", 3800) + node2 = Node.create("192.168.100.13", 3800) + node3 = Node.create("192.168.100.12", 3801) + node4 = Node.create("192.168.100.12", 3800) assert node1 != node2 assert node1 != node3 assert node1 == node4 - + def test_downstream(self, monkeypatch): monkeypatch.setattr(ucoin.ucg.peering.peers.DownStream, '__get__', patch_downstream_get) - node = Node("192.168.100.12", 3800) + node = Node.create("192.168.100.12", 3800) downstream = node.downstream_peers() assert downstream[0].server == "11.11.11.11" and downstream[0].port == 8881 assert downstream[1].server == "11.11.11.12" and downstream[1].port == 8882 - \ No newline at end of file diff --git a/src/cutecoin/gui/mainWindow.py b/src/cutecoin/gui/mainWindow.py index f97d676c72f155a29a7a2ab682a618dc8f0103ae..97e32f8296d6ee23dbbebaf774606aae0c0f07ea 100644 --- a/src/cutecoin/gui/mainWindow.py +++ b/src/cutecoin/gui/mainWindow.py @@ -87,7 +87,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.core.current_account)) self.tabs_communities.clear() - for community in self.core.current_account.communities.communities_list: + for community in self.core.current_account.communities: tab_community = CommunityTabWidget( self.core.current_account, community) diff --git a/src/cutecoin/gui/processConfigureAccount.py b/src/cutecoin/gui/processConfigureAccount.py index e8c7bc0bffd7a4ada35d5ffff7fade697c49822f..f74222dbdcdc55207deb9184457c696ca4e6b4e0 100644 --- a/src/cutecoin/gui/processConfigureAccount.py +++ b/src/cutecoin/gui/processConfigureAccount.py @@ -57,7 +57,7 @@ class StepPageCommunities(Step): ''' server = self.config_dialog.lineedit_server.text() port = self.config_dialog.spinbox_port.value() - default_node = Node(server, port, trust=True, hoster=True) + default_node = Node.create(server, port, trust=True, hoster=True) account = self.config_dialog.account self.config_dialog.community = account.communities.add_community( default_node) @@ -105,8 +105,8 @@ class ProcessConfigureAccount(QDialog, Ui_AccountConfigurationDialog): self.account = Account.create( available_keys[0]['keyid'], "", - Communities(), - Wallets()) + Communities.create(), + Wallets.create()) self.combo_keys_list.currentIndexChanged[ int].connect(self.key_changed) @@ -140,7 +140,7 @@ class ProcessConfigureAccount(QDialog, Ui_AccountConfigurationDialog): self.list_communities.setModel(CommunitiesListModel(self.account)) def open_process_edit_community(self, index): - community = self.account.communities.communities_list[index.row()] + community = self.account.communities[index.row()] dialog = ProcessConfigureCommunity(self.account, community) dialog.button_box.accepted.connect(self.action_edit_community) dialog.exec_() diff --git a/src/cutecoin/gui/processConfigureCommunity.py b/src/cutecoin/gui/processConfigureCommunity.py index 0778af06d28203cd8b924dd7093f87cbb2d53bf5..0a099fcb9b0d43463a3812715e2c2494829bd6e2 100644 --- a/src/cutecoin/gui/processConfigureCommunity.py +++ b/src/cutecoin/gui/processConfigureCommunity.py @@ -43,7 +43,7 @@ class StepPageInit(Step): ''' server = self.config_dialog.lineedit_server.text() port = self.config_dialog.spinbox_port.value() - default_node = Node(server, port, trust=True, hoster=True) + default_node = Node.create(server, port, trust=True, hoster=True) account = self.config_dialog.account self.config_dialog.community = account.add_community(default_node) self.config_dialog.nodes.append(default_node) @@ -161,7 +161,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog): server = self.lineedit_server.text() port = self.spinbox_port.value() if self.community is not None: - self.nodes.append(Node(server, port, trust=True)) + self.nodes.append(Node.create(server, port, trust=True)) self.tree_nodes.setModel(NodesTreeModel(self.community, self.nodes)) diff --git a/src/cutecoin/gui/transferMoneyDialog.py b/src/cutecoin/gui/transferMoneyDialog.py index 444da4d6f743ecca654a7eeb8206eff282422062..c7c2344cd0c0df2c9aec0a4622bae550e81ef94a 100644 --- a/src/cutecoin/gui/transferMoneyDialog.py +++ b/src/cutecoin/gui/transferMoneyDialog.py @@ -31,7 +31,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): self.setupUi(self) self.sender = sender for wallet in sender.wallets: - self.combo_wallets.addItem(wallet.getText()) + self.combo_wallets.addItem(wallet.get_text()) for contact in sender.contacts: self.combo_contact.addItem(contact.name) @@ -76,12 +76,12 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): self.combo_contact.currentIndex()] if self.radio_node_address.isChecked(): - node = Node( + node = Node.create( self.edit_node_address.text(), int( self.edit_port.text())) else: # TODO: Manage trusted nodes - node = Node( + node = Node.create( self.edit_node_address.text(), int( self.edit_port.text())) diff --git a/src/cutecoin/models/account/__init__.py b/src/cutecoin/models/account/__init__.py index 079fa39f49d6198e1d8023eb4e0eda7de0f3ffef..943c36a048e40ae8075ad02a053aedee6e1f6deb 100644 --- a/src/cutecoin/models/account/__init__.py +++ b/src/cutecoin/models/account/__init__.py @@ -46,21 +46,15 @@ class Account(object): def load(cls, json_data): keyid = json_data['keyid'] name = json_data['name'] - communities = Communities() - wallets = Wallets() contacts = [] for contact_data in json_data['contacts']: contacts.append(Person.from_json(contact_data)) - account = cls(keyid, name, communities, wallets, contacts) - - for community_data in json_data['communities']: - account.communities.communities_list.append( - Community.load( - community_data, - account)) + wallets = Wallets.load(json_data['wallets']) + communities = Communities.load(json_data['communities']) + account = cls(keyid, name, communities, wallets, contacts) return account def __eq__(self, other): @@ -119,7 +113,7 @@ class Account(object): return sent def quality(self, community): - if community in self.communities.communities_list: + if community in self.communities: wallets = self.wallets.community_wallets(community.currency) return community.person_quality(wallets, self.fingerprint()) else: @@ -134,6 +128,7 @@ class Account(object): def jsonify(self): data = {'name': self.name, 'keyid': self.keyid, - 'communities': self.communities.jsonify(self.wallets), + 'communities': self.communities.jsonify(), + 'wallets': self.wallets.jsonify(), 'contacts': self.jsonify_contacts()} return data diff --git a/src/cutecoin/models/account/communities/__init__.py b/src/cutecoin/models/account/communities/__init__.py index 13995afeb75888a4ebcca1eca2b7e270eb8d19ab..bd172932ae4e4ef58b94c78f96399511d6d5b994 100644 --- a/src/cutecoin/models/account/communities/__init__.py +++ b/src/cutecoin/models/account/communities/__init__.py @@ -14,27 +14,53 @@ class Communities(object): The list of the communities an account is member of. ''' - def __init__(self): + def __init__(self, _communities_list): ''' Constructor ''' - self.communities_list = [] + self._communities_list = [] + + @classmethod + def create(cls): + return cls([]) + + @classmethod + def load(cls, json_data): + _communities_list = [] + for community_data in json_data: + _communities_list.append(Community.load(community_data)) + return cls(_communities_list) + + def __iter__(self): + return self._communities_list.__iter__() + + def __contains__(self, wallet): + return wallet in self._communities_list + + def __reverse__(self): + return self._communities_list.__reverse__() + + def __len__(self): + return len(self._communities_list) + + def __getitem__(self, key): + return self._communities_list[key] def add_community(self, wallets): ''' Add a community with a mainNode ''' community = Community.create(wallets) - if community not in self.communities_list: - self.communities_list.append(community) + if community not in self._communities_list: + self._communities_list.append(community) return community return None - def jsonify(self, wallets): + def jsonify(self): ''' Return the list of communities in a key:value form. ''' data = [] - for community in self.communities_list: - data.append(community.jsonify(wallets)) + for community in self._communities_list: + data.append(community.jsonify()) return data diff --git a/src/cutecoin/models/account/communities/listModel.py b/src/cutecoin/models/account/communities/listModel.py index 83e2b047451273bb0ffeff80d723635fcc9564aa..b039663929c33ae04230126b1eb0f2f5fd8e20c8 100644 --- a/src/cutecoin/models/account/communities/listModel.py +++ b/src/cutecoin/models/account/communities/listModel.py @@ -21,13 +21,13 @@ class CommunitiesListModel(QAbstractListModel): self.communities = account.communities def rowCount(self, parent): - return len(self.communities.communities_list) + return len(self.communities) def data(self, index, role): if role == Qt.DisplayRole: row = index.row() - value = self.communities.communities_list[row].name() + value = self.communities[row].name() return value def flags(self, index): diff --git a/src/cutecoin/models/account/wallets/__init__.py b/src/cutecoin/models/account/wallets/__init__.py index bb6eb10910913eb104197e3b90ec3205c96834b2..9dd15ef9f9bad6173c117f8e4763f952d468dcbe 100644 --- a/src/cutecoin/models/account/wallets/__init__.py +++ b/src/cutecoin/models/account/wallets/__init__.py @@ -14,11 +14,22 @@ class Wallets(object): The list of the wallets owned by an account. ''' - def __init__(self, _wallets_list=[]): + def __init__(self, wallets_list): ''' Constructor ''' - self._wallets_list = _wallets_list + self._wallets_list = wallets_list + + @classmethod + def create(cls): + return cls([]) + + @classmethod + def load(cls, json_data): + wallets_list = [] + for wallet_data in json_data: + wallets_list.append(Wallet.load(wallet_data)) + return cls(wallets_list) def __iter__(self): return self._wallets_list.__iter__() @@ -51,17 +62,6 @@ class Wallets(object): def community_wallets(self, currency): return Wallets([w for w in self._wallets_list if w.currency == currency]) - def jsonify(self, community): - ''' - Return the list of wallets in a key:value form. - ''' - community_wallets = [ - w for w in self._wallets_list if w.community == community] - data = [] - for wallet in community_wallets: - data.append(wallet.jsonify()) - return data - def request(self, request, get_args={}): for wallet in self._wallets_list: try: @@ -77,3 +77,12 @@ class Wallets(object): except: pass return response + + def jsonify(self): + ''' + Return the list of wallets in a key:value form. + ''' + data = [] + for wallet in self._wallets_list: + data.append(wallet.jsonify()) + return data diff --git a/src/cutecoin/models/node/__init__.py b/src/cutecoin/models/node/__init__.py index 2de0ebf3ace538f33347d2809ad4dfccc32b3ef9..cb27cf01828ac879328bfdf02bf7d93b1b185f62 100644 --- a/src/cutecoin/models/node/__init__.py +++ b/src/cutecoin/models/node/__init__.py @@ -13,7 +13,7 @@ class Node(object): A ucoin node using BMA protocol ''' - def __init__(self, server, port, trust=False, hoster=False): + def __init__(self, server, port, trust, hoster): ''' Constructor ''' @@ -22,6 +22,18 @@ class Node(object): self.trust = trust self.hoster = hoster + @classmethod + def create(cls, server, port, trust=False, hoster=False): + return cls(server, port, trust, hoster) + + @classmethod + def load(cls, json_data): + server = json_data['server'] + port = json_data['port'] + trust = json_data['trust'] + hoster = json_data['hoster'] + return cls(server, port, trust, hoster) + def __eq__(self, other): return (self.server == other.server and self.port == other.port) @@ -39,7 +51,7 @@ class Node(object): peers = [] for peer in ucoin.network.peering.peers.DownStream().get()['peers']: - node = Node(peer['ipv4'], peer['port']) + node = Node.create(peer['ipv4'], peer['port']) peers.append(node) return peers diff --git a/src/cutecoin/models/wallet/__init__.py b/src/cutecoin/models/wallet/__init__.py index fb979ca1435563a58054dcf4f2dd0a3b46f5a4b8..843af0b5008192900bd057d6f2a3ce6c15924284 100644 --- a/src/cutecoin/models/wallet/__init__.py +++ b/src/cutecoin/models/wallet/__init__.py @@ -40,12 +40,17 @@ class Wallet(object): @classmethod def load(cls, json_data): coins = [] - for coinData in json_data['coins']: - coins.append(Coin.from_id(coinData['coin'])) + nodes = [] + for coin_data in json_data['coins']: + coins.append(Coin.from_id(coin_data['coin'])) + + for node_data in json_data['nodes']: + nodes.append(Node.load(node_data)) + fingerprint = json_data['fingerprint'] name = json_data['name'] currency = json_data['currency'] - return cls(fingerprint, coins, currency, name) + return cls(fingerprint, coins, currency, nodes, name) def __eq__(self, other): return (self.community == other.community) @@ -134,7 +139,7 @@ class Wallet(object): for peer in next_node.peers(): # Look for next node informations found = self._search_node_by_fingerprint( - node_fg, Node( + node_fg, Node.create( peer['ipv4'], int( peer['port'])), traversed_nodes) if found is not None: @@ -180,9 +185,15 @@ class Wallet(object): data.append({'coin': coin.get_id()}) return data + def jsonify_nodes_list(self): + data = [] + for node in self.nodes: + data.append(node.jsonify()) + return data + def jsonify(self): - #TODO: Jsonify nodes return {'coins': self.jsonify_coins_list(), 'fingerprint': self.fingerprint, 'name': self.name, - 'currency': self.currency} + 'currency': self.currency, + 'nodes': self.jsonify_nodes_list()}