[aur-dev] [PATCH 1/2] git-serve: Add support for (un-)voting

Lukas Fleischer lfleischer at archlinux.org
Mon Jan 23 08:25:46 UTC 2017


Add support for voting for packages and removing votes from the SSH
interface. The syntax is `vote <pkgbase>` resp. `unvote <pkgbase>`.

Signed-off-by: Lukas Fleischer <lfleischer at archlinux.org>
---
 aurweb/exceptions.py | 12 ++++++++++
 aurweb/git/serve.py  | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)

diff --git a/aurweb/exceptions.py b/aurweb/exceptions.py
index 5922b2d..639f9e0 100644
--- a/aurweb/exceptions.py
+++ b/aurweb/exceptions.py
@@ -48,6 +48,18 @@ class InvalidCommentException(AurwebException):
         super(InvalidCommentException, self).__init__(msg)
 
 
+class AlreadyVotedException(AurwebException):
+    def __init__(self, comment):
+        msg = 'already voted for package base: {:s}'.format(comment)
+        super(AlreadyVotedException, self).__init__(msg)
+
+
+class NotVotedException(AurwebException):
+    def __init__(self, comment):
+        msg = 'missing vote for package base: {:s}'.format(comment)
+        super(NotVotedException, self).__init__(msg)
+
+
 class InvalidArgumentsException(AurwebException):
     def __init__(self, msg):
         super(InvalidArgumentsException, self).__init__(msg)
diff --git a/aurweb/git/serve.py b/aurweb/git/serve.py
index e33fbeb..4c03e3b 100755
--- a/aurweb/git/serve.py
+++ b/aurweb/git/serve.py
@@ -318,6 +318,57 @@ def pkgbase_unflag(pkgbase, user):
     conn.commit()
 
 
+def pkgbase_vote(pkgbase, user):
+    pkgbase_id = pkgbase_from_name(pkgbase)
+    if not pkgbase_id:
+        raise aurweb.exceptions.InvalidPackageBaseException(pkgbase)
+
+    conn = aurweb.db.Connection()
+
+    cur = conn.execute("SELECT ID FROM Users WHERE Username = ?", [user])
+    userid = cur.fetchone()[0]
+    if userid == 0:
+        raise aurweb.exceptions.InvalidUserException(user)
+
+    cur = conn.execute("SELECT COUNT(*) FROM PackageVotes " +
+                       "WHERE UsersID = ? AND PackageBaseID = ?",
+                       [userid, pkgbase_id])
+    if cur.fetchone()[0] > 0:
+        raise aurweb.exceptions.AlreadyVotedException(pkgbase)
+
+    now = int(time.time())
+    conn.execute("INSERT INTO PackageVotes (UsersID, PackageBaseID, VoteTS) " +
+                 "VALUES (?, ?, ?)", [userid, pkgbase_id, now])
+    conn.execute("UPDATE PackageBases SET NumVotes = NumVotes + 1 " +
+                 "WHERE ID = ?", [pkgbase_id])
+    conn.commit()
+
+
+def pkgbase_unvote(pkgbase, user):
+    pkgbase_id = pkgbase_from_name(pkgbase)
+    if not pkgbase_id:
+        raise aurweb.exceptions.InvalidPackageBaseException(pkgbase)
+
+    conn = aurweb.db.Connection()
+
+    cur = conn.execute("SELECT ID FROM Users WHERE Username = ?", [user])
+    userid = cur.fetchone()[0]
+    if userid == 0:
+        raise aurweb.exceptions.InvalidUserException(user)
+
+    cur = conn.execute("SELECT COUNT(*) FROM PackageVotes " +
+                       "WHERE UsersID = ? AND PackageBaseID = ?",
+                       [userid, pkgbase_id])
+    if cur.fetchone()[0] == 0:
+        raise aurweb.exceptions.NotVotedException(pkgbase)
+
+    conn.execute("DELETE FROM PackageVotes WHERE UsersID = ? AND " +
+                 "PackageBaseID = ?", [userid, pkgbase_id])
+    conn.execute("UPDATE PackageBases SET NumVotes = NumVotes - 1 " +
+                 "WHERE ID = ?", [pkgbase_id])
+    conn.commit()
+
+
 def pkgbase_set_keywords(pkgbase, keywords):
     pkgbase_id = pkgbase_from_name(pkgbase)
     if not pkgbase_id:
@@ -467,6 +518,16 @@ def serve(action, cmdargv, user, privileged, remote_addr):
 
         pkgbase = cmdargv[1]
         pkgbase_unflag(pkgbase, user)
+    elif action == 'vote':
+        checkarg(cmdargv, 'repository name')
+
+        pkgbase = cmdargv[1]
+        pkgbase_vote(pkgbase, user)
+    elif action == 'unvote':
+        checkarg(cmdargv, 'repository name')
+
+        pkgbase = cmdargv[1]
+        pkgbase_unvote(pkgbase, user)
     elif action == 'set-comaintainers':
         checkarg_atleast(cmdargv, 'repository name')
 
@@ -485,6 +546,8 @@ def serve(action, cmdargv, user, privileged, remote_addr):
             "set-keywords <name> [...]": "Change package base keywords.",
             "setup-repo <name>": "Create a repository (deprecated).",
             "unflag <name>": "Remove out-of-date flag from a package base.",
+            "unvote <name>": "Remove vote from a package base.",
+            "vote <name>": "Vote for a package base.",
             "git-receive-pack": "Internal command used with Git.",
             "git-upload-pack": "Internal command used with Git.",
         }
-- 
2.11.0


More information about the aur-dev mailing list