Skip to content

Commit

Permalink
close db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Ceccolini committed Mar 1, 2024
1 parent f0c4d56 commit 774a244
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
6 changes: 5 additions & 1 deletion lektor/admin/modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def insert_pending_editor():
# Send the email
res = send_email(NEW_PENDING_EDITOR_MESSAGE, NEW_EDITOR_SUBJECT, ADMIN_EMAILS)
except Exception as e:
return jsonify(okay=False, message="Error: " + str(e))
db.close()
return jsonify(okay=False, message="Error: " + str(e))
return jsonify(editor={
'id': editorId,
'name': fullname,
Expand Down Expand Up @@ -342,15 +343,18 @@ def brokenlinks_result():

# if no build in progress, return error
if executor.futures._state('brokenlinks') == None:
db.close()
return jsonify(okay=False, error='No brokenlinks in progress')

# if build already in progress, dont start another one
if not executor.futures.done('brokenlinks'):
db.close()
return jsonify(okay=True, done=False, status=executor.futures._state('brokenlinks'), links=broken_links)

s = executor.futures._state('brokenlinks')
# if build is done, return the result
executor.futures.pop('brokenlinks')
db.close()
return jsonify(okay=True, done=True, links=broken_links, status=s)

@bp.route('/reset')
Expand Down
36 changes: 9 additions & 27 deletions lektor/mactutor_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class Database(object):
def __init__(self):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
self.db_path = os.path.join(BASE_DIR, "mactutor.db")
self.conn = sqlite3.connect(self.db_path)
db_path = os.path.join(BASE_DIR, "mactutor.db")
self.conn = sqlite3.connect(db_path)
# create "REVIEWED_COMMITS" table if it doesn't exist
self.conn.execute('''CREATE TABLE IF NOT EXISTS REVIEWED_COMMITS (commitId TEXT PRIMARY KEY, reviewed INTEGER)''')
# create "EDITORS" table if it doesn't exist
Expand All @@ -18,14 +18,9 @@ def __init__(self):
self.conn.execute('''CREATE TABLE IF NOT EXISTS BROKEN_LINKS (id INTEGER PRIMARY KEY AUTOINCREMENT, parent TEXT, real TEXT, reason TEXT)''')
# create "FIXED_LINKS" table if it doesn't exist
self.conn.execute('''CREATE TABLE IF NOT EXISTS FIXED_LINKS (id INTEGER PRIMARY KEY AUTOINCREMENT, parent TEXT, real TEXT)''')
self.conn.commit()
self.conn.close()

def open(self):
self.conn = sqlite3.connect(self.db_path)
self.conn.commit()

def get_broken_links(self, path=None):
self.open()
if path==None:
cursor = self.conn.execute("SELECT * from BROKEN_LINKS")
else:
Expand All @@ -34,11 +29,9 @@ def get_broken_links(self, path=None):
links = []
for row in cursor:
links.append({"id": row[0], "parent": row[1], "real": row[2], "reason": row[3]})
self.conn.close()
return links

def add_broken_link(self, parent, real, reason):
self.open()
if (parent and real and reason):
# check if link already exists by comparing real
cursor = self.conn.execute("SELECT * from BROKEN_LINKS WHERE real = ?", (real,))
Expand All @@ -50,37 +43,29 @@ def add_broken_link(self, parent, real, reason):
update_command = "INSERT INTO BROKEN_LINKS (parent, real, reason) VALUES (?,?,?)"
self.conn.execute(update_command, (parent, real, reason))
self.conn.commit()
self.conn.close()

def delete_broken_link(self, id, parent, real):
self.open()
if (id):
update_command = "DELETE FROM BROKEN_LINKS WHERE id = ?"
self.conn.execute(update_command, (id,))
add_command = "INSERT INTO FIXED_LINKS (parent, real) VALUES (?,?)"
self.conn.execute(add_command, (parent, real))
self.conn.commit()
self.conn.close()

def get_reviewed_commits(self):
self.open()
cursor = self.conn.execute("SELECT * from REVIEWED_COMMITS")
commits = []
for row in cursor:
commits.append(row[0])
self.conn.close()
return commits

def update_reviewed_commits(self, commitId):
self.open()
def update_reviewed_commits(self, commitId):
if (commitId):
update_command = "INSERT INTO REVIEWED_COMMITS (commitId) VALUES (?)"
self.conn.execute(update_command, (commitId,))
self.conn.commit()
self.conn.close()

def get_editors(self, pendingEditors=False, adminOnly=False):
self.open()
if (pendingEditors):
cursor = self.conn.execute("SELECT * from PENDING_EDITORS")
else:
Expand All @@ -92,17 +77,15 @@ def get_editors(self, pendingEditors=False, adminOnly=False):
if adminOnly and row[3] != 'admin':
continue
editors.append({"id": row[0], "email": row[1], "fullname": row[2], "role": row[3], "joined": row[4], "lastlogin": row[5]})
self.conn.close()
return editors

def find_editor(self, id, pending=False):
def find_editor(self, id, pending=False):
for editor in self.get_editors(pending):
if (editor['id'] == id):
return editor
return None

def insert_editor(self, id, email, fullname, userType, pendingEditor=False):
self.open()
if (self.find_editor(id, pending=pendingEditor)):
self.update_editor(id, email, fullname, userType, pendingEditor)
return
Expand All @@ -113,10 +96,8 @@ def insert_editor(self, id, email, fullname, userType, pendingEditor=False):
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.conn.execute(insert_command, (id, email, fullname, userType, now, now))
self.conn.commit()
self.conn.close()

def update_editor(self, id, email, fullname, userType, lastlogin=None, pendingEditor=False):
self.open()
if (pendingEditor):
if (email):
update_command = "UPDATE PENDING_EDITORS SET email=? WHERE id=?"
Expand All @@ -141,14 +122,15 @@ def update_editor(self, id, email, fullname, userType, lastlogin=None, pendingEd
update_command = "UPDATE EDITORS SET lastlogin=? WHERE id=?"
self.conn.execute(update_command, (lastlogin, id))
self.conn.commit()
self.conn.close()

def delete_editor(self, id, pendingEditor=False):
self.open()
if (pendingEditor):
delete_command = "DELETE FROM PENDING_EDITORS WHERE id=(?)"
else:
delete_command = "DELETE FROM EDITORS WHERE id=(?)"
self.conn.execute(delete_command, (id,))
self.conn.commit()
self.conn.close()

def close(self):
if (self.conn):
self.conn.close()

0 comments on commit 774a244

Please sign in to comment.