-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.py
More file actions
112 lines (94 loc) · 4.23 KB
/
Copy pathapp.py
File metadata and controls
112 lines (94 loc) · 4.23 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
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
from flask import Flask, url_for, session, render_template, request, redirect
import json
app = Flask(__name__)
app.secret_key = "PyFlask_2k18_1516"
@app.route("/", methods = ["GET"])
def index():
if("loggedIn" in session.keys() and session["loggedIn"] == True):
return redirect("/dashboard"), 302
return render_template("index.html"), 200
@app.route("/login", methods = ["POST"])
def login():
username = request.form["username"]
password = request.form["password"]
loginData = open("data/user.json", "r+")
jsonData = loginData.read()
loginData.close()
print(jsonData)
if jsonData is not "":
dataDictionary = json.loads(jsonData)
if username in dataDictionary.keys() and dataDictionary[username] == password:
session["loggedIn"] = True
session["username"] = username
return redirect(url_for("dashboard"), 302)
elif username in dataDictionary.keys() and dataDictionary[username] is not password:
return render_template("index.html", message = "Invalid Username/Password", error = True)
else:
dataDictionary[username] = password
loginData = open("data/user.json", "w+")
loginData.write(json.dumps(dataDictionary))
loginData.close()
return render_template("index.html", message = "Successful Signup", error = False)
else:
jsonData = {username:password}
loginData = open("data/user.json", "w+")
loginData.write(json.dumps(jsonData))
loginData.close()
return render_template("index.html", message = "Successful Signup", error = False)
@app.route("/dashboard", methods = ["GET"])
def dashboard():
if("loggedIn" in session.keys() and session["loggedIn"] is not True):
return render_template("index.html", message = "Unauthorized", error = True), 401
return render_template("dashboard.html")
@app.route("/addnote", methods = ["POST"])
def addnote():
if("loggedIn" in session.keys() and session["loggedIn"] is not True):
return render_template("index.html", message = "Unauthorized", error = True), 401
title = request.form["title"]
note = request.form["note"]
username = session["username"]
notesData = open("data/notes.json", "r+")
jsonData = notesData.read()
notesData.close()
notesData = open("data/notes.json", "w+")
if jsonData is not "":
notes = json.loads(jsonData)
if username not in notes.keys():
notes[username] = {}
if title not in notes[username].keys():
notes[username][title] = note
notesData.write(json.dumps(notes))
notesData.close()
return render_template("dashboard.html", message = "Note added successfully", error = False), 201
else:
notes = json.loads(jsonData)
notesData.write(json.dumps(notes))
notesData.close()
return render_template("dashboard.html", message = "Note with title '{}', already exists".format(title), error = True)
else:
notes = {username:{title: note}}
notesData.write(json.dumps(notes))
notesData.close()
return render_template("dashboard.html", message = "Note added successfully", error = False), 201
@app.route("/getnotes", methods = ["GET"])
def getnotes():
if("loggedIn" in session.keys() and session["loggedIn"] is not True):
return render_template("index.html", message = "Unauthorized", error = True), 401
notesData = open("data/notes.json", "r+")
jsonData = notesData.read()
notesData.close()
if jsonData is not "":
notes = json.loads(jsonData)
if session["username"] not in notes.keys():
return render_template("notes.html", notes = {}, message = "No notes added yet", error = True)
return render_template("notes.html", notes = notes[session["username"]])
else:
return render_template("notes.html", notes = {}, message = "No notes added yet", error = True)
@app.route("/logout", methods = ["GET"])
def logout():
if("loggedIn" in session):
del session["loggedIn"]
del session["username"]
return redirect("/"), 302
if __name__ == "__main__":
app.run("127.0.0.1", "3004", debug = True)