-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
86 lines (70 loc) · 2.81 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
77
78
79
80
81
82
83
84
85
86
# Import python libraries
import os
import datetime
# Import flask specific libraries
from flask import Flask, flash, render_template, redirect, request, url_for, session
from flask_session import Session
from flask_bcrypt import generate_password_hash, check_password_hash
# Import SQLAlchemy db model
from models import *
from sqlalchemy import exc
# cloudinary imports for image CDN
from cloudinary.uploader import upload
from cloudinary.utils import cloudinary_url
# Create a flask application
app = Flask(__name__)
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Check for environment variable DATABASE_URL
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Check for environment variable CLOUDINARY_URL
if not os.getenv("CLOUDINARY_URL"):
raise RuntimeError("CLOUDINARY_URL is not set")
# Set up database
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app) # db as defined in models.py
# Function to check if the user is already logged in
# Browser specific
def already_logged_in():
try:
session['logged_in'] == True
except KeyError:
session['logged_in'] = False
return session['logged_in']
@app.route('/')
def index():
return render_template("index.html")
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'GET':
return render_template("signup.html")
if request.method == 'POST':
session['user'] = User(firstname=request.form.get('firstname'),
middlename=request.form.get('middlename'),
lastname=request.form.get('lastname'),
emailid=request.form.get('emailid'),
password=generate_password_hash(request.form.get('password')).decode('utf-8'),
verified=True)
try:
db.session.add(session['user'])
db.session.commit()
except exc.IntegrityError:
return "Email ID already taken, please try again with different email ID."
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template("login.html")
if request.method == 'POST':
try:
session['user'] = User.query.filter_by(emailid=request.form.get('emailid')).first()
if check_password_hash(session['user'].password, request.form.get('password')):
session['loggedin'] = True
return "login successful"
except AttributeError:
return "There is no account with this Email ID, create one now!"
return redirect(url_for('login'))