-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
76 lines (56 loc) · 1.8 KB
/
application.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from flask import Flask, session, redirect, url_for, flash
from logging import FileHandler, WARNING
from functools import wraps
from isbnlib import is_isbn13, to_isbn10, is_isbn10
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
# Set up app
app = Flask(__name__)
# Error Log:
if not app.debug:
f_handler = FileHandler('error_log.txt')
f_handler.setLevel(WARNING)
app.logger.addHandler(f_handler)
# Change in production
app.secret_key = 'secret'
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
# Set up Goodreads API
if not os.getenv("GR_API_KEY"):
raise RuntimeError("GR_API_KEY is not set")
gr_api_key = os.getenv("GR_API_KEY")
# Set up API host
if not os.getenv("API_HOST"):
raise RuntimeError("API_HOST is not set")
api_host = os.getenv("API_HOST")
def is_isbn_code(search):
"""checks if the received string is valid isbn number"""
check = ''.join(ch for ch in search if ch.isalnum())
if is_isbn13(check):
return to_isbn10(check)
if is_isbn10(check):
return check
else:
return False
def login_only(view):
@wraps(view)
def wrap(*args, **kwargs):
try:
if session['user']:
return view(*args, **kwargs)
else:
flash('login first', 'debug')
return redirect(url_for('login'))
except KeyError:
return redirect(url_for('login'))
return wrap
import views # noqa: F401
import api # noqa: F401