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

Commit bf793e2

Browse files
authored
Merge pull request #28 from codewizardshq/history
questions API:
2 parents 2a0d6ad + 69662ca commit bf793e2

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

CodeChallenge/api/questions.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def answer_next_question():
8989

9090
data = request.get_json()
9191
text = data["text"]
92-
correct = str_cmp(text, q.answer)
92+
correct = str_cmp(text.lower(), q.answer.lower())
9393

9494
ans = Answer.query.filter_by(user_id=user.id, question_id=q.id).first()
9595

@@ -108,3 +108,46 @@ def answer_next_question():
108108
db.session.commit()
109109

110110
return jsonify({"status": "success", "correct": correct})
111+
112+
113+
@bp.route("/history", methods=["GET"])
114+
@jwt_required
115+
def history():
116+
""" Returns all past questions and answers for the currrent user"""
117+
118+
u = get_current_user()
119+
120+
qna = []
121+
for ans in Answer.query.filter_by(user_id=u.id): # type: Answer
122+
123+
qna.append(dict(
124+
question=dict(
125+
id=ans.question_id,
126+
title=ans.question.title,
127+
rank=ans.question.rank
128+
),
129+
answered=ans.text,
130+
correct=ans.correct
131+
))
132+
133+
return jsonify(qna)
134+
135+
136+
@bp.route("/reset", methods=["DELETE"])
137+
@jwt_required
138+
def reset_all():
139+
""" Reset rank for the current user """
140+
if current_app.config["ALLOW_RESET"]:
141+
142+
u = get_current_user()
143+
u.rank = 0
144+
145+
for ans in Answer.query.filter_by(user_id= u.id): # type: Answer
146+
db.session.delete(ans)
147+
148+
db.session.commit()
149+
150+
return jsonify(status="success", message="all answers and rank reset")
151+
152+
return jsonify(status="error",
153+
message="resetting not allowed at this time"), 403

CodeChallenge/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class DefaultConfig:
2828
MAIL_DEFAULT_SENDER = "CodeWizardsHQ <no-reply@codewizardshq.com>"
2929
MAIL_SUPPRESS_SEND = True
3030

31+
ALLOW_RESET = False
32+
3133
@property
3234
def ROOT_DIR(self):
3335
return os.path.dirname(self.APP_DIR)
@@ -48,6 +50,7 @@ class ProductionConfig(DefaultConfig):
4850
MAIL_USERNAME = os.getenv("MAIL_USERNAME")
4951
MAIL_PASSWORD = os.getenv("MAIL_PASSWORD")
5052
JWT_ACCESS_TOKEN_EXPIRES = 604800
53+
ALLOW_RESET = os.getenv("ALLOW_RESET")
5154

5255

5356
class DevelopmentConfig(ProductionConfig):
@@ -59,6 +62,7 @@ class DevelopmentConfig(ProductionConfig):
5962
JWT_SECRET_KEY = "SuperSecret"
6063
SECRET_KEY = "flaskSecretKey"
6164
JWT_COOKIE_CSRF_PROTECT = False
65+
ALLOW_RESET = True
6266

6367
@property
6468
def DIST_DIR(self):

CodeChallenge/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ class Answer(db.Model):
3333
db.ForeignKey("users.id", ondelete="cascade"))
3434
text = db.Column(db.String(2000))
3535
correct = db.Column(db.Boolean)
36+
question = db.relationship("Question", lazy=True, uselist=False)

tests/test_question.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def client_challenge_today():
1414
app.config["TESTING"] = True
1515
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
1616
app.config["CODE_CHALLENGE_START"] = time.time()
17+
app.config["ALLOW_RESET"] = True
1718

1819
with app.test_client() as client:
1920
with app.app_context():
@@ -142,6 +143,12 @@ def test_get_rank1(client_challenge_past):
142143
assert retval.get_json()["question"] == "What is 2+2?"
143144
assert retval.get_json()["rank"] == 1
144145

146+
retval = client_challenge_past.get("/api/v1/users/hello")
147+
data = retval.get_json()
148+
149+
assert retval.status_code == 200
150+
assert data["rank"] == 0
151+
145152

146153
def test_answer_rank1_correctly(client_challenge_past):
147154
retval = client_challenge_past.post("/api/v1/questions/answer", json=dict(
@@ -151,6 +158,22 @@ def test_answer_rank1_correctly(client_challenge_past):
151158
assert retval.status_code == 200
152159
assert retval.get_json()["correct"] is True
153160

161+
# check history
162+
retval = client_challenge_past.get("/api/v1/questions/history")
163+
history = retval.get_json()
164+
165+
assert len(history) == 1
166+
assert "question" in history[0]
167+
assert history[0]["answered"] == "4"
168+
assert history[0]["correct"]
169+
170+
# check rank
171+
retval = client_challenge_past.get("/api/v1/users/hello")
172+
data = retval.get_json()
173+
174+
assert retval.status_code == 200
175+
assert data["rank"] == 1
176+
154177

155178
def test_get_rank2(client_challenge_past):
156179
retval = client_challenge_past.get("/api/v1/questions/next")
@@ -168,6 +191,12 @@ def test_answer_rank2_incorrectly(client_challenge_past):
168191
assert retval.status_code == 200
169192
assert retval.get_json()["correct"] is False
170193

194+
retval = client_challenge_past.get("/api/v1/users/hello")
195+
data = retval.get_json()
196+
197+
assert retval.status_code == 200
198+
assert data["rank"] == 1
199+
171200

172201
def test_answer_rank2_correctly(client_challenge_past):
173202
retval = client_challenge_past.post("/api/v1/questions/answer", json=dict(
@@ -177,6 +206,12 @@ def test_answer_rank2_correctly(client_challenge_past):
177206
assert retval.status_code == 200
178207
assert retval.get_json()["correct"] is True
179208

209+
retval = client_challenge_past.get("/api/v1/users/hello")
210+
data = retval.get_json()
211+
212+
assert retval.status_code == 200
213+
assert data["rank"] == 2
214+
180215

181216
def test_get_rank3_404(client_challenge_past):
182217
retval = client_challenge_past.get("/api/v1/questions/next")
@@ -208,3 +243,16 @@ def test_answer_exceed_attempts(client_challenge_past):
208243
else:
209244
assert retval.status_code == 404
210245
assert "X-RateLimit-Remaining" in retval.headers
246+
247+
248+
def test_reset_all(client_challenge_past):
249+
250+
retval = client_challenge_past.delete("/api/v1/questions/reset")
251+
252+
assert retval.status_code == 200
253+
254+
retval = client_challenge_past.get("/api/v1/users/hello")
255+
data = retval.get_json()
256+
257+
assert retval.status_code == 200
258+
assert data["rank"] == 0

0 commit comments

Comments
 (0)