Ensure sessions are closed

This commit is contained in:
Scott Wallace 2022-01-22 12:32:34 +00:00
parent 13f41e6cc7
commit e2f52ba43c
Signed by: scott
GPG key ID: AA742FDC5AFE2A72
4 changed files with 30 additions and 20 deletions

View file

@ -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)

View file

@ -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()

View file

@ -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()

View file

@ -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,
)