[1/2] db.py: extract commonly used fetch_userid function

Message ID CAAue4xz4TS1b8syMWxpfB8A+fRo5Fq=UBznqHYGVbiNsTsROCg@mail.gmail.com
State New
Headers show
Series
  • add SSH command 'list-voted'
Related show

Commit Message

Simon Legner April 3, 2018, 7:19 p.m. UTC
---
 aurweb/db.py        |  7 ++++++
 aurweb/git/serve.py | 68 ++++++++++-------------------------------------------
 2 files changed, 19 insertions(+), 56 deletions(-)

     uids_rem = uids_old - uids_new
@@ -209,10 +185,7 @@ def pkgreq_close(reqid, user, reason, comments,
autoclose=False):
     if autoclose:
         userid = 0
     else:
-        cur = conn.execute("SELECT ID FROM Users WHERE Username = ?", [user])
-        userid = cur.fetchone()[0]
-        if userid == 0:
-            raise aurweb.exceptions.InvalidUserException(user)
+        userid = conn.fetch_userid(user)

     conn.execute("UPDATE PackageRequests SET Status = ?, ClosureComment = ? " +
                  "WHERE ID = ?", [status, comments, reqid])
@@ -250,9 +223,7 @@ def pkgbase_disown(pkgbase, user, privileged):
         comaintainers = pkgbase_get_comaintainers(pkgbase)
         if len(comaintainers) > 0:
             new_maintainer = comaintainers[0]
-            cur = conn.execute("SELECT ID FROM Users WHERE Username = ?",
-                               [new_maintainer])
-            new_maintainer_userid = cur.fetchone()[0]
+            new_maintainer_userid = conn.fetch_userid(new_maintainer)
             comaintainers.remove(new_maintainer)

     pkgbase_set_comaintainers(pkgbase, comaintainers, user, privileged)
@@ -261,10 +232,7 @@ def pkgbase_disown(pkgbase, user, privileged):

     conn.commit()

-    cur = conn.execute("SELECT ID FROM Users WHERE Username = ?", [user])
-    userid = cur.fetchone()[0]
-    if userid == 0:
-            raise aurweb.exceptions.InvalidUserException(user)
+    userid = conn.fetch_userid(user)

     subprocess.Popen((notify_cmd, 'disown', str(pkgbase_id), str(userid)))

@@ -280,10 +248,7 @@ def pkgbase_flag(pkgbase, user, comment):

     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)
+    userid = conn.fetch_userid(user)

     now = int(time.time())
     conn.execute("UPDATE PackageBases SET " +
@@ -303,10 +268,7 @@ def pkgbase_unflag(pkgbase, user):

     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)
+    userid = conn.fetch_userid(user)

     if user in pkgbase_get_comaintainers(pkgbase):
         conn.execute("UPDATE PackageBases SET OutOfDateTS = NULL " +
@@ -326,10 +288,7 @@ def pkgbase_vote(pkgbase, user):

     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)
+    userid = conn.fetch_userid(user)

     cur = conn.execute("SELECT COUNT(*) FROM PackageVotes " +
                        "WHERE UsersID = ? AND PackageBaseID = ?",
@@ -352,10 +311,7 @@ def pkgbase_unvote(pkgbase, user):

     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)
