From 7ee2fddcca6ef924ccfd10e6de9a799a9eb1abeb Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 23 Jan 2017 09:07:45 +0100 Subject: git-serve: Add support for (un-)voting Add support for voting for packages and removing votes from the SSH interface. The syntax is `vote ` resp. `unvote `. Signed-off-by: Lukas Fleischer --- 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 5922b2df..639f9e09 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 e33fbeb4..4c03e3b6 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 [...]": "Change package base keywords.", "setup-repo ": "Create a repository (deprecated).", "unflag ": "Remove out-of-date flag from a package base.", + "unvote ": "Remove vote from a package base.", + "vote ": "Vote for a package base.", "git-receive-pack": "Internal command used with Git.", "git-upload-pack": "Internal command used with Git.", } -- cgit v1.2.3-24-g4f1b