-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
215 lines (180 loc) · 7.52 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from flask import Flask, flash, redirect, render_template, request, session
import sqlite3 as sql
from flask_session import Session
from helper import login_required, checkValidPass, remember_login, get_db_connection, getJulianday
from werkzeug.security import check_password_hash, generate_password_hash
# Configure application
app = Flask(__name__)
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
@app.route("/", methods=["POST", "GET"])
@login_required
def index():
''' index page where user can add and edit notes '''
# check if user logged in
if not session.get("name"):
return redirect("/login")
# display the appropriate message
if session.get("state"):
flash(session["state"])
session.pop("state")
if request.method == "POST":
heading = request.form.get("head")
noting = request.form.get("notes")
# check whether the user entered All fields
if not heading or not noting:
remember_login("Fill all coulumns")
return redirect("/")
id = session.get("user_id")
con = get_db_connection()
cur = con.cursor()
cur.execute("SELECT julianday('now','localtime') AS d")
dt = cur.fetchall()
con.close()
try:
with sql.connect("webnote.db") as con:
con = get_db_connection()
cur = con.cursor()
cur.execute("INSERT INTO notes (user_id, heading, noted, dateandtime) VALUES (?, ?, ?, ?)", [id, heading, noting, dt[0]['d']])
con.commit()
con.close()
except:
remember_login("Notes Not saved!")
return redirect("/")
# Remember which user has logged in
print("success")
remember_login("Notes have successfully saved!")
# All good, go to index page
return redirect("/")
else:
return render_template("index.html")
@app.route("/login", methods=["POST", "GET"])
def login():
''' LogIn '''
# Forget any user_id
session.clear()
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
# ensure username and password are given
if not username or not password:
state = "should provide both username and password"
return render_template("login.html", data=state)
# Check whether the user name is valid or not
con = get_db_connection()
cur = con.cursor()
cur.execute("select * from users where username = ?", [username])
rows = cur.fetchall()
con.close()
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], password):
return render_template("login.html", data="Username or password is invalid!")
# Remember which user has logged in
session["name"] = username
session["user_id"] = rows[0]["id"]
# Redirect user to home page
return redirect("/")
else:
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/register", methods=["POST", "GET"])
def register():
''' Register the user '''
if request.method == "POST":
try:
# get user entered values
username = request.form.get("username")
pass1 = request.form.get("password")
pass2 = request.form.get("password1")
# check all fields have entered
if not username or not pass1 or not pass2:
return render_template("register.html", data="Enter All Fields")
else:
if (pass1 != pass2):
return render_template("register.html", data="Password mismatch")
# validate password
if (checkValidPass(pass1) == False):
return render_template("register.html", data="Password should have length more than 8 and should include atleast 1 number and 1 alphabet")
# Generate hash for the password
pass1 = generate_password_hash(pass1)
with sql.connect("webnote.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO users (username, hash) VALUES (?,?)", [username, pass1])
con.commit()
except:
msg = "error in insert operation. Use different user name"
return render_template("register.html",data = msg)
con = get_db_connection()
cur = con.cursor()
cur.execute("select id from users where username = ?", [username])
rows = cur.fetchall()
con.close()
# Remember which user has logged in
session["name"] = username
session["user_id"] = rows[0]["id"]
session["state"] = "Registered!"
# All good, go to index page
return redirect("/")
else:
return render_template("register.html")
@app.route("/view", methods=["POST", "GET"])
@login_required
def view():
''' to view the notes and edit them '''
# display the appropriate message
if session.get("state"):
flash(session["state"])
session.pop("state")
id = session.get("user_id")
con = get_db_connection()
cur = con.cursor()
cur.execute("SELECT note_id, heading, noted, date(dateandtime) AS dt, time(dateandtime) AS tm FROM notes WHERE user_id = ? ORDER BY DATE(dateandtime) DESC, TIME(dateandtime) DESC", [id])
rows = cur.fetchall()
con.close()
if request.method == "POST":
action = request.form.get('action')
if action == 'update':
note_id = request.form.get('note_id')
call_head = f'head{note_id}'
heading = request.form.get(call_head)
call_note = f'notes{note_id}'
notes = request.form.get(call_note)
print(note_id)
dt = getJulianday()
with sql.connect("webnote.db") as con:
con = get_db_connection()
cur = con.cursor()
cur.execute("UPDATE notes SET heading = ?, noted = ?, dateandtime = ? WHERE note_id = ?", [heading, notes, dt, note_id])
con.commit()
con.close()
elif action == "search":
word = request.form.get("sname")
con = get_db_connection()
cur = con.cursor()
cur.execute("SELECT note_id, heading, noted, date(dateandtime) AS dt, time(dateandtime) AS tm FROM notes WHERE user_id = ? AND heading LIKE ? ORDER BY DATE(dateandtime) DESC, TIME(dateandtime) DESC", [id, "%" + word + "%"])
rows = cur.fetchall()
con.close()
return render_template("view.html", data=rows)
else:
note_id = request.form.get('note_id')
con = get_db_connection()
cur = con.cursor()
cur.execute("DELETE FROM notes WHERE note_id = ?", [note_id])
con.commit()
con.close()
con = get_db_connection()
cur = con.cursor()
cur.execute("SELECT note_id, heading, noted, date(dateandtime) AS dt, time(dateandtime) AS tm FROM notes WHERE user_id = ? ORDER BY DATE(dateandtime) DESC, TIME(dateandtime) DESC", [id])
rows = cur.fetchall()
con.close()
return render_template("view.html", data=rows)
else:
return render_template("view.html", data=rows)