-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
266 lines (203 loc) · 8.5 KB
/
main.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
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
from flask import Flask, render_template, request, redirect, session, jsonify, send_from_directory
import requests
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash
import os
import uuid
import random
import datetime
# Create the application instance
app = Flask(__name__, template_folder='templates')
app.config["SECRET_KEY"] = uuid.uuid4().hex
HOST = "https://pine.jonathan2018.repl.co"
demo_mode = True
@app.before_request
def make_session_permanent():
session.permanent = True
# Activate Demo Version
if demo_mode:
session['logged_in'] = True
session['picture'] = "/static/images/icons/pfp.jpg"
session['username'] = "demouser"
session['email'] = "[email protected]"
# Functions
def generate_project_id():
random_id = ''
for i in range(15):
random_id += random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("SELECT * FROM projects WHERE id=:id", {"id": random_id})
if c.fetchone() is not None:
conn.close()
return generate_project_id()
conn.close()
return random_id
# Index Page
@app.route('/')
def index():
if 'logged_in' in session:
return redirect('/discover')
return render_template('index.html')
@app.route('/logout')
def logout():
session.clear()
return redirect('/')
@app.route('/dashboard')
def dashboard():
if 'logged_in' not in session:
return redirect('/login?destination=' + request.path)
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("SELECT id, name, date FROM projects WHERE creator=:username", {"username": session['username']})
projects = list(reversed(c.fetchall()))
conn.commit()
conn.close()
return render_template('dashboard.html', projects=projects)
@app.route('/discover', methods=['POST', 'GET'])
def discover():
if 'logged_in' not in session:
return redirect('/login')
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
projects = c.execute("SELECT * FROM projects WHERE id IS NOT 'hello'").fetchall()
conn.commit()
conn.close()
random.shuffle(projects)
return render_template('discover.html', projects=projects)
@app.route('/projects/create', methods=['POST', 'GET'])
def create_project():
if 'logged_in' not in session:
return redirect('/login?destination=' + request.path)
if request.method == 'POST':
name = request.form.get('project-name')
creator = session['username']
email = request.form.get('project-email')
image = request.form.get('project-image') or '/static/images/default-banner.png'
location = request.form.get('project-location')
project_date = request.form.get('project-date')
description = request.form.get('project-description')
project_id = generate_project_id()
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("INSERT INTO projects VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", (project_id, name, creator, email, image, location, project_date, description, session['username']))
conn.commit()
conn.close()
return redirect('/project/'+project_id)
current_date = datetime.date.today() - datetime.timedelta(1)
return render_template('create_project.html', current_date=current_date)
@app.route('/edit/<project_id>', methods=['POST', 'GET'])
def edit_project(project_id):
if 'logged_in' not in session:
return redirect('/login?destination=' + request.path)
if request.method == 'POST':
name = request.form.get('project-name')
email = request.form.get('project-email')
location = request.form.get('project-location')
project_date = request.form.get('project-date')
description = request.form.get('project-description')
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("SELECT creator FROM projects WHERE id=:project_id", {"project_id": project_id})
if c.fetchone()[0] != session['username']:
return "You can't edit a project that you don't own."
else:
pass
c.execute("UPDATE projects SET name=:name, email=:email, location=:location, date=:project_date, description=:description WHERE id=:project_id", {"name": name, "email": email, "location": location, "project_date": project_date, "description": description, "project_id": project_id})
conn.commit()
conn.close()
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
project_data = c.execute("SELECT * FROM projects WHERE id=:id", {"id": project_id}).fetchone()
if not project_data:
conn.commit()
conn.close()
return redirect('/dashboard')
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
project_data = c.execute("SELECT * FROM projects WHERE id=:id", {"id": project_id}).fetchone()
participant_count = len(project_data[8].split("|"))
conn.commit()
conn.close()
current_date = datetime.date.today() - datetime.timedelta(1)
return render_template("edit.html", project=project_data, participant_count=participant_count, current_date=current_date)
@app.route('/participants/<project_id>')
def participants(project_id):
if 'logged_in' not in session:
return redirect('/login?destination=' + request.path)
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
creator = c.execute("SELECT creator FROM projects WHERE id=:project_id", {"project_id": project_id}).fetchone()[0]
if session['username'] != creator:
return "You can't view the participants of a project if you don't own it."
project = c.execute("SELECT id, name FROM projects WHERE id=:project_id", {"project_id": project_id}).fetchone()
participants = c.execute("SELECT participants FROM projects WHERE id=:project_id", {"project_id": project_id}).fetchone()[0].split("|")
emails = []
for participant in participants:
emails.append(c.execute("SELECT email FROM users WHERE username=:username", {"username": participant}).fetchone()[0])
conn.commit()
conn.close()
return render_template('participants.html', project=project, participants=participants, emails=emails)
@app.route('/project/<project_id>')
def project(project_id):
if 'logged_in' not in session:
return redirect('/login?destination=' + request.path)
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
project_data = c.execute("SELECT * FROM projects WHERE id=:id", {"id": project_id}).fetchone()
if not project_data:
conn.commit()
conn.close()
return redirect('/discover')
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
project_data = c.execute("SELECT * FROM projects WHERE id=:id", {"id": project_id}).fetchone()
participant_count = len(project_data[8].split("|"))
participant_joined = "false"
if session['username'] in project_data[8].split("|"):
participant_joined = "true"
conn.commit()
conn.close()
return render_template('project.html', project=project_data, participant_count=participant_count, participant_joined=participant_joined)
@app.route('/api/v1/participate')
def participate():
if 'logged_in' not in session:
return redirect('/login')
project_id = request.args.get('project_id')
username = session['username']
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
participants = c.execute("SELECT participants FROM projects WHERE id=:project_id", {"project_id": project_id}).fetchone()[0].split("|")
participants.append(username)
print(participants)
participants = "|".join(participants)
print(participants)
c.execute("UPDATE projects SET participants=:participants WHERE id=:project_id", {"participants": participants, "project_id": project_id})
conn.commit()
conn.close()
return jsonify({"status": "success", "message": "Joined"})
@app.route('/api/v1/remove/<project_id>')
def remove_participant(project_id):
if 'logged_in' not in session:
return redirect('/login')
remove_user = request.args.get('remove')
if remove_user == session['username']:
return jsonify({"status": "failed", "message": "Cannot remove yourself"})
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("SELECT creator FROM projects WHERE id=:project_id", {"project_id": project_id})
if c.fetchone()[0] != session['username']:
conn.commit()
conn.close()
return jsonify({"status": "failed", "message": "Invalid permissions"})
else:
pass
participant_list = c.execute("SELECT participants FROM projects WHERE id=:project_id", {"project_id": project_id}).fetchone()[0].split("|")
participant_list.remove(remove_user)
participant_list = '|'.join(participant_list)
c.execute("UPDATE projects SET participants=:participant_list WHERE id=:project_id", {"project_id": project_id, "participant_list": participant_list})
conn.commit()
conn.close()
return jsonify({"status": "success", "message": "Participant Removed"})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)