From 794fb3737b3f53e066699080551f426da67e733e Mon Sep 17 00:00:00 2001
From: inso <insomniak.fr@gmaiL.com>
Date: Fri, 19 May 2017 08:54:46 +0200
Subject: [PATCH] Fix pages count

---
 .../gui/navigation/txhistory/controller.py    |  4 +-
 src/sakia/gui/navigation/txhistory/model.py   |  2 +-
 .../gui/navigation/txhistory/sql_adapter.py   | 81 +++++++++++--------
 3 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/src/sakia/gui/navigation/txhistory/controller.py b/src/sakia/gui/navigation/txhistory/controller.py
index 4737d262..80d8496e 100644
--- a/src/sakia/gui/navigation/txhistory/controller.py
+++ b/src/sakia/gui/navigation/txhistory/controller.py
@@ -73,7 +73,7 @@ class TxHistoryController(QObject):
         if len(received_list) > 0:
             localized_amount = await self.model.received_amount(received_list)
             text = self.tr("Received {amount} from {number} transfers").format(amount=localized_amount,
-                                                                           number=len(received_list))
+                                                                               number=len(received_list))
             if self.model.notifications():
                 toast.display(self.tr("New transactions received"), text)
 
@@ -108,3 +108,5 @@ class TxHistoryController(QObject):
             self.view.table_history.model().set_period(ts_from, ts_to)
 
             self.refresh_balance()
+            self.refresh_pages()
+
diff --git a/src/sakia/gui/navigation/txhistory/model.py b/src/sakia/gui/navigation/txhistory/model.py
index fbdc1fd3..4a0b674c 100644
--- a/src/sakia/gui/navigation/txhistory/model.py
+++ b/src/sakia/gui/navigation/txhistory/model.py
@@ -84,7 +84,7 @@ class TxHistoryModel(QObject):
         :return: minimum and maximum datetime
         """
         minimum_datetime = QDateTime()
-        minimum_datetime.setTime_t(0)
+        minimum_datetime.setTime_t(1488322800) # First of may 2017
         tomorrow_datetime = QDateTime().currentDateTime().addDays(1)
         return minimum_datetime, tomorrow_datetime
 
diff --git a/src/sakia/gui/navigation/txhistory/sql_adapter.py b/src/sakia/gui/navigation/txhistory/sql_adapter.py
index 5a73f786..46b79114 100644
--- a/src/sakia/gui/navigation/txhistory/sql_adapter.py
+++ b/src/sakia/gui/navigation/txhistory/sql_adapter.py
@@ -1,44 +1,54 @@
+import math
 import attr
 
 
 TX_HISTORY_REQUEST = """
 SELECT 
-    transactions.ts,
-    transactions.pubkey,
-    total_amount((amount * -1), amountbase) as amount,
-    transactions.comment ,
-    transactions.sha_hash,
-    transactions.written_on
-    FROM transactions
-    WHERE transactions.currency = ? 
-    and transactions.pubkey =? 
-    AND transactions.ts >= ? 
-    and transactions.ts <= ? 
-    AND transactions.issuers LIKE "%{pubkey}%"
+      transactions.ts,
+      transactions.pubkey,
+      total_amount((amount * -1), amountbase) as amount,
+      transactions.comment ,
+      transactions.sha_hash,
+      transactions.written_on
+    FROM 
+      transactions
+    WHERE 
+      transactions.currency = ? 
+      and transactions.pubkey = ? 
+      AND transactions.ts >= ? 
+      and transactions.ts <= ? 
+      AND transactions.issuers LIKE "%{pubkey}%"
 UNION ALL
 SELECT 
-    transactions.ts,
-    transactions.pubkey,
-    total_amount(amount, amountbase) as amount,
-    transactions.comment ,
-    transactions.sha_hash,
-    transactions.written_on
-    FROM transactions
-    WHERE transactions.currency = ? 
-    and transactions.pubkey =? 
-    AND transactions.ts >= ? 
-    and transactions.ts <= ? 
-    AND transactions.receivers LIKE "%{pubkey}%"
+      transactions.ts,
+      transactions.pubkey,
+      total_amount(amount, amountbase) as amount,
+      transactions.comment ,
+      transactions.sha_hash,
+      transactions.written_on
+    FROM 
+      transactions
+    WHERE 
+      transactions.currency = ? 
+      and transactions.pubkey = ? 
+      AND transactions.ts >= ? 
+      and transactions.ts <= ? 
+      AND transactions.receivers LIKE "%{pubkey}%"
 UNION ALL
 SELECT 
-    dividends.timestamp as ts,
-    dividends.pubkey ,
-    total_amount(amount, base) as amount,
-    NULL as comment,
-    NULL as sha_hash,
-    dividends.block_number AS written_on
-    FROM dividends
-    WHERE dividends.currency = ? and dividends.pubkey =? AND dividends.timestamp >= ? and dividends.timestamp <= ?
+      dividends.timestamp as ts,
+      dividends.pubkey ,
+      total_amount(amount, base) as amount,
+      NULL as comment,
+      NULL as sha_hash,
+      dividends.block_number AS written_on
+    FROM 
+      dividends
+    WHERE 
+      dividends.currency = ? 
+      and dividends.pubkey =? 
+      AND dividends.timestamp >= ? 
+      and dividends.timestamp <= ?
 """
 
 PAGE_LENGTH = 50
@@ -79,10 +89,10 @@ LIMIT {limit} OFFSET {offset}""").format(offset=offset,
         :param str pubkey: the criterions of the lookup
         :rtype: List[sakia.data.entities.Transaction]
         """
-        request = """
+        request = ("""
 SELECT COUNT(*)
 FROM (
-""" + TX_HISTORY_REQUEST + ")"
+""" + TX_HISTORY_REQUEST + ")").format(pubkey=pubkey)
         c = self._conn.execute(request, (currency, pubkey, ts_from, ts_to,
                                                     currency, pubkey, ts_from, ts_to,
                                                     currency, pubkey, ts_from, ts_to))
@@ -119,5 +129,6 @@ FROM (
         :rtype: List[sakia.data.entities.Transaction]
         """
         count = self._transfers_and_dividends_count(currency, pubkey, ts_from, ts_to)
-        return int(count / PAGE_LENGTH) + 1
+        return int(count / PAGE_LENGTH)
+    
 
-- 
GitLab