From 3957ea7d012dfaa7f51b568441171a2125a77164 Mon Sep 17 00:00:00 2001
From: Shamil K Muhammed <noteness@disroot.org>
Date: Mon, 10 Jul 2017 22:47:32 +0530
Subject: [PATCH] Use the `with` statement on the connection object

Before, v.0.0.3, the connection was reinitialized everytime a new
auth request was sent to the module. But after that version, things
changed and a single connection was used.

This caused #2. It was because the transactions weren't commited
before querying again. Which stops the auth provider from getting
new data. So, the context manager is also used on the connection
everytime a query is sent to ensure the transactions are committed.

So, hopefully this commit fixes #2
---
 diaspora_auth_provider.py | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/diaspora_auth_provider.py b/diaspora_auth_provider.py
index 5ab5004..3e2af8c 100644
--- a/diaspora_auth_provider.py
+++ b/diaspora_auth_provider.py
@@ -22,13 +22,13 @@ import bcrypt
 
 import logging
 
-__VERSION__ = "0.0.4"
+__VERSION__ = "0.0.5"
 
 logger = logging.getLogger(__name__)
 
 
 class DiasporaAuthProvider:
-    __version__ = "0.0.4"
+    __version__ = "0.0.5"
 
     def __init__(self, config, account_handler):
         self.account_handler = account_handler
@@ -55,15 +55,16 @@ class DiasporaAuthProvider:
         local_part = user_id.split(':', 1)[0][1:]
         logger.info("Checking if user {} exists.".format(local_part))
         try:
-            with self.connection.cursor() as cursor:
-                yield threads.deferToThread( # Don't think this is needed, but w/e
-                    cursor.execute,
-                    "SELECT username, encrypted_password FROM users WHERE username=%s",
-                    (local_part,)
-                )
-                user = yield threads.deferToThread(
-                    cursor.fetchone
-                )
+            with self.connection:
+                with self.connection.cursor() as cursor:
+                    yield threads.deferToThread( # Don't think this is needed, but w/e
+                        cursor.execute,
+                        "SELECT username, encrypted_password FROM users WHERE username=%s",
+                        (local_part,)
+                    )
+                    user = yield threads.deferToThread(
+                        cursor.fetchone
+                    )
             # check if the user exists.
             if not user:
                 logger.info("User {} does not exist. Rejecting auth request".format(local_part))
-- 
GitLab