This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
909 lines (660 loc) · 27.8 KB
/
Copy pathapplication.py
File metadata and controls
909 lines (660 loc) · 27.8 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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
""" Flask main application. """
import os
import csv
import re
import datetime
import logging
from flask import Flask, \
render_template, send_file, url_for, redirect, flash, request, send_from_directory, \
abort, session, current_app, make_response
from flask_session import Session
from functools import wraps
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash
from werkzeug.utils import secure_filename
import requests
from reminders import complete_reminder
from twilio.request_validator import RequestValidator
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from io import BytesIO
# from selenium.webdriver import Firefox
# from selenium.webdriver.firefox.options import Options
# from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import psycopg2
import psycopg2.extras
from helpers import \
allowed_file, style_metar, count_pageview, retrieve_pageviews, \
file_store, file_retrieve, fetch_metar
# import threading
# import discord
from dotenv import load_dotenv
load_dotenv()
# Import Authorized User List
authusers = []
authusers.append(os.getenv('USERA'))
logging.basicConfig(level=logging.INFO)
# Flask App
app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY')
# PostgreSQL database connection
db = os.getenv('DATABASE_URL')
# Twilio configuration
messaging_service_sid = os.getenv('TWILIO_MESSAGING_SERVICE_SID')
account_sid = os.getenv('TWILIO_ACCOUNT_SID')
auth_token = os.getenv('TWILIO_AUTH_TOKEN')
number_turk = os.getenv('NUMBER_TURK')
number_cluck = os.getenv('NUMBER_CLUCK')
client = Client(account_sid, auth_token)
# App - Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config["SESSION_PERMANENT"] = False
# app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(hours=0.001)
app.config["SESSION_TYPE"] = "filesystem"
# app.config["SESSION_TYPE"] = "null"
app.config['UPLOAD_FOLDER'] = os.getenv('PWD') + "/static/uploads"
Session(app)
### DECORATORS ###
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/1.0/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
def validate_twilio_request(f):
# https://www.twilio.com/docs/usage/tutorials/how-to-secure-your-flask-app-by-validating-incoming-twilio-requests#
"""Validates that incoming requests genuinely originated from Twilio"""
@wraps(f)
def decorated_function(*args, **kwargs):
# Create an instance of the RequestValidator class
validator = RequestValidator(os.environ.get('TWILIO_AUTH_TOKEN'))
# Validate the request using its URL, POST data,
# and X-TWILIO-SIGNATURE header
request_valid = validator.validate(
request.url,
request.form,
request.headers.get('X-TWILIO-SIGNATURE', ''))
# Continue processing the request if it's valid, return a 403 error if
# it's not
if request_valid:
return f(*args, **kwargs)
else:
return abort(403)
return decorated_function
def send_text(recipient, message):
""" Sends Twilio message. """
message = client.messages.create(
body=message,
messaging_service_sid=messaging_service_sid,
to=recipient
)
# ### SELENIUM ###
# def load_driver():
# """ Loads Selenium driver. """
# # https://elements.heroku.com/buildpacks/pyronlaboratory/heroku-integrated-firefox-geckodriver
# options = webdriver.FirefoxOptions()
# # enable trace level for debugging
# options.log.level = "trace"
# options.add_argument("-remote-debugging-port=9224")
# options.add_argument("-headless")
# options.add_argument("-disable-gpu")
# options.add_argument("-no-sandbox")
# binary = FirefoxBinary(os.environ.get('FIREFOX_BIN'))
# firefox_driver = webdriver.Firefox(
# firefox_binary=binary,
# executable_path=os.environ.get('GECKODRIVER_PATH'),
# options=options)
# return firefox_driver
# def scrape():
# """ Web scraping functions for Selenium. """
# # Selenium options
# # opts = Options()
# # opts.set_headless()
# # assert opts.headless # Operating in headless mode
# # browser = Firefox(options=opts)
# browser = load_driver()
# browser.get('https://www.turkosaur.us')
# # Search and return results
# element = browser.find_element_by_id('name')
# element.send_keys('testname')
# element = browser.find_element_by_id('email')
# element.send_keys('test@email.com')
# element = browser.find_element_by_id('message')
# element.send_keys('Looks like we made it. 🐱🐉')
# element.submit()
# browser.close()
# # results = browser.find_elements_by_class_name('result_body')
# # print(results[0].text)
# # print(results[0].text)
# return 0
### HELPERS ###
def send_msg(recipient, message):
""" Sends Twilio text message. """
message = client.messages.create(
body = message,
# status_callback='https://www.turkosaur.us/message/status',
messaging_service_sid=messaging_service_sid,
to = recipient
)
return 0
@app.route("/backup/<table>")
@login_required
def backup(table):
print("BACKUP")
try:
cur = conn.cursor()
cur.execute(f"SELECT * FROM {table} ORDER BY id ASC;")
data = cur.fetchall()
cur.close()
with open(f'{table}.csv', 'a', encoding="utf8") as file:
writer = csv.writer(file)
for line in data:
writer.writerow(line)
flash("Backup complete.")
return redirect("/admin")
except Exception as e:
flash(e)
return redirect("/admin")
### ROUTES ###
# @app.route("/test")
# @login_required
# def test():
# return redirect('/')
@app.route("/")
def home():
count_pageview('/')
return render_template("index.html")
@app.route("/status")
def status():
return render_template("index.html")
@app.route("/admin")
@login_required
def admin():
count_pageview('/admin')
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
#TODO this is probably wrong, results is none, but records populates
cur.execute("SELECT * FROM logs")
logs = cur.fetchall()
pageviews = retrieve_pageviews()
conn.close()
return render_template("admin.html", logs=logs, pageviews=pageviews)
# @app.route("/admin/<action>", methods=['POST'])
# @login_required
# def admin_edit(action):
# if action == 'resume':
# version = request.form.get('version')
@app.route("/keg")
def keg():
"""
Keg Weight Worksheet
"""
count_pageview('/keg')
scripts = "static/keg.js"
return render_template("keg.html", scripts=scripts)
@app.route("/ping/<content>", methods=['GET'])
def ping(content):
recipient = number_turk
message = f"turkosaurus message\ncontent:\n{content}"
send_text(recipient, message)
flash("Thank you. We'll be in touch soon!")
return redirect('/')
@app.route("/message", methods=['POST'])
@validate_twilio_request
def message_received():
"""
Processes incoming Twilio messages
"""
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
# Testing environment:
if os.getenv('FLASK_DEBUG') == '1':
body = """
lorem ipsum
foo foo froofy foo
"""
else:
# Send a dynamic reply to an incoming text message
# Get the message the user sent our Twilio number
body = request.values.get('Body', None)
num_from = request.values.get('From', None)
MessageSid = request.values.get('MessageSid', None)
NumMedia = request.values.get('NumMedia', None) # The number of media items associated with your message
# TODO consolidate into logging function
cur.execute("SELECT * FROM numbers WHERE number=%(num_from)s", {'num_from': num_from})
existing = cur.fetchone()
if not existing:
time_now = datetime.datetime.utcnow().isoformat()
cur.execute("INSERT INTO numbers (number, notify, created_on) \
VALUES (%s, %s, %s)",
(num_from, 'False', time_now))
cur.execute("SELECT num_id \
FROM numbers \
WHERE number=%(num_from)s",
{'num_from': num_from})
num_id = cur.fetchone()[0]
print(f"Creating activity for num_id:{num_id}")
time_now = datetime.datetime.utcnow().isoformat()
activity = "message"
cur.execute("INSERT INTO activity (num_id, timestamp, activity, messagesid) \
VALUES (%s, %s, %s, %s)",
(num_id, time_now, activity, MessageSid))
conn.commit()
cur.close()
print("Message received:")
print(f"from: {num_from}\n")
print(f"body: {body}")
# Start our TwiML response
resp = MessagingResponse()
response_match = False
metar_words = ['METAR', 'Metar', 'metar']
if any(x in body for x in metar_words):
response_match = True
# Find all airports in text
airports = re.findall("([Kk]...)", body)
# airports = airports.strip()
print(f"Found airports: {airports}")
# TODO iterate to loop through each airport that matches
metar = fetch_metar(airports[0])
resp.message(f"METAR {airports[0]}\n{metar}")
if body == "yes" or "Yes" or "YES":
success = complete_reminder(num_from)
if success:
response_match = True
# if 'Punch time' in body:
# response_match = True
# # timedata = punch()
# resp.message(f"Time Card\n---\n{timedata}")
if response_match is False:
resp.message(f"MENU\n✈ Reply 'METAR K---' for aviation weather\n")
# Check for Main Menu
# menu_words = ['INFO', 'Info' 'info', 'MENU', 'Menu', 'menu', 'OPTIONS', 'Options', 'options']
# if any(x in body for x in menu_words):
# response_match = True
# resp.message(f'''
# __MENU__
# METAR K___
# ''')
# "Punch time/in/out"\n
conn.close()
return str(resp)
@app.route("/message/status", methods=['GET', 'POST'])
def message_status():
""" Callback for Twilio message statuses. """
# https://www.twilio.com/docs/sms/tutorials/how-to-confirm-delivery-python
message_sid = request.values.get('MessageSid', None)
message_status = request.values.get('MessageStatus', None)
num_from = request.values.get('From', None)
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
# Find existing number id, or create new one if not existing
cur = conn.cursor()
cur.execute("SELECT * FROM numbers WHERE number=%(num_from)s", {'num_from': num_from})
existing = cur.fetchone()
if not existing:
time = datetime.datetime.utcnow().isoformat()
cur.execute("INSERT INTO numbers (number, notify, created_on) VALUES (%s, %s, %s)", (num_from, 'False', time))
# TODO move this up and consolidate, as it's a redundant query
cur.execute("SELECT num_id FROM numbers WHERE number=%(num_from)s", {'num_from': num_from})
num_id = cur.fetchone()[0]
print(f"Creating activity for num_id:{num_id}")
time = datetime.datetime.utcnow().isoformat()
activity = "callback"
cur.execute("INSERT INTO activity (num_id, timestamp, activity, messagesid, status) VALUES (%s, %s, %s, %s, %s)", (num_id, time, activity, message_sid, message_status))
conn.commit()
cur.close()
conn.close()
return ('', 204)
@app.route("/metar", methods=['GET', 'POST'])
def metar():
"""
Servers
"""
# https://www.psycopg.org/docs/usage.html
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
count_pageview('/metar')
if request.method == 'GET':
return render_template('cluck.html')
# POST
else:
number = request.form.get("number")
notify = request.form.get("notify")
if notify != 'true':
notify = False
print(f"number:{number}")
# Convert to E.164 per
# https://www.twilio.com/docs/lookup/tutorials/validation-and-formatting#validate-a-national-phone-number
try:
phone_number = client.lookups \
.v1 \
.phone_numbers(number) \
.fetch(country_code='US')
except:
flash("Invalid Number")
else:
number = phone_number.phone_number
print(f"received text from:{number}")
# cur.execute("SELECT * FROM numbers")
cur.execute("SELECT * FROM numbers WHERE number=%(number)s", {'number': number})
existing = cur.fetchall()
print(f"existing:{existing}")
if not existing:
time = datetime.datetime.utcnow().isoformat()
cur.execute("INSERT INTO numbers (number, notify, created_on) VALUES (%s, %s, %s)", (number, notify, time))
elif notify == 'true':
cur.execute("UPDATE numbers SET notify = 'true' WHERE number=%(number)s", {'number': number})
elif notify == 'false':
cur.execute("UPDATE numbers SET notify = 'true' WHERE number=%(number)s", {'number': number})
conn.commit()
cur.close()
msg_signup = "Thanks for signing up! Reply 'METAR KAUS' or 'METAR KLAX' for weather."
send_msg(number, msg_signup)
flash("Thank you. We'll be in touch soon!")
return redirect("/metar")
@app.route("/metar/now", methods=['POST'])
def metar_button():
""" Delivers METAR to browser via Flash message. """
airport = request.form.get("airport")
metar_result = fetch_metar(airport)
flash(f"{metar_result}")
return redirect("/metar")
@app.route("/data", methods=['GET', 'POST'])
def data():
""" Processes submitted csv and returns changes. """
count_pageview('/data')
data = []
if request.method == 'GET':
return render_template("data.html", data=data)
else:
delivery = request.form.get('delivery')
# https://flask.palletsprojects.com/en/2.0.x/patterns/fileuploads/
# check if the post request has the file part
if 'inputfile' not in request.files:
flash('No file part')
return redirect("/data")
file = request.files['inputfile']
# If the user does not select a file, the browser submits an empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect("/data")
if file and allowed_file(file.filename):
filename_user = secure_filename(file.filename) # User supplied filenames kept
# filename = 'tmp.csv'
print(f"filename:{filename_user}")
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename_user))
# Read into a SQL table
with open(f'static/uploads/{filename_user}', 'r', encoding="utf8") as csvfile:
print(f'Reading {filename_user}...')
csv_reader = csv.reader(csvfile)
# next(csv_reader)
row_counter = 0
negatives = []
for row in csv_reader:
data.append(row)
for row in data:
print(row)
for i in range(len(row)):
try:
if int(row[i]) < 0:
print(f"found a negative value ({row[i]}) on row {row_counter}")
row[i] = abs(int(row[i]))
negatives.append(row_counter + 1)
except:
print("EXCEPTION")
row_counter += 1
# TODO save as binary object
# https://www.postgresqltutorial.com/postgresql-python/blob/
with open(f'static/uploads/{filename_user}', 'w', encoding="utf8") as csvfile:
scribe = csv.writer(csvfile)
for row in data:
scribe.writerow(row)
flash(f"File uploaded. {row_counter} rows assessed.")
if negatives:
flash(f"Negative values found and converted on row {negatives}")
if delivery == 'download':
return send_from_directory(app.config['UPLOAD_FOLDER'], filename=filename_user, attachment_filename=filename_user, as_attachment=True, mimetype='text/csv')
if delivery == 'view':
return render_template("data.html", data=data)
return redirect('/data')
@app.route("/resume")
def resume():
""" Delivers resume. """
count_pageview(f'/resume')
# TODO add authorizations/click tracking
# https://www.psycopg.org/docs/usage.html
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
# Find most recent public resume
cur = conn.cursor()
cur.execute("SELECT id FROM files WHERE description=%s ORDER by uploaded DESC LIMIT 1;", ["resume_public"])
id = cur.fetchone()[0]
print(f"Most recent resume_public: id = {id}")
cur.close()
file = file_retrieve(id)
# # LOCAL - Create new local file
# with open("static/uploads/resume-recent.pdf", "wb") as f:
# print((file['filedata']))
# f.write(file['filedata'])
# EMBED
# https://stackoverflow.com/questions/18281433/flask-handling-a-pdf-as-its-own-page
# https://flask.palletsprojects.com/en/2.0.x/api/#flask.make_response
response = make_response(bytes(file['filedata']))
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline; filename=%s' % file['filename']
return response
@app.route("/<shorturl>")
def short_url(shorturl):
""" Catches all other URL requests, can be used for temp URLs. """
count_pageview(shorturl)
#URL shortener
print(f"short_url:{shorturl}")
return redirect("/")
# @app.route("/secret/<key>")
# def secret(key):
# count_pageview(f"secret/{key}")
# # Lookup links table to enable logic here
# @app.route("/pdf")
# def pdf():
# count_pageview('/pdf')
# return send_file('static/resume-TravisTurk-web.pdf', mimetype='pdf', as_attachment=True, attachment_filename='resume.TravisTurk.pdf')
# @app.route("/file/<id>")
# @login_required
# def file(id):
# # Retrieve file from database, generate file object from data using BytesIO()
# file = file_retrieve(id)
# data = BytesIO(file['filedata'])
# return send_file(data, mimetype=file['filetype'], as_attachment=True, attachment_filename=file['filename'])
@app.route("/upload", methods=['POST'])
@login_required
def pdf_upload():
""" Uploads a file to the database. """
count_pageview('/upload')
version = request.form.get('version')
if version == 'public':
filename = 'resume_TravisTurk_web'
description = 'resume_public'
if version == 'private':
filename = 'resume_TravisTurk'
description = 'resume_private'
# https://flask.palletsprojects.com/en/2.0.x/patterns/fileuploads/
# check if the post request has the file part
if 'inputfile' not in request.files:
flash('No file part')
return redirect("/admin")
file = request.files['inputfile']
# filename_override = request.form.get('filename_override')
# If the user does not select a file, the browser submits an empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect("/admin")
print(file)
print(filename)
print(file.filename)
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print(allowed_file(file.filename))
# TODO reinstitute checks for public files
if file and allowed_file(file.filename):
# filename = secure_filename(file.filename) # original filenames kept
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
# else:
# flash('Upload Error')
# return redirect("/admin")
# # Read into a SQL table
# with open(f'static/uploads/{filename_user}', 'r') as csvfile:
# Default file metadata
uploaded = datetime.datetime.utcnow().isoformat()
filetype = "pdf" # TODO infer filetypes to make this more generic
# Open file, convert to bytes
file = open(f"{app.config['UPLOAD_FOLDER']}/{filename}.{filetype}", 'rb')
filedata = file.read()
file.close()
filedata = bytes(filedata)
# print(f"filedata:{filedata}")
# Insert into database
# https://www.psycopg.org/docs/usage.html
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
cur.execute("INSERT INTO files (uploaded, name, filetype, description, filedata) \
VALUES (%s, %s, %s, %s, %s)",
(uploaded, filename, filetype, description, filedata))
conn.commit()
cur.close()
conn.close()
flash(f"{filename}.{filetype} ({description})saved to database.")
return redirect('/admin.html')
###### USER ACCOUNTS ####
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
count_pageview('/register')
# Serve registration page
if request.method == 'GET':
return render_template("register.html")
# Process submitted form responses on POST
else:
# Error Checking
# Ensure username was submitted
if not request.form.get("username"):
flash("Username required.")
return redirect('/register')
# Ensure password was submitted
if not request.form.get("password"):
flash("Password required.")
return redirect('/register')
# Ensure password and password confirmation match
if request.form.get("password") != request.form.get("passwordconfirm"):
flash("Passwords must match.")
return redirect('/register')
# Ensure minimum password length
if len(request.form.get("password")) < 8:
flash("Password must be at least 8 characters.")
return redirect('/register')
# Store the hashed username and password
username = request.form.get("username")
hashedpass = generate_password_hash(request.form.get("password"))
if username not in authusers:
flash("Unauthorized user.")
return redirect('/')
# https://www.psycopg.org/docs/usage.html
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
# Check if username is already taken
cur.execute("SELECT username FROM users WHERE username LIKE %s;", [username])
matches = cur.fetchall()
if not matches:
# Add the username
time_now = datetime.datetime.utcnow().isoformat()
cur.execute("INSERT INTO users (username, password, created_on) VALUES (%s, %s, %s)",
[username, hashedpass, time_now])
conn.commit()
else:
flash("Username invalid or already taken.")
conn.close()
return redirect("/")
@app.route("/login", methods=["GET", "POST"])
def login():
count_pageview('/login')
"""Log user in"""
# 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 required")
return redirect("/login")
# Ensure password was submitted
elif not request.form.get("password"):
flash("Password Required")
return render_template("login.html")
username = request.form.get("username")
# https://www.psycopg.org/docs/usage.html
with psycopg2.connect(db) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) as cur:
# Query database for username
cur.execute("SELECT * FROM users WHERE username=%s;", [username])
rows = cur.fetchall()
# Ensure username exists
if len(rows) != 1:
print("loginerror")
flash("Login Error")
# conn.close()
return render_template("login.html")
# Ensure username exists and password is correct
if not check_password_hash(rows[0][2], request.form.get("password")):
# # TODO implement fancier backoff
# time.sleep(5)
print("passworderror")
flash("Password Error")
# conn.close()
return render_template("login.html")
# Remember which user has logged in
session["user_id"] = rows[0][0]
# Update "last_login"
lastlogin = datetime.datetime.utcnow().isoformat()
cur.execute("UPDATE users SET last_login=%s WHERE id=%s", [lastlogin, session["user_id"]])
conn.commit()
# conn.close()
# Redirect user to home page
flash(f"Welcome, {username}")
return redirect("/admin")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
count_pageview('/logout')
# Forget any user_id
session.clear()
# Redirect user to login form
flash("Session Cleared")
return redirect("/")
# if __name__ == '__main__':
# app.run()
# tWebpage = threading.Thread(target=webpage)
# tWebpage.start()
# tWebpage = threading.Thread(target=webpage)
# tWebpage.start()
# load_dotenv()
# def cluck():
# print("Waking Cluck...")
# TOKEN = os.getenv('DISCORD_TOKEN')
# client = discord.Client()
# @client.event
# async def on_ready():
# print(f'{client.user} has connected to Discord!')
# client.run(TOKEN)
# flash("Cluck is in the coop.")
# tCluck = threading.Thread(target=cluck)
# tCluck.start()