-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
358 lines (305 loc) · 10.6 KB
/
manage.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from flask import (
Flask, render_template,
redirect, request,
flash, session,
jsonify
)
from utils.forms import (
LoginForm, SignUpForm,
AddNoteForm, AddTagForm,
ChangeEmailForm, ChangePasswordForm
)
from flask_restful import Resource, Api, reqparse
from utils.decorators import login_required
from flask_pagedown import PageDown
from flask import Markup
import utils.functions as functions
import datetime
import markdown
import os
from dotenv import load_dotenv
load_dotenv('.env')
app = Flask(__name__)
api = Api(app)
pagedown = PageDown(app)
parser = reqparse.RequestParser()
app.secret_key = os.getenv('SECRET_KEY')
@app.route('/')
def home_page():
'''
App for hompage
'''
session['user_count'] = functions.get_user_count()
try:
if session['username']:
return render_template('homepage.html', username=session['username'])
return render_template('homepage.html')
except (KeyError, ValueError):
return render_template('homepage.html')
@app.route('/profile/')
@login_required
def profile():
'''
App for user profile can only be accessed only after successful login
'''
if request.method == 'GET':
notes = functions.get_data_using_user_id(session['id'])
tags = []
if notes:
for note in notes:
tags_list = functions.get_tag_using_note_id(note[0])
temp_list = []
if tags_list:
for tag in tags_list:
temp = functions.get_data_using_tag_id(tag)
if temp is not None:
temp_list.append(temp[0])
tags.append(', '.join(temp_list))
return render_template('profile.html',username=session['username'],notes=notes,tags=tags)
@app.route('/login/', methods=('GET', 'POST'))
def login():
'''
App for creating Login page
'''
form = LoginForm()
if form.validate_on_submit():
username = request.form['username']
password = functions.generate_password_hash(request.form['password'])
user_id = functions.check_user_exists(username, password)
if user_id:
session['username'] = username
session['id'] = user_id
functions.store_last_login(session['id'])
return redirect('/profile/')
else:
flash('Username/Password Incorrect!')
return render_template('login.html', form=form)
@app.route('/signup/', methods=('GET', 'POST'))
def signup():
'''
App for registering new user
'''
form = SignUpForm()
if form.validate_on_submit():
username = request.form['username']
password = functions.generate_password_hash(request.form['password'])
email = request.form['email']
check = functions.check_username(username)
if check:
flash('Username already taken!')
else:
functions.signup_user(username, password, email)
session['username'] = username
user_id = functions.check_user_exists(username, password)
session['id'] = user_id
return redirect('/profile/')
return render_template('signup.html', form=form)
@app.route("/logout/")
def logout():
'''
App for logging out user
'''
session['username'] = None
session['id'] = None
return login()
@app.route("/notes/add/", methods=['GET', 'POST'])
@login_required
def add_note():
'''
App for adding note
'''
form = AddNoteForm()
form.tags.choices = functions.get_all_tags(session['id'])
if form.tags.choices is None:
form.tags = None
if form.validate_on_submit():
note_title = request.form['note_title']
note_markdown = form.note.data
note = Markup(markdown.markdown(note_markdown))
try:
tags = form.tags.data
tags = ','.join(tags)
except:
tags = None
functions.add_note(note_title, note, note_markdown, tags, session['id'])
return redirect('/profile/')
return render_template('add_note.html', form=form, username=session['username'])
@app.route("/notes/<id>/")
@login_required
def view_note(id):
'''
App for viewing a specific note
'''
notes = functions.get_data_using_id(id)
return render_template('view_note.html', notes=notes, username=session['username'])
@app.route("/notes/edit/<note_id>/", methods=['GET', 'POST'])
@login_required
def edit_note(note_id):
'''
App for editing a particular note
'''
form = AddNoteForm()
form.tags.choices = functions.get_all_tags(session['id'])
form.tags.default = functions.get_tag_using_note_id(note_id)
form.tags.process(request.form)
if form.tags.choices is None:
form.tags = None
if request.method == 'GET':
data = functions.get_data_using_id(note_id)
form.note_id.data = note_id
form.note_title.data = data[0][3]
form.note.data = data[0][5]
return render_template('edit_note.html', form=form, username=session['username'], id=note_id)
elif form.validate_on_submit():
note_id = form.note_id.data
note_title = request.form['note_title']
note_markdown = form.note.data
try:
tags = form.tags.data
tags = ','.join(tags)
except:
tags = None
note = Markup(markdown.markdown(note_markdown))
functions.edit_note(note_title, note, note_markdown, tags, note_id=note_id)
return redirect('/profile/')
@app.route("/notes/delete/<id>/", methods=['GET', 'POST'])
@login_required
def delete_note(id):
'''
App for viewing a specific note
'''
functions.delete_note_using_id(id)
notes = functions.get_data_using_user_id(session['id'])
tags = []
if notes:
for note in notes:
tags_list = functions.get_tag_using_note_id(note[0])
temp_list = []
if tags_list:
for tag in tags_list:
temp = functions.get_data_using_tag_id(tag)
if temp is not None:
temp_list.append(temp[0])
tags.append(', '.join(temp_list))
return render_template('profile.html', delete=True, tags=tags, username=session['username'], notes=notes)
@app.route("/tags/add/", methods=['GET', 'POST'])
@login_required
def add_tag():
'''
App for adding a tag
'''
form = AddTagForm()
if form.validate_on_submit():
tag = request.form['tag']
functions.add_tag(tag, session['id'])
return redirect('/profile/')
return render_template('add_tag.html', form=form, username=session['username'])
@app.route("/tags/")
@login_required
def view_tag():
'''
App for viewing all available tags
'''
tags = functions.get_all_tags(session['id'])
return render_template('edit_tag.html', tags=tags, username=session['username'])
@app.route("/tags/view/<tag_id>")
@login_required
def view_notes_using_tag(tag_id):
'''
App for viewing all available notes tagged under specific tag
'''
notes = functions.get_notes_using_tag_id(tag_id, session['id'])
tag_name = functions.get_tagname_using_tag_id(tag_id)
return render_template(
'view_tag.html',
notes=notes,
username=session['username'],
tag_name=tag_name
)
@app.route("/tags/delete/<tag_id>/")
@login_required
def delete_tag(tag_id):
'''
App for deleting a specific tag
'''
functions.delete_tag_using_id(tag_id)
tags = functions.get_all_tags(session['id'])
return render_template('edit_tag.html', tags=tags, delete=True, username=session['username'])
# Custom Filter
@app.template_filter()
def custom_date(date):
'''
Convert a datetime into custom format like: Sep 12,2017 19:07:32
'''
date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
return date.strftime('%b %d,%Y %H:%M:%S')
@app.route("/profile/settings/")
@login_required
def profile_settings():
'''
App for getting profile settings for a user
'''
user_data = functions.get_user_data(session['id'])
notes_count = functions.get_number_of_notes(session['id'])
tag_count = functions.get_number_of_tags(session['id'])
return render_template(
'profile_settings.html',user_data=user_data,username=session['username'],notes_count=notes_count,tag_count=tag_count)
@app.route("/profile/settings/change_email/", methods=['GET', 'POST'])
@login_required
def change_email():
'''
App for changing the email of a user
'''
form = ChangeEmailForm()
if form.validate_on_submit():
email = request.form['email']
functions.edit_email(email, session['id'])
return redirect('/profile/settings/')
return render_template('change_email.html', form=form, username=session['username'])
@app.route("/profile/settings/change_password/", methods=['GET', 'POST'])
@login_required
def change_password():
'''
App for changing the password of a user
'''
form = ChangePasswordForm()
if form.validate_on_submit():
password = request.form['password']
functions.edit_password(password, session['id'])
return redirect('/profile/settings/')
return render_template('change_password.html', form=form, username=session['username'])
@app.route('/background_process/')
def background_process():
'''
App for handling AJAX request for searching notes
'''
try:
notes = request.args.get('notes')
if notes == '':
return jsonify(result='')
results = functions.get_search_data(str(notes), session['id'])
temp = ''
for result in results:
temp += "<h4><a href='/notes/" + str(result[0]) + "/'>" + result[1] + "</a></h4><br>"
return jsonify(result=Markup(temp))
except Exception as e:
return str(e)
class GetDataUsingUserID(Resource):
def post(self):
try:
args = parser.parse_args()
username = args['username']
password = functions.generate_password_hash(args['password'])
user_id = functions.check_user_exists(username, password)
if user_id:
functions.store_last_login(user_id)
return functions.get_rest_data_using_user_id(user_id)
else:
return {'error': 'You cannot access this page, please check username and password'}
except AttributeError:
return {'error': 'Please specify username and password'}
api.add_resource(GetDataUsingUserID, '/api/')
parser.add_argument('username')
parser.add_argument('password')
if __name__ == '__main__':
app.run(debug=True)