+    userid = conn.fetch_userid(user)

     cur = conn.execute("SELECT COUNT(*) FROM PackageVotes " +
                        "WHERE UsersID = ? AND PackageBaseID = ?",

Comments

Lukas Fleischer April 5, 2018, 2:17 p.m. UTC | #1
On Tue, 03 Apr 2018 at 21:19:05, Simon Legner wrote:
> ---
>  aurweb/db.py        |  7 ++++++
>  aurweb/git/serve.py | 68 ++++++++++-------------------------------------------
>  2 files changed, 19 insertions(+), 56 deletions(-)
> 
> diff --git a/aurweb/db.py b/aurweb/db.py
> index 0b58197..bbc674b 100644
> --- a/aurweb/db.py
> +++ b/aurweb/db.py
> @@ -49,3 +49,10 @@ class Connection:
> 
>      def close(self):
>          self._conn.close()
> +
> +    def fetch_userid(self, user):
> +        cur = self.execute("SELECT ID FROM Users WHERE Username = ?", [user])
> +        userid = cur.fetchone()[0]
> +        if userid == 0:
> +            raise aurweb.exceptions.InvalidUserException(user)
> +        return userid
> [...]

Thank you for your contribution.

I agree that it is a good idea to factor out this code fragment, however
I do not think db.py (which currently handles database access in a quite
generic way) is the best place to put this new function. It might be
better to add it to a new module users.py were we can collect other
user-related functions later; ultimately, it would be nice to have
proper ORM but this is at least a step in the right direction...

Regards,
Lukas

Patch

diff --git a/aurweb/db.py b/aurweb/db.py
index 0b58197..bbc674b 100644
--- a/aurweb/db.py
+++ b/aurweb/db.py
@@ -49,3 +49,10 @@  class Connection:

     def close(self):
         self._conn.close()
+
+    def fetch_userid(self, user):
+        cur = self.execute("SELECT ID FROM Users WHERE Username = ?", [user])
+        userid = cur.fetchone()[0]
+        if userid == 0:
+            raise aurweb.exceptions.InvalidUserException(user)
+        return userid
diff --git a/aurweb/git/serve.py b/aurweb/git/serve.py
index 93ff34c..01aea20 100755
--- a/aurweb/git/serve.py
+++ b/aurweb/git/serve.py
@@ -38,10 +38,7 @@  def pkgbase_exists(pkgbase):
 def list_repos(user):
     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)
+    userid = conn.fetch_userid(user)

     cur = conn.execute("SELECT Name, PackagerUID FROM PackageBases " +
                        "WHERE MaintainerUID = ?", [userid])
@@ -58,10 +55,7 @@  def create_pkgbase(pkgbase, user):

     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)
+    userid = conn.fetch_userid(user)

     now = int(time.time())
     cur = conn.execute("INSERT INTO PackageBases (Name, SubmittedTS, " +
@@ -90,10 +84,7 @@  def pkgbase_adopt(pkgbase, user, privileged):
     if not privileged and not cur.fetchone():
         raise aurweb.exceptions.PermissionDeniedException(user)

-    cur = conn.execute("SELECT ID FROM Users WHERE Username = ?", [user])
-    userid = cur.fetchone()[0]
-    if userid == 0:
-        raise aurweb.exceptions.InvalidUserException(user)
+    userid = conn.fetch_userid(user)

     cur = conn.execute("UPDATE PackageBases SET MaintainerUID = ? " +
                        "WHERE ID = ?", [userid, pkgbase_id])
@@ -138,23 +129,8 @@  def pkgbase_set_comaintainers(pkgbase, userlist,
user, privileged):

     userlist_old = set(pkgbase_get_comaintainers(pkgbase))

-    uids_old = set()
-    for olduser in userlist_old:
-        cur = conn.execute("SELECT ID FROM Users WHERE Username = ?",
-                           [olduser])
-        userid = cur.fetchone()[0]
-        if userid == 0:
-            raise aurweb.exceptions.InvalidUserException(user)
-        uids_old.add(userid)
-
-    uids_new = set()
-    for newuser in userlist:
-        cur = conn.execute("SELECT ID FROM Users WHERE Username = ?",
-                           [newuser])
-        userid = cur.fetchone()[0]
-        if userid == 0:
-            raise aurweb.exceptions.InvalidUserException(user)
-        uids_new.add(userid)
+    uids_old = set([conn.fetch_userid(olduser) for olduser in userlist_old])
+    uids_new = set([conn.fetch_userid(newuser) for newuser in userlist])

     uids_add = uids_new - uids_old