-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
770 lines (591 loc) · 27.1 KB
/
Copy pathapp.py
File metadata and controls
770 lines (591 loc) · 27.1 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session, url_for, send_file
from flask_session import Session
from sqlalchemy import false
from werkzeug.security import check_password_hash, generate_password_hash
from functools import wraps
import os
# Set Folder
ALLOWED_EXTENSIONS = {'txt'}
# Configure application
app = Flask(__name__)
# Configure folder
#app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///project.db")
# debugging switches
# input txt or paste
debug1 = False
# display unjoined list
debug2 = False
# display joined list
debug3 = False
# displya unique SQL title query
debug4 = False
# different format button route
debug5 = False
# fixing txt input
debug6 = False
def login_required(f):
"""
Decorate routes to require login.
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
#what if file name is e.g. text.kindle.txt?
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def homepage():
return render_template("index.html")
@app.route("/login", methods= ['GET', 'POST'])
def login():
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
flash("Username Field Empty", 'flash_error')
return render_template("login.html")
# Ensure password was submitted
elif not request.form.get("password"):
flash("Password Field Empty", 'flash_error')
return render_template("login.html")
# Query database for username
rows = db.execute("SELECT * FROM users WHERE username = ?", request.form.get("username"))
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
flash("Invalid Username and/or Password", 'flash_error')
return render_template("login.html")
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to home page
flash("Login Successful!", 'flash_success')
return redirect("/clean")
if request.method == "GET":
return render_template("login.html")
return render_template("error.html", message="Login Faied To Load")
@app.route("/register", methods = ['GET', 'POST'])
def register():
if request.method == "POST":
# validation
username = request.form.get("username")
# if emtpy
if not username:
flash("Username Field Empty", 'flash_error')
return render_template("register.html")
# if username taken
username_check_dict = db.execute("SELECT username from users")
username_check = []
for items in username_check_dict:
username_check.append(items["username"])
if username in username_check:
flash("Username Taken", 'flash_error')
return render_template("register.html")
# if empty password
password = request.form.get("password")
if not password:
flash("Password Field Empty", 'flash_error')
return render_template("register.html")
# check if passwords are the same
confirmation = request.form.get("confirmation")
if password != confirmation:
flash("Passwrod Does Not Match", 'flash_error')
return render_template("register.html")
# if password match, hash password
hash = generate_password_hash(password, method='pbkdf2:sha256', salt_length=16)
# store user in TABLE users
db.execute("INSERT INTO users (username, hash) VALUES (?, ?)", username, hash)
# obtain automatically assigned user id
user_id = db.execute("SELECT id FROM users WHERE username = ?", username)
user_id_id = user_id[0]['id']
# create TABLE for users portfolio
db.execute("CREATE TABLE user_highlights_? (highlight_id INTEGER PRIMARY KEY NOT NULL, highlight_counter INTEGER NOT NULL, title TEXT NOT NULL, author TEXT NOT NULL, details TEXT NOT NULL, highlights TEXT NOT NULL)", user_id_id)
# alert user of successful registration
flash("Registration Successful", 'flash_success')
# redirect user to login page
return redirect("/login")
if request.method == "GET":
return render_template("register.html")
return render_template("error.html", message="Registration Failed")
@app.route("/logout")
def logout():
session.clear()
flash("You were logged out successfully!", 'flash_success')
return redirect("/")
@app.route("/clean", methods = ['GET', 'POST'])
@login_required
def clean():
if request.method == "POST":
# check if post request has file part, if not then its paste box
if 'textfile' not in request.files:
text = request.form["hl-text"]
if not text:
flash("Text Field Empty", 'flash_error')
return render_template("clean.html")
if debug1:
print("-----pasted text-----")
print(text)
print("-----end of pasted text-----")
# Process text string
# set counters
highlight_counter = 0
colon_counter = 0
details_end_counter = 0
equals_counter = 0
# switch
# 0 = detecting title
# 1 = detecting author
# 2 = detecting details
# 3 = detecting highlights
# 4 = compiling one highlight block
switch = 0
# empty arrays to store data
title = []
author = []
details = []
highlight = []
if debug6:
print("reached start of for loop")
# iterate through text
for letter in text:
# detect title
if switch == 0 and letter == '(':
if debug6:
print("reached end of 0")
title = title[: len(title)-1]
#remove 'ufeff' from list
if '\ufeff' in title:
title.remove('\ufeff')
if '\n' in title:
title.remove('\n')
if '\r' in title:
title.remove('\r')
# continue to next step
switch = 1
if switch == 0:
title.append(letter)
# detect author
if switch == 1 and letter == ')':
if debug6:
print("reached end of 1")
if '\ufeff' in highlight:
highlight.remove('\ufeff')
author = author[1:]
switch = 2
if switch == 1:
author.append(letter)
# detect details
if details_end_counter == 2:
if debug6:
print("reached end of 2")
if '\ufeff' in highlight:
highlight.remove('\ufeff')
# reset counters
colon_counter = 0
details_end_counter = 0
details = details[5:]
switch = 3
if switch == 2:
details.append(letter)
if colon_counter == 2:
details_end_counter += 1
if switch == 2 and letter == ':':
colon_counter += 1
# detect highlights
if switch == 3 and letter == '=':
equals_counter += 1
if equals_counter == 10:
if debug6:
print("reached end of 3")
if '\ufeff' in highlight:
highlight.remove('\ufeff')
equals_counter = 0
highlight = highlight[4:(len(highlight)-2)]
switch = 4
if switch == 3 and letter != '=':
highlight.append(letter)
# compiling highlight block
if switch == 4:
# add to highlight counter
highlight_counter += 1
# debug before list join
if debug2:
print("---start of UNjoined highlight---")
print("Highlight counter:", highlight_counter)
print(title)
print(author)
print(details)
print(highlight)
print("equals_counter:", equals_counter)
print("---end of UNjoined highlight---")
# joining list into strings
title_string = ''.join([str(item) for item in title])
author_string = ''.join([str(item) for item in author])
details_string = ''.join([str(item) for item in details])
highlight_string =''.join([str(item) for item in highlight])
# debug after list join
if debug3:
print("---start of joined highlight-----")
print("Highlight_counter:", highlight_counter)
print(title_string)
print(author_string)
print(details_string)
print(highlight_string)
print("---end of joined highlight-----")
# get user session id
user_id = session["user_id"]
# input into user database
db.execute("INSERT INTO user_highlights_? (highlight_counter, title, author, details, highlights) VALUES (?, ?, ?, ?, ?)",
user_id, highlight_counter, title_string, author_string, details_string, highlight_string)
# clear temporary array data
title = []
author = []
details = []
highlight = []
# switch back and loop over new highlight
switch = 0
flash("Cleaning Successful!", 'flash_success')
return redirect("/format")
# if have, then its file submit
else:
if debug1:
print("request.files: ", request.files)
file = request.files['textfile']
if debug1:
print("file: ", file)
# validate empty file
if file.filename=="":
flash("No File Found", 'flash_error')
if debug1:
print("No File Found")
return render_template("clean.html")
# validate file format
if not allowed_file(file.filename):
flash("Not Supported File Format", 'flash_error')
if debug1:
print("Not Allowed File Format")
return render_template("clean.html")
# if cleared all validation
text = file.read()
text = str(text)
if debug1:
print("----- txt text -----")
print(text)
print("-----end of txt text-----")
# Process txt specific text string
# set counters
highlight_counter = 0
colon_counter = 0
details_end_counter = 0
equals_counter = 0
# switch
# 0 = detecting title
# 1 = detecting author
# 2 = detecting details
# 3 = detecting highlights
# 4 = compiling one highlight block
switch = 0
# empty arrays to store data
title = []
author = []
details = []
highlight = []
if debug6:
print("reached start of for loop")
# iterate through text
for letter in text:
# detect title
if switch == 0 and letter == '(':
if debug6:
print("reached end of 0")
title = title[: len(title)-1]
if highlight_counter == 0:
title = title[2:]
if highlight_counter > 0:
title = title[16:]
# continue to next step
switch = 1
if switch == 0:
title.append(letter)
# detect author
if switch == 1 and letter == ')':
if debug6:
print("reached end of 1")
author = author[1:]
switch = 2
if switch == 1:
author.append(letter)
# detect details
if details_end_counter == 2:
if debug6:
print("reached end of 2")
# reset counters
colon_counter = 0
details_end_counter = 0
details = details[5:]
switch = 3
if switch == 2:
details.append(letter)
if colon_counter == 2:
details_end_counter += 1
if switch == 2 and letter == ':':
colon_counter += 1
# detect highlights
if switch == 3 and letter == '=':
equals_counter += 1
if equals_counter == 10:
if debug6:
print("reached end of 3")
equals_counter = 0
highlight = highlight[8:(len(highlight)-4)]
switch = 4
if switch == 3 and letter != '=':
highlight.append(letter)
# compiling highlight block
if switch == 4:
# add to highlight counter
highlight_counter += 1
# debug before list join
if debug2:
print("---start of UNjoined highlight---")
print("Highlight counter:", highlight_counter)
print(title)
print(author)
print(details)
print(highlight)
print("equals_counter:", equals_counter)
print("---end of UNjoined highlight---")
# joining list into strings
title_string = ''.join([str(item) for item in title])
author_string = ''.join([str(item) for item in author])
details_string = ''.join([str(item) for item in details])
highlight_string =''.join([str(item) for item in highlight])
# debug after list join
if debug3:
print("---start of joined highlight-----")
print("Highlight_counter:", highlight_counter)
print(title_string)
print(author_string)
print(details_string)
print(highlight_string)
print("---end of joined highlight-----")
# replace special character
# ' = \xe2\x80\x99
character1 = '\\xe2\\x80\\x99'
if character1 in highlight_string:
if debug6:
print("character1 found")
highlight_string = highlight_string.replace(character1, "'")
# debug after replacing special character
if debug3:
print("---start of replaced highlight-----")
print("Highlight_counter:", highlight_counter)
print(title_string)
print(author_string)
print(details_string)
print(highlight_string)
print("---end of replaced highlight-----")
# get user session id
user_id = session["user_id"]
# input into user database
db.execute("INSERT INTO user_highlights_? (highlight_counter, title, author, details, highlights) VALUES (?, ?, ?, ?, ?)",
user_id, highlight_counter, title_string, author_string, details_string, highlight_string)
# clear temporary array data
title = []
author = []
details = []
highlight = []
# switch back and loop over new highlight
switch = 0
flash("Cleaning Successful!", 'flash_success')
return redirect("/format")
if request.method == "GET":
return render_template("clean.html")
return render_template("error.html", message="Clean Faied To Load")
@app.route("/format", methods = ['GET', 'POST'])
@login_required
def format():
if request.method == "GET":
# get user id
user_id = session["user_id"]
# query dictionary of table using db.execute
highlights_table = db.execute("SELECT * FROM user_highlights_?", user_id)
# query dictionary for unique books
title_dictionary = db.execute("SELECT DISTINCT title FROM user_highlights_?", user_id)
# for debugging same but considered distinct title
if debug4:
print("---SQL distinct titles---")
for title in title_dictionary:
print(title["title"])
return render_template("format.html", table=highlights_table, titles=title_dictionary)
if request.method == "POST":
# get user id
user_id = session["user_id"]
# check selected path
if request.form['format'] == 'delete-selected':
# debug to check for route
if debug5:
print("---delete-selected route choosen---")
# get checked boxes as a list
checkboxes = request.form.getlist("checkboxes")
if debug5:
print("checkboxes getlist : ", checkboxes)
# validate if checkboxes are empty
if not checkboxes:
flash("No Checkboxes Selected!", 'flash_error')
# query dictionary of table using db.execute
highlights_table = db.execute("SELECT * FROM user_highlights_?", user_id)
# query dictionary for unique books
title_dictionary = db.execute("SELECT DISTINCT title FROM user_highlights_?", user_id)
return render_template("format.html", table=highlights_table, titles=title_dictionary)
# delete highlights with that highlight_id
for highlight_id in checkboxes:
db.execute("DELETE FROM user_highlights_? WHERE highlight_id=?", user_id, highlight_id)
# flash successful delete
flash("Delete Successful!", 'flash_success')
# query dictionary of table using db.execute
highlights_table = db.execute("SELECT * FROM user_highlights_?", user_id)
# query dictionary for unique books
title_dictionary = db.execute("SELECT DISTINCT title FROM user_highlights_?", user_id)
return render_template("format.html", table=highlights_table, titles=title_dictionary)
if request.form['format'] == 'compile-selected':
if debug5:
print("---compile-selected route choosen---")
# get checked boxes as a list
checkboxes = request.form.getlist("checkboxes")
# validate if checkboxes are empty
if not checkboxes:
flash("No Checkboxes Selected!", 'flash_error')
# query dictionary of table using db.execute
highlights_table = db.execute("SELECT * FROM user_highlights_?", user_id)
# query dictionary for unique books
title_dictionary = db.execute("SELECT DISTINCT title FROM user_highlights_?", user_id)
return render_template("format.html", table=highlights_table, titles=title_dictionary)
if debug5:
print("checkboxes getlist : ", checkboxes)
# declare empty list
highlight_all_list = []
# query dictionary with highlight_id selected
for highlight_id in checkboxes:
highlight_listofdict = db.execute("SELECT * FROM user_highlights_? WHERE highlight_id=?", user_id, highlight_id)
highlight = highlight_listofdict[0]
highlight_list = []
highlight_list.append(highlight["title"])
highlight_list.append("\n")
highlight_list.append(highlight["highlights"])
highlight_string = ''.join([str(item) for item in highlight_list])
highlight_all_list.append(highlight_string)
if debug5:
print("highlight_all_list: ", highlight_all_list)
# compile all different highlights in a single string
compiled_highlights = '\n\n'.join(str(item) for item in highlight_all_list)
if debug5:
print("compiled_highlights: ", compiled_highlights)
# concatonate path with user_id and create file
path = "./static/temporarytxt/compiled_highlights_" + str(user_id) + ".txt"
# remove previous path
if os.path.exists(path):
os.remove(path)
# write string into text file
text_file = open(path, 'w')
text_file.write(compiled_highlights)
text_file.close()
# send file
return send_file(path, as_attachment=True)
if request.form['format'] == 'compile-by-title':
if debug5:
print("---compile-by-title route choosen---")
# request for title
title = request.form.get("titles")
# validate title
if not title:
flash("No Title Selected!", 'flash_error')
# query dictionary of table using db.execute
highlights_table = db.execute("SELECT * FROM user_highlights_?", user_id)
# query dictionary for unique books
title_dictionary = db.execute("SELECT DISTINCT title FROM user_highlights_?", user_id)
return render_template("format.html", table=highlights_table, titles=title_dictionary)
if debug5:
print("title: ", title)
# declare empty list
highlight_all_list = []
# query for all highlights with selected title
highlights_by_title_dict = db.execute("SELECT * FROM user_highlights_? WHERE title=?", user_id, title)
for highlight in highlights_by_title_dict:
highlight_list = []
# declare switch for first check
first_check_switch = 0
# check for requests
title_check = request.form.get("title_check")
if title_check:
first_check_switch += 1
if first_check_switch != 1:
highlight_list.append("\n")
highlight_list.append(highlight["title"])
author_check = request.form.get("author_check")
if author_check:
first_check_switch += 1
if first_check_switch != 1:
highlight_list.append("\n")
highlight_list.append(highlight["author"])
details_check = request.form.get("details_check")
if details_check:
first_check_switch += 1
if first_check_switch != 1:
highlight_list.append("\n")
highlight_list.append(highlight["details"])
# append highlight
first_check_switch += 1
if first_check_switch != 1:
highlight_list.append("\n")
highlight_list.append(highlight["highlights"])
# join list
highlight_string = ''.join([str(item) for item in highlight_list])
# join one highlight to all list
highlight_all_list.append(highlight_string)
if debug5:
print()
print("highlight_all_list: \n", highlight_all_list)
# compile all different highlights in a single string
compiled_highlights = '\n\n'.join(str(item) for item in highlight_all_list)
if debug5:
print()
print("compiled_highlights: \n", compiled_highlights)
# concatonate path with user_id and create file
path = "./static/temporarytxt/compiled_highlights_" + str(user_id) + ".txt"
# remove previous path
os.remove(path)
# write string into text file
text_file = open(path, 'w')
text_file.write(compiled_highlights)
text_file.close()
# send file
return send_file(path, as_attachment=True)
return render_template("error.html", message="Format Failed To Load")
@app.route("/credit")
def credit():
return send_file("credits.txt", as_attachment=True)
if __name__ == "__main__":
port = os.environ.get("PORT", 5000) # Heroku will set the PORT environment variable for web traffic
app.run(debug=False, host="0.0.0.0", port=port)