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

Commit a5e8e34

Browse files
authored
Merge branch 'master' into voting-disable-frontend
2 parents eadc595 + c96d966 commit a5e8e34

File tree

7 files changed

+400
-15
lines changed

7 files changed

+400
-15
lines changed

CodeChallenge/api/vote.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..auth import Users
1010
from ..mail import mail
1111
from ..mailgun import mg_validate
12-
from ..models import Answer, db, Vote, Question
12+
from ..models import Answer, db, Vote, Question, ranking
1313

1414
bp = Blueprint("voteapi", __name__, url_prefix="/api/v1/vote")
1515

@@ -49,12 +49,13 @@ def get_contestants():
4949
Users.studentfirstname,
5050
Users.studentlastname,
5151
Users.username,
52-
func.concat(Users.studentfirstname, func.right(Users.studentlastname, 1))
52+
func.concat(Users.studentfirstname, func.right(Users.studentlastname, 1)),
53+
Answer.disqualified
5354
) \
5455
.join(Answer.question) \
5556
.join(Answer.user) \
5657
.outerjoin(Answer.votes) \
57-
.filter(Question.rank == core.max_rank(), Answer.correct) \
58+
.filter(Question.rank == core.max_rank(), Answer.correct, Answer.disqualified.is_(None)) \
5859
.group_by(Answer.id)
5960

6061
if desc is not None:
@@ -186,6 +187,10 @@ def vote_confirm():
186187
return jsonify(status="error",
187188
reason="vote not found - try voting again, or contestant may have been disqualified.")
188189

190+
if v.confirmed:
191+
return jsonify(status="success",
192+
reason="vote already confirmed")
193+
189194
delete_votes = Vote.query \
190195
.filter(Vote.voter_email == v.voter_email,
191196
Vote.id != v.id) \
@@ -199,6 +204,18 @@ def vote_confirm():
199204

200205
db.session.commit()
201206

207+
msg = Message(subject="Vote confirmation successful!",
208+
recipients=[v.voter_email])
209+
210+
votes, rank = v.ranking()
211+
212+
msg.html = render_template("challenge_vote_submitted.html",
213+
username=v.answer.user.username,
214+
votes=int(votes),
215+
rank=rank)
216+
217+
mail.send(msg)
218+
202219
return jsonify(status="success",
203220
reason="vote confirmed")
204221

@@ -230,7 +247,7 @@ def search():
230247
.join(Answer.question) \
231248
.join(Answer.user) \
232249
.outerjoin(Answer.votes) \
233-
.filter(Question.rank == core.max_rank(), Answer.correct,
250+
.filter(Question.rank == core.max_rank(), Answer.correct, Answer.disqualified.is_(None),
234251
or_(Users.username.ilike(keyword), Users.studentfirstname.ilike(keyword),
235252
Users.studentlastname.ilike(keyword))) \
236253
.group_by(Answer.id)\

CodeChallenge/models.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from flask_sqlalchemy import SQLAlchemy
2+
from typing import Tuple
3+
24

35
db = SQLAlchemy()
46

@@ -11,6 +13,24 @@ def drop_all():
1113
db.drop_all()
1214

1315

16+
def ranking(answer_id: int) -> Tuple[int, int]:
17+
return db.session.execute("""
18+
select rainv.num_votes, rainv.rank
19+
from (
20+
select @rownum := @rownum + 1 as 'rank',
21+
prequery.answer_id,
22+
prequery.num_votes
23+
from (select @rownum := 0) sqlvars,
24+
(select answer_id,
25+
count(*) as num_votes
26+
from vote
27+
group by answer_id
28+
order by count(*) desc) prequery
29+
) as rainv
30+
where answer_id = :answer_id
31+
""", {'answer_id': answer_id}).first()
32+
33+
1434
class Question(db.Model):
1535
id = db.Column(db.Integer, primary_key=True)
1636
title = db.Column(db.String(5000), nullable=False)
@@ -39,7 +59,7 @@ class Answer(db.Model):
3959
user = db.relationship("Users", lazy=True, uselist=False)
4060
votes = db.relationship("Vote", cascade="all,delete",
4161
lazy=True, uselist=True)
42-
disqualified = db.Column(db.String)
62+
disqualified = db.Column(db.String(255))
4363

4464
def confirmed_votes(self) -> int:
4565
confirmed = 0
@@ -55,6 +75,7 @@ class Vote(db.Model):
5575
answer_id = db.Column(db.Integer,
5676
db.ForeignKey("answer.id", ondelete="cascade"),
5777
nullable=False)
78+
answer = db.relationship("Answer", lazy=True, uselist=False)
5879
voter_email = db.Column(db.String(255), nullable=False)
5980
confirmed = db.Column(db.Boolean, nullable=False, default=False)
6081

@@ -63,3 +84,6 @@ def existing_vote(email: str) -> bool:
6384
v = Vote.query.filter_by(voter_email=email).first()
6485
return v
6586

87+
def ranking(self):
88+
return ranking(self.answer.id)
89+

0 commit comments

Comments
 (0)