-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
88 lines (73 loc) · 2.73 KB
/
app.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
from flask import Flask, request, redirect
from flask.templating import render_template
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, migrate
import logging
# Implemented & Configuring the logger
logging.basicConfig(filename='record.log', level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
app = Flask(__name__)
app.debug = True
# adding configuration for using a sqlite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
# Creating an SQLAlchemy instance
db = SQLAlchemy(app)
# Settings for migrations
migrate = Migrate(app, db)
# Models
class Profile(db.Model):
# Id : Field which stores unique id for every row in
# database table.
# first_name: Used to store the first name if the user
# last_name: Used to store last name of the user
# Age: Used to store the age of the user
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(20), unique=False, nullable=False)
last_name = db.Column(db.String(20), unique=False, nullable=False)
age = db.Column(db.Integer, nullable=False)
# repr method represents how one object of this datatable
# will look like
def __repr__(self):
return f"Name : {self.first_name}, Age: {self.age}"
# function to render index page
@app.route('/')
def index():
profiles = Profile.query.all()
app.logger.debug("debug log info")
app.logger.info("Info log information")
app.logger.warning("Warning log info")
app.logger.error("Error log info")
app.logger.critical("Critical log info")
return render_template('index.html', profiles=profiles)
@app.route('/add_data')
def add_data():
return render_template('add_profile.html')
# function to add profiles
@app.route('/add', methods=["POST"])
def profile():
# In this function we will input data from the
# form page and store it in our database. Remember
# that inside the get the name should exactly be the same
# as that in the html input fields
first_name = request.form.get("first_name")
last_name = request.form.get("last_name")
age = request.form.get("age")
# create an object of the Profile class of models and
# store data as a row in our datatable
if first_name != '' and last_name != '' and age is not None:
p = Profile(first_name=first_name, last_name=last_name, age=age)
db.session.add(p)
db.session.commit()
return redirect('/')
else:
return redirect('/')
@app.route('/delete/<int:id>')
def erase(id):
# deletes the data on the basis of unique id and
# directs to home page
data = Profile.query.get(id)
db.session.delete(data)
db.session.commit()
return redirect('/')
#app.run(host='localhost', debug=True)
if __name__ == '__main__':
app.run()