-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
220 lines (169 loc) · 6.6 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
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
from flask import Flask, redirect, url_for, render_template, request, flash, session
import sqlite3
import secrets
import smtplib
import ssl
from datetime import datetime
from email.message import EmailMessage
import time
import os
app = Flask(__name__)
# Generate a random secret key
app.secret_key = secrets.token_hex(16)
def send_email(email, password):
current_date_and_time = datetime.now()
email_sender = '*' # Enter provider server's email
email_password = '*' # Enter provider's password
email_receiver = str(email) # Enter admin email.
subject = 'Forget password... '
body = "current date and time : \n" + str(current_date_and_time)
body = "\nDon't send this email to someone⚠"
body = body + f"\npassword is : {password}"
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_receiver
em['Subject'] = subject
em.set_content(body)
# Add SSL (layer of security)
context = ssl.create_default_context()
# Log in and send the email
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_receiver, em.as_string())
@app.route('/', methods=['GET'])
def index():
return render_template("index.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# SQLite3
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
email = request.form['email']
password = request.form['password']
query = "SELECT email, password FROM users WHERE email=? AND password=?"
cursor.execute(query, (email, password))
result = cursor.fetchall()
if len(result) == 0:
return redirect(url_for('login'))
else:
# Save user info in session
session['email'] = email
session['password'] = password
return redirect(url_for('unique'))
# Check if the user is already logged in
if 'email' in session and 'password' in session:
return redirect(url_for('unique'))
return render_template("index.html")
@app.route('/unique')
def unique():
email = session.get('email')
password = session.get('password')
connection = sqlite3.connect("user_data.db")
query = "SELECT name FROM users WHERE email = ? AND password = ?"
cursor = connection.cursor()
cursor.execute(query, (email, password))
name = cursor.fetchone()
cursor.close()
connection.close()
connection = sqlite3.connect("user_content.db")
cursor = connection.cursor()
cursor.execute("SELECT users.title, users.content, users.name FROM users")
posts = cursor.fetchall()
connection.close()
# Check if the user is logged in
logged_in = 'email' in session and 'password' in session
logged_out = not logged_in
return render_template('main.html', name=name, posts=posts, logged_in=not logged_in, logged_out=logged_out)
@app.route('/createPost')
def create_post():
if 'email' in session and 'password' in session:
return render_template('createPost.html')
else:
return redirect(url_for('index'))
@app.route('/submit_post', methods=['POST'])
def submit_post():
if 'email' in session and 'password' in session:
title = request.form['title']
content = request.form['content']
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
print(session['email'])
cursor.execute("SELECT name FROM users WHERE email=? AND password=?", (session['email'],
session['password']))
result = cursor.fetchone()
cursor.close()
connection.close()
nameTmp = result[0]
connection = sqlite3.connect('User_Content.db')
cursor = connection.cursor()
cursor.execute("INSERT INTO users (name, email, title, content) VALUES (?, ?, ?, ?)",
(nameTmp, session['email'], title, content))
connection.commit()
return render_template('post_success.html', title=title)
else:
return redirect(url_for('index'))
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('index'))
@app.route('/register', methods=['POST'])
def register():
username = request.form['username']
email = request.form['email']
phone = request.form['phone']
password = request.form['pwd']
query = "SELECT email FROM users where email = '" + email + "'"
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
if len(result) == 0:
cursor.execute("INSERT INTO users (name, password, email, phone) VALUES (?, ?, ?, ?)",
(username, password, email, phone))
connection.commit()
else:
session['error'] = 'Email already exists.'
return redirect(url_for('index'))
connection.close()
return redirect(url_for('index'))
@app.route('/search')
def search():
search_query = request.args.get('search', '')
connection = sqlite3.connect("user_content.db")
cursor = connection.cursor()
cursor.execute("SELECT title, content, name FROM users WHERE title LIKE ?", ('%' + search_query + '%',))
search_results = cursor.fetchall()
connection.close()
return render_template('search.html', search_query=search_query, results=search_results)
@app.route('/user_count')
def user_count():
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM users")
result = cursor.fetchone()
connection.close()
return {'userCount': result[0]}
@app.route('/forgetpassword', methods=['GET', 'POST'])
def forget_password():
if request.method == 'POST':
email = request.form['gmail']
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
query = "SELECT password FROM users WHERE email=?"
cursor.execute(query, (email,))
result = cursor.fetchone()
cursor.close()
connection.close()
if result:
password = result[0]
flash("An email with your password has been sent to your email address.")
send_email(email, password)
time.sleep(5)
else:
flash("The entered email was not found.")
time.sleep(5)
return redirect(url_for('index'))
return render_template("forgetpassword.html")
if __name__ == '__main__':
app.run(debug=True)