This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (74 loc) · 2.98 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
import hashlib
import sqlite3
import uuid
from flask import Flask, render_template, request, redirect, session
import random
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
app = Flask(__name__)
app.secret_key = 'secret_key_goes_here'
# for session mgmt, since I'm not deploying rn I'm not super worried about a secure one
# Initialize the scheduler
scheduler = BackgroundScheduler()
scheduler.start()
# Function to clear the database
def clear_database():
session['has_posted'] = False
conn = sqlite3.connect('identifier.sqlite')
cursor = conn.cursor()
# Clear the 'posts' table
cursor.execute('TRUNCATE TABLE posts') # could also use truncate table
# Commit the changes and close the connection
conn.commit()
conn.close()
# Schedule the task to run daily at midnight UTC using cron trigger
scheduler.add_job(clear_database, trigger=CronTrigger(hour=0, minute=0, second=0, timezone='UTC'))
# this works well enough for a unique user id function for now
def generate_user_id():
# Get the machine's hardware address (MAC address)
mac = ':'.join(['{:02x}'.format((uuid.getnode() >> elements) & 0xff) for elements in range(0, 2 * 6, 2)][::-1])
mac = hashlib.sha256(mac.encode('utf-8')).hexdigest()
out_string = ""
for item in random.sample(mac, 8):
out_string = out_string + item
return out_string
@app.route('/')
def home():
session.setdefault('has_posted', False) # set flag to false by default
return render_template('home.html')
@app.route('/new_post')
def create_post():
return render_template('newpost.html')
@app.route('/view_post')
def view_post():
# Connect to the database
conn = sqlite3.connect('identifier.sqlite')
cursor = conn.cursor()
# Get a random post from the 'posts' table
cursor.execute('SELECT user_id, message FROM posts ORDER BY RANDOM() LIMIT 1')
result = cursor.fetchone()
# Close the connection
conn.close()
# If there are no posts in the database, set default values
user_id = "No User"
post = "No Post"
# Update user_id and post if there is a result
if result:
user_id, post = result
return render_template('viewpost.html', user_id=user_id, post=post)
@app.route('/submit_post', methods=['POST'])
def submit_post():
message = request.form['post_content']
user_id = 'user_' + generate_user_id() # Replace with the actual user_id based on your authentication system
# Connect to the database
conn = sqlite3.connect('identifier.sqlite')
cursor = conn.cursor()
# Insert the new post into the 'posts' table
cursor.execute('INSERT INTO posts (message, user_id) VALUES (?, ?)', (message, user_id))
# Commit the changes and close the connection
conn.commit()
conn.close()
session['has_posted'] = True
return redirect('/view_post') # Redirect to the home page or any other page after submitting the post
if __name__ == '__main__':
app.run()