-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
142 lines (105 loc) · 3.8 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Connection DB with Flask views
- We’ve finally learned enough to make a real website!
- Key Features
- Display Templates
- Accept User Information through Forms
- Save Supplied Information in Database
- Report back saved information
Troubleshoot links:
- Flask-SQLAlchemy nullable=False -> https://stackoverflow.com/questions/9844150/flask-sqlalchemy-nullable-false/9845764
"""
from flask import Flask, render_template, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from forms import AddEntry, DeleteEntry, AddUser, DeleteUser
import os, datetime as dt
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database Configuration
app.config['SECRET_KEY'] = 'mysecretkey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir, 'journalsDB.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app, db)
# Create Database Models
class Journal(db.Model):
__tablename__ = 'journals'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(128), nullable=False)
date = db.Column(db.DateTime, nullable=False)
content = db.Column(db.Text)
userid = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, userid, title, date, content):
self.userid = userid
self.title = title
self.date = date
self.content = content
def __repr__(self):
return f"Journal<'{self.user}, {self.date}', '{self.title}'>"
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(128), nullable=False)
journal_entry = db.relationship('Journal', backref='user', lazy="dynamic")
def __init__(self, username):
self.username = username
def __repr__(self):
return f"User<'{self.username}'>"
# Create Database Views
@app.route('/')
def index():
return render_template("home.html")
@app.route('/adduser', methods=['GET', 'POST'])
def adduser():
form = AddUser()
if form.validate_on_submit():
username=form.username.data
new_user = Users(username)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('index'))
return render_template('user.html', form=form)
@app.route('/add', methods=['GET', 'POST'])
def add():
form = AddEntry()
if form.validate_on_submit():
user = form.userid.data
title = form.title.data
date = dt.datetime.now()
content = form.content.data
new_entry = Journal(user, title, date, content)
db.session.add(new_entry)
db.session.commit()
return redirect(url_for('index'))
return render_template('add.html', form=form)
@app.route('/delete', methods=['GET', 'POST'])
def delete():
form = DeleteEntry()
if form.validate_on_submit():
id = form.id.data
entry = Journal.query.get(id)
db.session.delete(entry)
db.session.commit()
return redirect(url_for('index'))
return render_template('delete.html', form=form)
@app.route('/deleteuser', methods=['GET', 'POST'])
def deleteuser():
form = DeleteUser()
if form.validate_on_submit():
id = form.id.data
user = Users.query.get(id)
db.session.delete(user)
db.session.commit()
return redirect(url_for('index'))
return render_template('deleteuser.html', form=form)
@app.route('/list', methods=['GET', 'POST'])
def list():
entries = Journal.query.all()
return render_template('list.html', entries=entries)
@app.route('/listusers', methods=['GET', 'POST'])
def listusers():
userslist = Users.query.all()
return render_template('listusers.html', userslist=userslist)
if __name__ == "__main__":
app.run(debug=True)