From b566c4cdc027da9bfa3d800b86ccd224cdc30c03 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sat, 31 Jan 2015 10:02:33 +0100
Subject: [PATCH] Added referentials "quantitative zerosum" and "relative
 zerosum"

---
 src/cutecoin/core/account.py     | 22 ++++++++++++++++++----
 src/cutecoin/core/community.py   | 10 ++++++++++
 src/cutecoin/gui/mainwindow.py   |  5 ++++-
 src/cutecoin/models/txhistory.py |  4 ++--
 src/cutecoin/models/wallets.py   |  2 +-
 5 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index 04021a4e..2a8f01a3 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -21,7 +21,7 @@ from .person import Person
 from ..tools.exceptions import NoPeerAvailable
 
 
-def units(units, community):
+def quantitative(units, community):
     return units
 
 
@@ -31,6 +31,19 @@ def relative(units, community):
     return relative_value
 
 
+def quantitative_zerosum(units, community):
+    median = community.monetary_mass / community.nb_members
+    return units - median
+
+
+def relative_zerosum(units, community):
+    median = community.monetary_mass / community.nb_members
+    ud = community.dividend()
+    relative_value = units / float(ud)
+    relative_median = median / ud
+    return relative_value - relative_median
+
+
 class Account(object):
 
     '''
@@ -38,8 +51,10 @@ class Account(object):
     Each account has only one key, and a key can
     be locally referenced by only one account.
     '''
-    referentials = {'Units': (units, '{0}'),
-                    'UD': (relative, 'ud {0}')
+    referentials = {'Units': (quantitative, '{0}'),
+                    'UD': (relative, 'ud {0}'),
+                    'Quant Z-sum': (quantitative_zerosum, 'q0 {0}'),
+                    'Relat Z-sum': (relative_zerosum, 'r0 {0}')
                     }
 
     def __init__(self, salt, pubkey, name, communities, wallets, contacts,
@@ -106,7 +121,6 @@ class Account(object):
     def add_contact(self, person):
         same_contact = [contact for contact in self.contacts if person.pubkey == contact.pubkey]
         if len(same_contact) == 0:
-            print("add contact")
             self.contacts.append(person)
             return True
         return False
diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index 0eb1330c..52d4e787 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -150,6 +150,7 @@ class Community(object):
         else:
             return 1
 
+    @property
     def monetary_mass(self):
         try:
             block = self.request(bma.blockchain.Current)
@@ -158,6 +159,15 @@ class Community(object):
             if '404' in e:
                 return 0
 
+    @property
+    def nb_members(self):
+        try:
+            block = self.request(bma.blockchain.Current)
+            return block['membersCount']
+        except ValueError as e:
+            if '404' in e:
+                return 0
+
     def _peering_traversal(self, peer, found_peers, traversed_pubkeys):
         logging.debug("Read {0} peering".format(peer.pubkey))
         traversed_pubkeys.append(peer.pubkey)
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index f21d2d38..ab7e74bd 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -191,8 +191,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
                                                   != self.app.default_account)
             self.password_asker = PasswordAskerDialog(self.app.current_account)
 
-            self.combo_referential.addItems(Account.referentials.keys())
+            self.combo_referential.blockSignals(True)
+            self.combo_referential.addItems(sorted(Account.referentials.keys()))
             self.combo_referential.setEnabled(True)
+            self.combo_referential.blockSignals(False)
+            self.combo_referential.setCurrentText(self.app.current_account.referential)
             self.menu_contacts.setEnabled(True)
             self.action_configure_parameters.setEnabled(True)
             self.menu_actions.setEnabled(True)
diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py
index f296d714..a8affe82 100644
--- a/src/cutecoin/models/txhistory.py
+++ b/src/cutecoin/models/txhistory.py
@@ -92,7 +92,7 @@ class HistoryTableModel(QAbstractTableModel):
         amount_ref = self.account.units_to_ref(amount, self.community)
         ref_name = self.account.ref_name(self.community.short_currency)
 
-        return (date.date(), sender, "", "{0} {1}".format(amount_ref, ref_name),
+        return (date.date(), sender, "", "{0:.2f} {1}".format(amount_ref, ref_name),
                 comment)
 
     def data_sent(self, tx):
@@ -116,7 +116,7 @@ class HistoryTableModel(QAbstractTableModel):
         amount_ref = self.account.units_to_ref(-amount, self.community)
         ref_name = self.account.ref_name(self.community.short_currency)
 
-        return (date.date(), receiver, "{0} {1}".format(amount_ref, ref_name),
+        return (date.date(), receiver, "{0:.2f} {1}".format(amount_ref, ref_name),
                 "", comment)
 
     def data(self, index, role):
diff --git a/src/cutecoin/models/wallets.py b/src/cutecoin/models/wallets.py
index 10b462f0..3887b36d 100644
--- a/src/cutecoin/models/wallets.py
+++ b/src/cutecoin/models/wallets.py
@@ -34,7 +34,7 @@ class WalletsListModel(QAbstractListModel):
             ref_amount = self.account.units_to_ref(amount, self.community)
             ref_name = self.account.ref_name(self.community.currency)
             return """{0}
-{1} {2}""".format(w.name, ref_amount, ref_name)
+{1:.2f} {2}""".format(w.name, ref_amount, ref_name)
         elif role == Qt.EditRole:
             return self.wallets[row].name
 
-- 
GitLab