-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
198 lines (185 loc) · 7.18 KB
/
main.py
File metadata and controls
198 lines (185 loc) · 7.18 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
from flask import Flask, render_template,request,redirect,url_for,session
import sqlite3
import os
current = ""
adminCh = False
DevKey = 'UvqPf2fGbH2YoWaetp1bA'
app = Flask(__name__)
app.secret_key = '\xf0\xa5\x9ewe6\x82RU\x8b\t\x0b\xb6\xcc\xf8\xb2\xdb\x02\x83\xab\x0f\x13\x15'
conn = sqlite3.connect("Users.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS users (first_name text NOT NULL,last_name text NOT NULL,email text PRIMARY KEY NOT NULL, password text NOT NULL, username text NOT NULL)")
c.execute("CREATE TABLE IF NOT EXISTS bullets (ID integer PRIMARY KEY AUTOINCREMENT, email text NOT NULL, username text NOT NULL, bullet text NOT NULL)")
c.close()
conn.close()
@app.route('/',methods=['GET', 'POST'])
def index():
conn = sqlite3.connect("Users.db")
c = conn.cursor()
if request.method == 'POST':
if request.form.get('first_name'):
fname = request.form['first_name']
lname = request.form['last_name']
email = request.form['email']
passw = request.form['password']
cpass = request.form['cpassword']
uname = request.form['username']
t = (email.lower(),)
c.execute("SELECT email FROM users WHERE email = ?", t)
checkEmail = c.fetchone()
if checkEmail != None:
c.close()
conn.close()
return render_template('signup.html', error = "Email already in use")
t = (uname.lower(),)
c.execute("SELECT username FROM users WHERE username = ?", t)
checkUser = c.fetchone()
if checkUser != None:
c.close()
conn.close()
return render_template('signup.html', error = "Username already in use")
c.execute("INSERT INTO users VALUES(?,?,?,?,?)",(fname,lname,email,passw,uname))
conn.commit()
c.close()
conn.close()
return redirect(url_for('signin'))
if current != '':
c.execute("SELECT * FROM bullets")
bulletList = c.fetchall()
print (bulletList)
return render_template('user.html', username = current, bullList = bulletList)
c.close()
conn.close()
return render_template('signup.html', error = '')
@app.route('/signin' ,methods=['GET','POST'])
def signin():
conn = sqlite3.connect("Users.db")
c = conn.cursor()
global current
if request.method == 'POST':
emai = request.form['email']
passc = request.form['password']
if emai == "admin" and passc == "admin1234":
c.close()
conn.close()
global adminCh
adminCh = True
return redirect(url_for('admin'))
else:
t = (emai.lower(),)
c.execute("SELECT email, username, password FROM users WHERE email = ?",t)
checkLogin = c.fetchone()
if checkLogin == None:
c.execute("SELECT email, username, password FROM users WHERE lower(username) = ?",t)
checkLogin = c.fetchone()
if checkLogin != None:
if checkLogin[2] == passc:
c.close()
conn.close()
current = checkLogin[1]
session['logged_in'] = True
session['username'] = checkLogin[0]
return redirect(url_for('index'))
else:
c.close()
conn.close()
return render_template('signin.html', error = "Username and password do not match.")
c.close()
conn.close()
return render_template('signin.html', error = "Username or Email address doesn't exist. Signup first.")
if checkLogin[2] == passc:
c.close()
conn.close()
current = checkLogin[1]
session['logged_in'] = True
session['username'] = checkLogin[0]
return redirect(url_for('index'))
c.close()
conn.close()
return render_template('signin.html', error = "Email and password do not match.")
if current != '':
c.close()
conn.close()
return redirect(url_for('index'))
c.close()
conn.close()
return render_template('signin.html', error = '')
@app.route('/addBulletin', methods = ['GET', 'POST'])
def addBulletin():
if current == '':
return redirect(url_for('index'))
conn = sqlite3.connect("Users.db")
c = conn.cursor()
if request.method == 'POST':
content = request.form['content']
t = session['username']
c.execute("INSERT INTO bullets (email,username,bullet) VALUES(?,?,?)",(t,current,content))
conn.commit()
c.close()
conn.close()
return redirect(url_for('index'))
return render_template('addBull.html', username = current)
@app.route('/admin', methods = ['GET', 'POST'])
def admin():
if request.method == 'POST':
tempId = int(request.form['submit'])
conn = sqlite3.connect('Users.db')
c = conn.cursor()
c.execute('DELETE FROM bullets WHERE ID = ?',(tempId,))
conn.commit()
c.execute("SELECT * FROM bullets")
bulletList = c.fetchall()
return render_template('admin.html', username = "admin", bulletList = bulletList)
global admin
if adminCh == True:
conn = sqlite3.connect('Users.db')
c = conn.cursor()
c.execute("SELECT * FROM bullets")
bulletList = c.fetchall()
print (bulletList)
return render_template('admin.html', username="admin", bulletList = bulletList)
else:
return redirect(url_for('signin'))
@app.route('/edit', methods = ['GET', 'POST'])
def edit():
if request.method == 'POST':
tempId = session['tempId']
content = request.form['content']
conn = sqlite3.connect('Users.db')
c = conn.cursor()
c.execute("UPDATE bullets SET bullet = ? WHERE ID = ?",(content,tempId))
conn.commit()
c.close()
conn.close()
return redirect(url_for('index'))
if current == '':
return redirect(url_for('index'))
return render_template('edit.html', ID = session['tempId'])
@app.route('/profile', methods = ['GET', 'POST'])
def profile():
if request.method == 'POST':
session['tempId'] = int(request.form['submit'])
return redirect(url_for('edit'))
if current == '':
return redirect(url_for('index'))
conn = sqlite3.connect("Users.db")
c = conn.cursor()
u = (session['username'],)
c.execute("SELECT * FROM users WHERE email = ?",u)
userData = c.fetchone()
c.execute("SELECT * FROM bullets WHERE email = ?",u)
bulletData = c.fetchall()
print(userData)
print(bulletData)
return render_template('profile.html', username = current, userData = userData, bulletData = bulletData)
@app.route('/logout')
def logout():
session.pop('username', None)
session['logged_in'] = False
global adminCh
global current
adminCh = False
current = ''
return redirect(url_for('signin'))
if __name__ == '__main__':
app.run(debug = True)