-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
27 lines (23 loc) · 878 Bytes
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sqlite3
from flask import redirect, session
from functools import wraps
# Function to redirect user to login page if not logged in
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/0.12/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
# Function to get database connection
def get_db_connection(db_name):
conn = sqlite3.connect(db_name)
conn.row_factory = sqlite3.Row # to access columns by name
return conn
# Function to check allowed file extensions
def allowed_file(filename, ALLOWED_EXTENSIONS):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS