[aur-dev] [PATCH 1/2] git-serve: Add support for (un-)voting
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@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
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- test/t1200-git-serve.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/test/t1200-git-serve.sh b/test/t1200-git-serve.sh index d422c48..f986b62 100755 --- a/test/t1200-git-serve.sh +++ b/test/t1200-git-serve.sh @@ -414,4 +414,70 @@ test_expect_success "Flag using a comment which is too short." ' test_cmp expected actual ' +test_expect_success "Vote for a package base." ' + SSH_ORIGINAL_COMMAND="vote foobar" AUR_USER=user AUR_PRIVILEGED=0 \ + "$GIT_SERVE" 2>&1 && + cat >expected <<-EOF && + 3|1 + EOF + echo "SELECT PackageBaseID, UsersID FROM PackageVotes;" | \ + sqlite3 aur.db >actual && + test_cmp expected actual && + cat >expected <<-EOF && + 1 + EOF + echo "SELECT NumVotes FROM PackageBases WHERE Name = \"foobar\";" | \ + sqlite3 aur.db >actual && + test_cmp expected actual +' + +test_expect_success "Vote for a package base twice." ' + SSH_ORIGINAL_COMMAND="vote foobar" AUR_USER=user AUR_PRIVILEGED=0 \ + test_must_fail "$GIT_SERVE" 2>&1 && + cat >expected <<-EOF && + 3|1 + EOF + echo "SELECT PackageBaseID, UsersID FROM PackageVotes;" | \ + sqlite3 aur.db >actual && + test_cmp expected actual && + cat >expected <<-EOF && + 1 + EOF + echo "SELECT NumVotes FROM PackageBases WHERE Name = \"foobar\";" | \ + sqlite3 aur.db >actual && + test_cmp expected actual +' + +test_expect_success "Remove vote from a package base." ' + SSH_ORIGINAL_COMMAND="unvote foobar" AUR_USER=user AUR_PRIVILEGED=0 \ + "$GIT_SERVE" 2>&1 && + cat >expected <<-EOF && + EOF + echo "SELECT PackageBaseID, UsersID FROM PackageVotes;" | \ + sqlite3 aur.db >actual && + test_cmp expected actual && + cat >expected <<-EOF && + 0 + EOF + echo "SELECT NumVotes FROM PackageBases WHERE Name = \"foobar\";" | \ + sqlite3 aur.db >actual && + test_cmp expected actual +' + +test_expect_success "Try to remove the vote again." ' + SSH_ORIGINAL_COMMAND="unvote foobar" AUR_USER=user AUR_PRIVILEGED=0 \ + test_must_fail "$GIT_SERVE" 2>&1 && + cat >expected <<-EOF && + EOF + echo "SELECT PackageBaseID, UsersID FROM PackageVotes;" | \ + sqlite3 aur.db >actual && + test_cmp expected actual && + cat >expected <<-EOF && + 0 + EOF + echo "SELECT NumVotes FROM PackageBases WHERE Name = \"foobar\";" | \ + sqlite3 aur.db >actual && + test_cmp expected actual +' + test_done -- 2.11.0
participants (1)
-
Lukas Fleischer