-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
68 lines (53 loc) · 2.06 KB
/
app.py
File metadata and controls
68 lines (53 loc) · 2.06 KB
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
import os
from flask import Flask, render_template, request, session, url_for, redirect
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import Length, DataRequired
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
#DATABASE CONFIG FILE
projectdir = os.path.dirname(os.path.abspath(__file__))
databasefile = "sqlite:///{}".format(os.path.join(projectdir, 'notes.db'))
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SECRET_KEY'] = 'bernardisawesome'
app.config['SQLALCHEMY_DATABASE_URI'] = databasefile
app.config['SQLALCHEMY_TRACK_MODIFICATION'] = False
class Notes(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(20))
note = db.Column(db.String(128))
def __init__(self, title, note):
self.title = title
self.note = note
def __repr__(self):
return "<Note: {}".format(self.note)
class NotesForm(FlaskForm):
title = StringField('Enter the title of your notes', validators=[DataRequired(), Length(min=3, max=20)])
notes = TextAreaField('Enter your notes', validators=[DataRequired(), Length(min=5)])
submit = SubmitField('Save')
@app.route('/')
def index():
form = NotesForm()
allnotes = Notes.query.all()
return render_template('index.html', form=form, allnotes = allnotes)
@app.route('/addnotes', methods=['GET', 'POST'])
def addnotes():
form = NotesForm()
if request.method == 'POST':
note = Notes(title=form.title.data, note=form.notes.data)
db.session.add(note)
db.session.commit()
return redirect(url_for('index'))
@app.route('/notes', methods=['GET', 'POST'])
def notes():
form = NotesForm()
allnotes = Notes.query.all()
return render_template('notes.html', allnotes=allnotes, form=form)
@app.route('/delete/<string:id>/', methods=['GET', 'POST'])
def delete(id):
allnotes = Notes.query.filter_by(id=id).first()
db.session.delete(allnotes)
db.session.commit()
return redirect(url_for('notes'))
if __name__ == '__main__':
app.run(debug=True)