-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
204 lines (164 loc) · 6.91 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
import os, sys
import requests
import json
import logging
import pymongo
from pymongo import MongoClient
from bson.objectid import ObjectId
from datetime import datetime, timezone
from functools import wraps
from requests_oauthlib import OAuth2Session
from flask import Flask, render_template, url_for, redirect, g, request, session, send_from_directory, abort, jsonify
from flask_wtf.csrf import CSRFProtect
ADMINS = os.environ.get("ADMINS").split(',')
# DATABASE
DB_HOST = os.environ.get("DB_HOST")
DB_PORT = os.environ.get("DB_PORT")
DB_DB = os.environ.get("DB_DB")
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")
# REDDIT API
REDDIT_APP_ID = os.environ.get("REDDIT_APP_ID")
REDDIT_APP_SECRET = os.environ.get("REDDIT_APP_SECRET")
REDDIT_REDIRECT_URI = os.environ.get("REDDIT_REDIRECT_URI")
REDDIT_API_BASE_URL = "https://www.reddit.com/api/v1"
REDDIT_OAUTH_BASE_URL = "https://oauth.reddit.com/api/v1"
app = Flask(__name__)
csrf = CSRFProtect(app)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)
# app.logger.debug('debug')
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'this_should_be_configured')
mongo = MongoClient(host=DB_HOST, port=int(DB_PORT), username=DB_USER, password=DB_PASSWORD, authSource=DB_DB, authMechanism='SCRAM-SHA-256')
db = mongo[DB_DB]
def require_auth(f):
@wraps(f)
def wrapper(*args, **kwargs):
user = session.get('user', None)
if not user:
return redirect(url_for('login_reddit'))
if user['id'] not in ADMINS:
return 'ur not admin lmao'
return f(*args, **kwargs)
return wrapper
@app.route('/', methods=['GET', 'POST'])
def home():
now = datetime.utcnow()
voting_start = datetime(now.year if now.month % 12 else now.year + 1, now.month % 12 + 1, 1, tzinfo=timezone.utc).timestamp()
voting_end = datetime(now.year, now.month, 8, tzinfo=timezone.utc).timestamp()
if now.timestamp() <= voting_start and now.timestamp() < voting_end:
if session.get('user', None) and request.method == 'POST' and request.form.get('vote'):
vote = request.form.get('vote')
db.votes.update_one(
{"id": session.get("user")["id"]},
{"$set": {"vote": vote}},
upsert = True
)
current_vote = db.votes.find_one({"id": session.get("user")["id"]}) if session.get("user") else None
return render_template('voting.html',
session=session,
admins=ADMINS,
countdown_to=voting_end,
submissions=db.submissions.find({"$query": {}, "$orderby": {"title": pymongo.ASCENDING}}),
current_vote=ObjectId(current_vote["vote"]) if current_vote else None
)
else:
return render_template('winner.html',
session=session,
admins=ADMINS,
countdown_to=voting_start,
winner=db.submissions.find_one({"_id": ObjectId(count_votes()[0]["_id"])})
)
@app.route('/login')
def login_reddit():
# Check for state and for 0 errors
state = session.get('oauth2_state')
if request.values.get('error'):
return "\r\n".join([
f'<pre>'
f'There was an error authenticating with reddit: {request.values.get("error")}',
f'<a href="{url_for("logout")}">Return Home</a>'
f'</pre>'
])
if state and request.args.get('code'):
# Fetch token
client_auth = requests.auth.HTTPBasicAuth(REDDIT_APP_ID, REDDIT_APP_SECRET)
post_data = {"grant_type": "authorization_code", "code": request.args.get('code'), "redirect_uri": REDDIT_REDIRECT_URI}
reddit_token = requests.post(REDDIT_API_BASE_URL + "/access_token", auth=client_auth, data=post_data, headers={'User-agent': 'low_poly web auth, /u/RenegadeAI'}).json()
if not reddit_token or not 'access_token' in reddit_token:
return redirect(url_for('logout'))
# Fetch the user
user = requests.get(REDDIT_OAUTH_BASE_URL + "/me", headers={"Authorization": "bearer {}".format(reddit_token["access_token"]), 'User-agent': 'low_poly web, /u/RenegadeAI'}).json()
session['user'] = {key: user[key] for key in ['name', 'id']}
return redirect(url_for('home'))
else:
scope = ['identity']
reddit = make_reddit_session(scope=scope)
authorization_url, state = reddit.authorization_url(
REDDIT_API_BASE_URL + "/authorize",
access_type="offline"
)
session['oauth2_state'] = state
return redirect(authorization_url)
@app.route('/admin', methods=['GET', 'POST'])
@require_auth
def admin():
action = request.form.get('action', None)
_id = request.form.get('_id', None)
if request.method == 'POST' and action and _id:
if action == 'edit':
title = request.form.get('title', None)
image = request.form.get('image', None)
if title and image:
db.submissions.find_one_and_update(
{"_id": ObjectId(_id)},
{"$set": {'title': title, 'image': image}}
)
else: return 'Missing required arguments'
elif action == 'remove':
db.submissions.delete_one({"_id": ObjectId(_id)})
else: return 'Undefined action'
votes = {vote["_id"]: vote["count"] for vote in count_votes()}
submissions = []
for submission in db.submissions.find({"$query": {}, "$orderby": {"title": pymongo.ASCENDING}}):
submission["_id"] = str(submission["_id"])
submission["score"] = votes[submission["_id"]] if submission["_id"] in votes else 0
submissions.append(submission)
return render_template('admin.html',
session=session,
submissions=sorted(submissions, key=lambda k: k['score'], reverse=True)
)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
def count_votes():
_votes = db.votes.aggregate([
{
"$group": {
"_id": "$vote",
"count": {"$sum": 1}
}
},
{"$sort": { "count": -1}}
])
return list(_votes)
def to_json(value):
return json.dumps(value)
app.jinja_env.filters['to_json'] = to_json
def make_reddit_session(token=None, state=None, scope=None):
return OAuth2Session(
client_id=REDDIT_APP_ID,
token=token,
state=state,
scope=scope,
redirect_uri=REDDIT_REDIRECT_URI,
auto_refresh_kwargs={
'client_id':None,
'client_secret':None,
},
auto_refresh_url=None,
token_updater=None
)
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)