Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class Meta:
database = db


class QuestionStatus(Model):
question = ForeignKeyField(Question)
user = TextField()
status = TextField()

class Meta:
database = db
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add an index for the database here.



class ContestProblems(Model):
contest = ForeignKeyField(Contest, backref="questions")
question = ForeignKeyField(Question)
Expand All @@ -76,7 +85,7 @@ class Meta:


db.connect()
db.create_tables([User, Session, Submission, ContestProblems, Contest, Question])
db.create_tables([User, Session, Submission, ContestProblems, Contest, Question, QuestionStatus])


def login_required(function):
Expand Down Expand Up @@ -236,9 +245,16 @@ def question(code, number):
.dicts()
.get()
)
return bottle.template(
"question.html", question_number=number, contest=code, question=statement
)
user = Session.get(Session.token == bottle.request.get_cookie("s_id")).user
question_status = QuestionStatus.select().where((QuestionStatus.question == number) & (QuestionStatus.user == user))
if question_status.count() == 0:
return bottle.template(
"question.html", question_number=number, contest=code, question=statement, question_status='Not Submitted'
)
else:
return bottle.template(
"question.html", question_number=number, contest=code, question=statement, question_status='Submitted'
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to directly return the question_status extracted from the query. Also, right now the status shows Submitted/ Not Submitted, it would be better to instead use Accepted/ Wrong Answer/ Unattempted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also avoid the if-else block by using something like:

question_status = QuestionStatus.select(QuestionStatus.status).where(
        (QuestionStatus.question == number) & (QuestionStatus.user == user)
    ).dicts().get()
return bottle.template(
    "question.html", question_number=number, contest=code, question=statement,
    question_status= ((question_status[status]==Null)?"Unattempted":question_status[status])
)


@app.get("/contest/<code>")
Expand Down Expand Up @@ -399,6 +415,12 @@ def file_upload(code, number):
)
except Exception as e:
bottle.abort(500, str(e))
try:
QuestionStatus.get_or_create(
question=number, user=user, status='Attempted'
)
except Exception as e:
bottle.abort(500, str(e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to merge the two try catch blocks into one.

if not ans:
return "Wrong Answer!!"
else:
Expand Down
1 change: 1 addition & 0 deletions views/question.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ <h1>Submission Page</h1>
</header>
<div class="container">
<p>{{question["question_statement"]}}</p>
<p><b>Status :</b> {{question_status}}<p>
<a href="/question/{{question_number}}" role="button" class="btn btn-primary">
Download Test Case
</a>
Expand Down