-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
93 lines (69 loc) · 2.82 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
87
88
89
90
91
92
import os
from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from flask_session import Session
# Configure application
app = Flask(__name__)
# Configure sessions
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///birthdays.db")
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
# TODO: Add the user's entry into the database
id = request.form.get("id")
job = request.form.get("job")
name = request.form.get("name").strip().title()
month = int(request.form.get("month"))
day = int(request.form.get("day"))
if not request.form.get("name"):
flash('Please Enter Valid Data 😒', 'warning')
# Redirect Error Fix
request.method = "GET"
return index()
if day > 31 or day < 1:
flash('Please Enter Valid Day 😓', 'error')
# Redirect Error Fix
request.method = "GET"
return index()
if month > 12 or month < 1:
flash('Please Enter Valid Month 😔', 'error')
# Redirect Error Fix
request.method = "GET"
return index()
if not name.isalnum() or not name.isalpha():
flash('Please Enter Valid Name and Check Enter The First Name Only 😕', 'error')
# Redirect Error Fix
request.method = "GET"
return index()
if job == "delete":
flash('You have Deleted successfully 😟', 'info')
db.execute("DELETE FROM birthdays WHERE id = ?", id)
session['id'] = None
session['name'] = None
# Redirect Error Fix
request.method = "GET"
return index()
if job == "edit":
flash('You have successfully modified your birthday 😄', 'success')
db.execute("UPDATE birthdays SET name = ?,day = ?, month = ? WHERE id = ?", name, day, month, id)
# Redirect Error Fix
request.method = "GET"
return index()
if job == "add":
flash('Congratulations, you have added successfully 🥳', 'success')
user_id = db.execute("INSERT INTO birthdays (name,month,day) VALUES(?,?,?)", name, month, day)
session['id'] = user_id
session['name'] = name
# Redirect Error Fix
request.method = "GET"
return index()
else:
# TODO: Display the entries in the database on index.html
birthdays = db.execute("SELECT * FROM birthdays")
return render_template("index.html", birthdays=birthdays)