diff --git a/CodeChallenge/api/vote.py b/CodeChallenge/api/vote.py index 259a218..f9629a9 100644 --- a/CodeChallenge/api/vote.py +++ b/CodeChallenge/api/vote.py @@ -48,10 +48,21 @@ def get_contestants(): contestants = [] for ans in p.items: # type: Answer + + display = None + if ans.user.studentfirstname \ + and ans.user.studentlastname: + display = f"{ans.user.studentfirstname} " \ + f"{ans.user.studentlastname[0]}." + contestants.append(dict( id=ans.id, text=ans.text, numVotes=len(ans.votes), + firstName=ans.user.studentfirstname, + lastName=ans.user.studentlastname, + username=ans.user.username, + display=display )) return jsonify( diff --git a/CodeChallenge/models.py b/CodeChallenge/models.py index 9534cef..395ec01 100644 --- a/CodeChallenge/models.py +++ b/CodeChallenge/models.py @@ -36,6 +36,7 @@ class Answer(db.Model): text = db.Column(db.String(2000)) correct = db.Column(db.Boolean) question = db.relationship("Question", lazy=True, uselist=False) + user = db.relationship("Users", lazy=True, uselist=False) votes = db.relationship("Vote", cascade="all,delete", lazy=True, uselist=True) diff --git a/tests/test_question.py b/tests/test_question.py index 3aa2b20..1e06a3f 100644 --- a/tests/test_question.py +++ b/tests/test_question.py @@ -90,7 +90,7 @@ def register(client, email, username, password, firstname, lastname, studentemai return client.post("/api/v1/users/register", json=dict( username=username, parentEmail=email, password=password, parentFirstName=firstname, parentLastName=lastname, DOB="1994-04-13", - studentEmail=studentemail + studentEmail=studentemail, studentFirstName="Sam", studentLastName="Hoffman" ), follow_redirects=True) @@ -364,6 +364,11 @@ def test_vote_ballot(client_challenge_lastq): assert "id" in items[0] assert "numVotes" in items[0] assert "text" in items[0] + assert items[0]["firstName"] == "Sam" + assert items[0]["lastName"] == "Hoffman" + assert items[0]["username"] == "cwhqsam" + assert items[0]["display"] == "Sam H." + VALID_ANSWER = items[0]["id"]