From e2f52ba43c776d9ceaafcbb264e87cf553ef12a0 Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Sat, 22 Jan 2022 12:32:34 +0000 Subject: [PATCH] Ensure sessions are closed --- main.py | 8 ++++---- slinky/__init__.py | 13 ++++++++++--- slinky/db.py | 6 +++--- slinky/web.py | 23 +++++++++++++---------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index 1dc132f..01bf518 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,8 @@ """ Main Flask-based app for Slinky """ -from flask import Flask, render_template -from flask_bootstrap import Bootstrap +from flask import Flask, Response, render_template +from flask_bootstrap import Bootstrap # type: ignore[import] from slinky.web import protect, slinky_webapp @@ -14,11 +14,11 @@ Bootstrap(app) @app.route('/') @protect -def index() -> str: +def index() -> Response: """ Index/Landing page Returns: str: string of page content """ - return render_template('index.html') + return Response(render_template('index.html'), 200) diff --git a/slinky/__init__.py b/slinky/__init__.py index 69756ca..a4c6764 100644 --- a/slinky/__init__.py +++ b/slinky/__init__.py @@ -8,7 +8,7 @@ from dataclasses import dataclass from datetime import datetime from typing import Optional -import sqlalchemy +import sqlalchemy # type: ignore[import] from slinky import db @@ -82,6 +82,7 @@ class Slinky: ) self.session.add(dbentry) self.session.commit() + self.session.close() return shortcode @@ -98,13 +99,15 @@ class Slinky: entry = self.session.query(db.ShortURL).filter_by(shortcode=shortcode).first() if entry: - return Shortcode( + ret_sc = Shortcode( entry.id, entry.shortcode, entry.url, entry.fixed_views, entry.expiry, ) + self.session.close() + return ret_sc return Shortcode(0, '', '', 0, '1970-01-01 00:00:00.000000') def remove_view(self, sc_id: int) -> None: @@ -118,6 +121,7 @@ class Slinky: {db.ShortURL.fixed_views: db.ShortURL.fixed_views - 1} ) self.session.commit() + self.session.close() def get_all(self) -> list[Shortcode]: """ @@ -129,7 +133,9 @@ class Slinky: Returns: Shortcode: full Shortcode object for the given shortcode """ - return list(self.session.query(db.ShortURL).all()) + shortcodes = list(self.session.query(db.ShortURL).all()) + self.session.close() + return shortcodes def delete_by_shortcode(self, shortcode: str) -> None: """ @@ -142,3 +148,4 @@ class Slinky: self.session.delete(entry) self.session.commit() + self.session.close() diff --git a/slinky/db.py b/slinky/db.py index 3f3c3fb..d286de4 100644 --- a/slinky/db.py +++ b/slinky/db.py @@ -3,9 +3,9 @@ DB component """ from dataclasses import dataclass -from sqlalchemy import Column, Integer, String, create_engine -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy import Column, Integer, String, create_engine # type: ignore[import] +from sqlalchemy.ext.declarative import declarative_base # type: ignore[import] +from sqlalchemy.orm import Session, sessionmaker # type: ignore[import] Base = declarative_base() diff --git a/slinky/web.py b/slinky/web.py index 437b0f7..971b9ab 100644 --- a/slinky/web.py +++ b/slinky/web.py @@ -8,11 +8,11 @@ from functools import wraps from typing import Any, Callable import yaml -from flask import Blueprint, Response, redirect, render_template, request -from flask_wtf import FlaskForm -from wtforms import DateTimeLocalField, HiddenField, IntegerField, StringField -from wtforms.validators import DataRequired, Length - +from flask import Blueprint, Response, render_template, request +from flask_wtf import FlaskForm # type: ignore[import] +from wtforms import HiddenField # type: ignore[import] +from wtforms import DateTimeLocalField, IntegerField, StringField +from wtforms.validators import DataRequired, Length # type: ignore[import] from slinky import Slinky slinky_webapp = Blueprint('webapp', __name__, template_folder='templates') @@ -112,7 +112,7 @@ def try_path_as_shortcode(path: str) -> Response: should_redirect = False if should_redirect: - return redirect(shortcode.url, 302) + return Response('Redirecting...', status=302, headers={'location': shortcode.url}) return Response('Not found', 404) @@ -124,7 +124,7 @@ def add() -> Response: Create and add a new shorturl Returns: - str: HTTP response + Response: HTTP response """ shortcode = '' url = '' @@ -150,7 +150,7 @@ def add() -> Response: else: return Response('Could not create a unique shortcode', 500) - return render_template('add.html', form=form, shortcode=shortcode) + return Response(render_template('add.html', form=form, shortcode=shortcode), 200) @slinky_webapp.route('/_/list', methods=['GET', 'POST']) @@ -160,7 +160,7 @@ def lister() -> Response: List the shortcodes, URLs, etc. Returns: - str: shortcode for the URL + Response: HTTP response """ form = DelForm(meta={'csrf': False}) slinky = Slinky(cfg['db']) @@ -168,4 +168,7 @@ def lister() -> Response: if form.is_submitted(): slinky.delete_by_shortcode(form.delete.data.strip()) - return render_template('list.html', form=form, shortcodes=slinky.get_all()) + return Response( + render_template('list.html', form=form, shortcodes=slinky.get_all()), + 200, + )