-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
199 lines (163 loc) · 5.81 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
from datetime import date
from authlib.integrations.flask_client import OAuth
from flask import Flask, jsonify, redirect, request
from flask import session as flask_session
from flask import url_for
from flask_cors import CORS
from flask_migrate import Migrate
from config import config
from models import Book, Contest, ContestAdmin, IndexPage, Session, User, engine
app = Flask(__name__)
app.secret_key = config["APP_SECRET_KEY"]
CORS(app, origins="*", supports_credentials=True)
oauth = OAuth(app)
oauth.register(
name=config["APP_NAME"],
client_id=config["CONSUMER_KEY"],
client_secret=config["CONSUMER_SECRET"],
access_token_url="https://commons.wikimedia.org/w/rest.php/oauth2/access_token",
authorize_url="https://commons.wikimedia.org/w/rest.php/oauth2/authorize",
api_base_url="https://commons.wikimedia.org/w",
client_kwargs={},
)
migrate = Migrate(app, db)
ws_contest = oauth.create_client("ws test 5")
def _str(val):
"""
Ensures that the val is the default str() type for python2 or 3
"""
if str == bytes:
if isinstance(val, str):
return val
else:
return str(val)
else:
if isinstance(val, str):
return val
else:
return str(val, "ascii")
@app.route("/api/login")
def login():
return ws_contest.authorize_redirect()
@app.route("/api/logout")
def logout():
flask_session.clear()
if "next" in request.args:
return redirect(request.args["next"])
return jsonify({"status": "logged out"})
@app.route("/oauth-k")
def authorize():
token = ws_contest.authorize_access_token()
if token:
resp = ws_contest.get("/w/rest.php/oauth2/resource/profile", token=token)
resp.raise_for_status()
profile = resp.json()
print(profile)
flask_session['profile'] = profile
return redirect("http://localhost:5173/contest")
@app.before_request
def force_https():
if request.headers.get("X-Forwarded-Proto") == "http":
return redirect(
"https://" + request.headers["Host"] + request.headers["X-Original-URI"],
code=301,
)
def get_current_user(cached=True):
if cached:
print(flask_session)
@app.route("/api/graph-data", methods=["GET"])
def graph_data():
return jsonify("graph data here")
@app.route("/api/contest/create", methods=["POST"])
def create_contest():
get_current_user(True)
if request.method == "POST":
try:
data = request.json
session = Session()
contest = Contest(
name=data["name"],
created_by=get_current_user(),
start_date=date.fromisoformat(data["start_date"]),
end_date=date.fromisoformat(data["end_date"]),
status=True,
point_per_proofread=int(data["proofread_points"]),
point_per_validate=int(data["validate_points"]),
lang=data["language"],
)
session.add(contest)
book_names = data.get("book_names").split("\n")
for book in book_names:
session.add(Book(name=book.split(":")[1], contest=contest))
admins = data.get("admins").split("\n")
for admin_name in admins:
admin = (
session.query(ContestAdmin).filter_by(user_name=admin_name).first()
)
if admin:
admin.contests.append(contest)
else:
session.add(ContestAdmin(user_name=admin_name, contests=[contest]))
session.commit()
return jsonify({"success": True}), 200
except Exception as e:
return jsonify({"success": False, "message": str(e)}), 404
@app.route("/api/contests", methods=["GET"])
def contest_list():
session = Session()
contests = (
session.query(Contest)
.with_entities(
Contest.name, Contest.start_date, Contest.end_date, Contest.status
)
.all()
)
return (
jsonify(
[
{
"name": name,
"start_date": start_date.strftime("%d-%m-%Y"),
"end_date": end_date.strftime("%d-%m-%Y"),
"status": status,
}
for name, start_date, end_date, status in contests
]
),
200,
)
@app.route("/api/contest/<int:id>")
def contest_by_id(id):
session = Session()
contest = session.get(Contest, id)
if not contest:
return jsonify("Contest with this id does not exist!"), 404
else:
data = {}
data["contest_details"] = contest
data["adminstrators"] = [admin.user_name for admin in contest.admins]
data["books"] = [book.name for book in contest.books]
data["users"] = []
for user in session.query(User).filter(User.cid == id).all():
proofread_count = len(user.proofread_pages)
validated_count = len(user.validated_pages)
points = (proofread_count * contest.point_per_proofread) + (
validated_count * contest.point_per_validate
)
data["users"].append(
{
user.user_name: {
"proofread_count": proofread_count,
"validated_count": validated_count,
"points": points,
"pages": session.query(IndexPage)
.filter(
(IndexPage.validator_username == user.user_name)
| (IndexPage.proofreader_username == user.user_name)
).all(),
}
}
)
return jsonify(data), 200
if __name__ == "__main__":
app.run(debug=True)