mirror of https://github.com/me50/kukemuna.git
76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
import os
|
|
|
|
from cs50 import SQL
|
|
from flask import Flask, flash, jsonify, redirect, render_template, request, session
|
|
|
|
# Configure application
|
|
app = Flask(__name__)
|
|
|
|
# Ensure templates are auto-reloaded
|
|
app.config["TEMPLATES_AUTO_RELOAD"] = True
|
|
|
|
# Configure CS50 Library to use SQLite database
|
|
db = SQL("sqlite:///birthdays.db")
|
|
|
|
|
|
@app.after_request
|
|
def after_request(response):
|
|
"""Ensure responses aren't cached"""
|
|
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
|
|
response.headers["Expires"] = 0
|
|
response.headers["Pragma"] = "no-cache"
|
|
return response
|
|
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
def index():
|
|
if request.method == "POST":
|
|
|
|
# Add the user's entry into the database
|
|
name = request.form.get("name")
|
|
if not name:
|
|
return redirect("/")
|
|
|
|
day = request.form.get("day")
|
|
if not day:
|
|
return redirect("/")
|
|
try:
|
|
day = int(day)
|
|
except ValueError:
|
|
return redirect("/")
|
|
if day < 1 or day > 31:
|
|
return redirect("/")
|
|
|
|
month = request.form.get("month")
|
|
if not month:
|
|
return redirect("/")
|
|
try:
|
|
month = int(month)
|
|
except ValueError:
|
|
return redirect("/")
|
|
if month < 1 or month > 12:
|
|
return redirect("/")
|
|
|
|
db.execute("INSERT INTO birthdays(name, day, month) VALUES(?, ?, ?)", name, day ,month)
|
|
|
|
return redirect("/")
|
|
|
|
else:
|
|
|
|
# Display the entries in the database on index.html
|
|
bdays = db.execute("SELECT * FROM birthdays")
|
|
return render_template("index.html", bdays=bdays)
|
|
|
|
# Remove single entry
|
|
@app.route("/remove", methods=["POST"])
|
|
def remove():
|
|
bday_id = request.form.get("id")
|
|
db.execute("DELETE FROM birthdays WHERE ID=?", bday_id)
|
|
return redirect("/")
|
|
|
|
# Remove all entries
|
|
@app.route("/remove_all", methods=["POST"])
|
|
def remove_all():
|
|
db.execute("DELETE FROM birthdays")
|
|
return redirect("/")
|