This is an automated email from the ASF dual-hosted git repository.

gstein pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/steve.git

commit 43e44b7e24cc815e52c107b6b44440ba8393fc2f
Author: Greg Stein <[email protected]>
AuthorDate: Fri Sep 26 20:12:01 2025 -0500

    Add some query APIs to help populate the UX.
    
    Queries for "what can I vote in?" and "what elections do I own and
    manage?"
    
    * queries.yaml: add queries
    * schema.sql: comment tweak
    * election.py: new class/static methods for opening a database and
        building cursors, and for answering the above questions/APIs.
---
 v3/queries.yaml      | 13 +++++++++++++
 v3/schema.sql        |  2 +-
 v3/steve/election.py | 31 +++++++++++++++++++++++++++++--
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/v3/queries.yaml b/v3/queries.yaml
index 4cc3548..0125855 100644
--- a/v3/queries.yaml
+++ b/v3/queries.yaml
@@ -85,6 +85,19 @@ election:
     #     the ORDER BY. However, with few revotes, that sort is not
     #     expensive. Collect some data. Or maybe people can lobby.
 
+    q_open_to_me: |
+        SELECT DISTINCT e.eid, e.title
+        FROM mayvote m
+        JOIN issue i ON m.iid = i.iid
+        JOIN election e ON i.eid = e.eid
+        WHERE m.pid = ?
+          AND e.salt IS NOT NULL
+          AND (e.closed IS NULL OR e.closed = 0)
+        ORDER BY e._ROWID_
+    q_owned: |
+        SELECT eid, title, authz, closed
+        FROM election WHERE owner_pid = ?
+
 
 person:
     c_add_person: |
diff --git a/v3/schema.sql b/v3/schema.sql
index 251312c..05bc4bb 100644
--- a/v3/schema.sql
+++ b/v3/schema.sql
@@ -87,7 +87,7 @@ CREATE TABLE election (
 
     /* Has this election been closed? NULL or 0 for not-closed (see
        SALT and OPENED_KEY to determine if the election has been
-       opened). 1 for closed (implies it was opened).  */
+       opened). 1 for closed (implies it was formerly-opened).  */
     closed  INTEGER  CHECK (closed IS NULL OR closed IN (0, 1)),
 
 
diff --git a/v3/steve/election.py b/v3/steve/election.py
index c34b4f4..6508fc0 100644
--- a/v3/steve/election.py
+++ b/v3/steve/election.py
@@ -44,11 +44,15 @@ class Election:
     S_OPEN = 'open'
     S_CLOSED = 'closed'
 
+    @staticmethod
+    def open_database(db_fname):
+        return asfpy.db.DB(db_fname,
+                           yaml_fname=QUERIES, yaml_section='election')
+
     def __init__(self, db_fname, eid):
         _LOGGER.debug(f'Opening election ID "{eid}"')
 
-        self.db = asfpy.db.DB(db_fname,
-                              yaml_fname=QUERIES, yaml_section='election')
+        self.db = self.open_database(db_fname)
         self.eid = eid
 
     def __getattr__(self, name):
@@ -382,3 +386,26 @@ class Election:
     def delete_by_eid(cls, db_fname, eid):
         "Delete the specified Election."
         cls(db_fname, eid).delete()
+
+    @classmethod
+    def open_to_pid(cls, db_fname, pid):
+        "List of elections are OPEN for PID to vote upon."
+
+        db = cls.open_database(db_fname)
+
+        # Run the generator to get all rows. Returned as EasyDicts.
+        db.q_open_to_me(pid,)
+        return [ row for row in db.q_open_to_me.fetchall() ]
+
+    @classmethod
+    def owned_elections(cls, db_fname, pid):
+        "List of elections are that PID has created."
+
+        db = cls.open_database(db_fname)
+
+        # NOTE: contains subset of columns. We don't want to return the
+        #       SALT or OPENED_KEY values.
+        #
+        # Run the generator to get all rows. Returned as EasyDicts.
+        db.q_owned(pid,)
+        return [ row for row in db.q_owned.fetchall() ]

Reply via email to