From 4f38afb3599ea921d727ea9c844f24dad44517fb Mon Sep 17 00:00:00 2001
From: mtrela <mtrela@syncad.com>
Date: Fri, 4 Sep 2020 14:25:49 +0200
Subject: [PATCH] An unification of presentation for find_votes/list_votes

---
 hive/server/database_api/methods.py | 58 +++++++++++------------------
 1 file changed, 21 insertions(+), 37 deletions(-)

diff --git a/hive/server/database_api/methods.py b/hive/server/database_api/methods.py
index 71a737304..28f521881 100644
--- a/hive/server/database_api/methods.py
+++ b/hive/server/database_api/methods.py
@@ -167,6 +167,25 @@ class VotesPresentation(Enum):
     CondenserApi = 3
     BridgeApi = 4
 
+def result_presentation(rows, votes_presentation):
+  ret = []
+  for row in rows:
+      if votes_presentation == VotesPresentation.DatabaseApi:
+          ret.append(dict(voter=row.voter, author=row.author, permlink=row.permlink,
+                          weight=row.weight, rshares=row.rshares, vote_percent=row.percent,
+                          last_update=str(row.time), num_changes=row.num_changes))
+      elif votes_presentation == VotesPresentation.CondenserApi:
+          ret.append(dict(percent=str(row.percent), reputation=rep_to_raw(row.reputation),
+                          rshares=str(row.rshares), voter=row.voter))
+      elif votes_presentation == VotesPresentation.BridgeApi:
+          ret.append(dict(rshares=str(row.rshares), voter=row.voter))
+      else:
+          ret.append(dict(percent=row.percent, reputation=rep_to_raw(row.reputation),
+                          rshares=number_to_json_value(row.rshares), time=time_string_with_t(row.time), 
+                          voter=row.voter, weight=number_to_json_value(row.weight)
+                          ))
+  return ret
+
 @return_error_info
 async def find_votes(context, author: str, permlink: str, votes_presentation = VotesPresentation.DatabaseApi):
     """ Returns all votes for the given post """
@@ -192,25 +211,8 @@ async def find_votes(context, author: str, permlink: str, votes_presentation = V
             voter_id
     """
 
-    ret = []
     rows = await db.query_all(sql, author=author, permlink=permlink)
-
-    for row in rows:
-        if votes_presentation == VotesPresentation.DatabaseApi:
-            ret.append(dict(voter=row.voter, author=row.author, permlink=row.permlink,
-                            weight=row.weight, rshares=row.rshares, vote_percent=row.percent,
-                            last_update=str(row.time), num_changes=row.num_changes))
-        elif votes_presentation == VotesPresentation.CondenserApi:
-            ret.append(dict(percent=str(row.percent), reputation=rep_to_raw(row.reputation),
-                            rshares=str(row.rshares), voter=row.voter))
-        elif votes_presentation == VotesPresentation.BridgeApi:
-            ret.append(dict(rshares=str(row.rshares), voter=row.voter))
-        else:
-            ret.append(dict(percent=row.percent, reputation=rep_to_raw(row.reputation),
-                            rshares=number_to_json_value(row.rshares), time=time_string_with_t(row.time), 
-                            voter=row.voter, weight=number_to_json_value(row.weight)
-                            ))
-    return ret
+    return result_presentation(rows, votes_presentation)
 
 @return_error_info
 async def list_votes(context, start: list, limit: int, order: str, votes_presentation = VotesPresentation.DatabaseApi):
@@ -221,8 +223,6 @@ async def list_votes(context, start: list, limit: int, order: str, votes_present
     assert len(start) == 3, "Expecting 3 elements in start array"
     db = context['db']
 
-    ret = []
-
     sql = """
         SELECT
             voter,
@@ -266,20 +266,4 @@ async def list_votes(context, start: list, limit: int, order: str, votes_present
         """
         rows = await db.query_all(sql, author=start[1], permlink=start[2], voter=start[0], limit=limit)
 
-    for row in rows:
-        if votes_presentation == VotesPresentation.DatabaseApi:
-            ret.append(dict(voter=row.voter, author=row.author, permlink=row.permlink,
-                            weight=row.weight, rshares=row.rshares, vote_percent=row.percent,
-                            last_update=str(row.time), num_changes=row.num_changes))
-        elif votes_presentation == VotesPresentation.CondenserApi:
-            ret.append(dict(percent=str(row.percent), reputation=rep_to_raw(row.reputation),
-                            rshares=str(row.rshares), voter=row.voter))
-        elif votes_presentation == VotesPresentation.BridgeApi:
-            ret.append(dict(rshares=str(row.rshares), voter=row.voter))
-        else:
-            ret.append(dict(percent=row.percent, reputation=rep_to_raw(row.reputation),
-                            rshares=number_to_json_value(row.rshares), time=time_string_with_t(row.time), 
-                            voter=row.voter, weight=number_to_json_value(row.weight)
-                            ))
-
-    return ret
+    return result_presentation(rows, votes_presentation)
-- 
GitLab