Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 540cf48

Browse files
authored
Merge pull request #116 from codewizardshq/master
begin sync
2 parents a4c43ff + 422a7df commit 540cf48

File tree

22 files changed

+2484
-1770
lines changed

22 files changed

+2484
-1770
lines changed

CodeChallenge/api/eb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def worker():
3737
recipients=[current_app.config["MG_LIST"]])
3838

3939
msg.html = render_template("challenge_daily_email.html",
40-
name="%recipient_fname%")
40+
name="%recipient_fname%",
41+
external_url=current_app.config["EXTERNAL_URL"])
4142
msg.extra_headers = {"List-Unsubscribe": "%unsubscribe_email%"}
4243

4344
mail.send(msg)

CodeChallenge/api/questions.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ def answer_next_question():
105105

106106
data = request.get_json()
107107
text = data["text"]
108-
correct = str_cmp(text.lower(), q.answer.lower())
108+
109+
try:
110+
correct = str_cmp(text.casefold().strip(), q.answer.lower())
111+
except TypeError:
112+
return jsonify(status="success", correct=False)
113+
109114

110115
ans = Answer.query.filter_by(user_id=user.id, question_id=q.id).first()
111116

@@ -199,6 +204,10 @@ def answer_eval():
199204
return jsonify(status="error",
200205
reason="missing 'language' property in JSON body"), 400
201206

207+
if language not in ("js", "python"):
208+
return jsonify(status="error",
209+
reason="unsupported language. valid choices are 'js' or 'python'"), 400
210+
202211
# designated output variable for evaluation
203212
if language == "js":
204213
code += ";output"
@@ -220,13 +229,20 @@ def answer_eval():
220229
eval_error = eval_data["error"]
221230
eval_output = str(eval_data["output"])
222231

232+
if language == "python":
233+
eval_output = eval_output.rstrip() # remove trailing \n from print()
234+
223235
# any API error is an automatic failure
224236
if eval_error:
225237
return jsonify(status="success",
226238
correct=False,
227239
js_error=eval_error)
228240

229-
correct = str_cmp(eval_output, q.answer)
241+
try:
242+
correct = str_cmp(eval_output, q.answer)
243+
except TypeError:
244+
return jsonify(correct=False,
245+
status="success")
230246

231247
if request.json.get("checkOnly", False):
232248
return jsonify(correct=correct,

CodeChallenge/api/users.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
unset_jwt_cookies)
88
from flask_limiter.util import get_remote_address
99
from flask_mail import Message
10+
from requests import HTTPError
1011
from sqlalchemy import func
1112

1213
from .. import core
@@ -125,16 +126,22 @@ def register():
125126
)
126127

127128
mg_vars["type"] = "parent"
128-
mg_list_add(new_u.parent_email,
129-
f"{new_u.parentfirstname} {new_u.parentlastname}",
130-
data=mg_vars)
129+
try:
130+
mg_list_add(new_u.parent_email,
131+
f"{new_u.parentfirstname} {new_u.parentlastname}",
132+
data=mg_vars)
133+
except HTTPError as error:
134+
current_app.logger.exception(f"failed to add {new_u.parent_email!r} to mailing list: {error}")
131135

132136
# if provided, also add student to mailing list
133137
if new_u.student_email:
134138
mg_vars["type"] = "student"
135-
mg_list_add(new_u.student_email,
136-
f"{new_u.studentfirstname} {new_u.studentlastname}",
137-
data=mg_vars)
139+
try:
140+
mg_list_add(new_u.student_email,
141+
f"{new_u.studentfirstname} {new_u.studentlastname}",
142+
data=mg_vars)
143+
except HTTPError as error:
144+
current_app.logger.exception(f"failed to add {new_u.parent_email!r} to mailing list: {error}")
138145

139146
rcpts = [new_u.parent_email]
140147
if new_u.student_email:

CodeChallenge/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ProductionConfig(DefaultConfig):
7575
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
7676
SHEET_ID = os.getenv("SHEET_ID")
7777
MG_LIST = os.getenv("MG_LIST")
78+
ANSWER_ATTEMPT_LIMIT = "5 per 1 minutes"
7879

7980

8081
class DevelopmentConfig(ProductionConfig):

CodeChallenge/core.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, timezone, timedelta, time
1+
from datetime import datetime, timezone, timedelta
22

33
from flask import current_app
44
from sqlalchemy import func
@@ -20,7 +20,8 @@ def day_number() -> int:
2020
return -1
2121

2222
delta = now - start
23-
return 1 if delta.days == 0 else delta.days
23+
24+
return delta.days + 1
2425

2526

2627
def current_rank() -> int:
@@ -42,16 +43,8 @@ def time_until_next_rank() -> str:
4243
epoch = int(current_app.config["CODE_CHALLENGE_START"])
4344
start = datetime.fromtimestamp(epoch, timezone.utc)
4445
now = datetime.now(timezone.utc)
45-
46-
if start > now:
47-
diff = datetime.combine(start, time.min, now.tzinfo) - now
48-
return str(diff)
49-
50-
tomorrow = now + timedelta(days=1)
51-
52-
diff = datetime.combine(tomorrow, time.min, now.tzinfo) - now
53-
54-
return str(diff)
46+
next_date = start + timedelta(days=day_number())
47+
return str(next_date - now)
5548

5649

5750
def friendly_starts_on() -> str:

CodeChallenge/templates/challenge_daily_email.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,18 @@
301301
<td class="content-block">
302302
<h2>{{name}}, today's code challenge question is unlocked!</h2>
303303
<br/>
304-
<p>Continue your quest by <a href="https://challenge.codewizardshq.com/login"> logging in.</a></p>
304+
<p>Continue your quest by <a href="{{ external_url }}/login"> logging in.</a></p>
305305
<p>Good luck and safe travels! </p>
306306
<br/>
307-
<a href="https://challenge.codewizardshq.com/login" class="btn-primary">ANSWER TODAY'S QUESTION</a>
307+
<a href="{{ external_url }}/login" class="btn-primary">ANSWER TODAY'S QUESTION</a>
308308
</td>
309309
</tr>
310310
<tr>
311311
<td>
312-
<p><a href="https://challenge.codewizardshq.com/login">Login to Play</a><br/>
312+
<p><a href="{{ external_url }}/login">Login to Play</a><br/>
313313
<a href="http://codewizardshq.com/challenge">Challenge Details</a><br/>
314314
<a href="http://codewizardshq.com/challenge#challenge-prizes">Prizes</a><br/>
315-
<a href="https://challenge.codewizardshq.com/frequently-asked-questions">Frequently Asked Questions</a></p>
315+
<a href="{{ external_url }}/frequently-asked-questions">Frequently Asked Questions</a></p>
316316
</td>
317317
</tr>
318318
</table>

0 commit comments

Comments
 (0)