Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question interface #134

Merged
merged 14 commits into from
Jun 3, 2019
Merged
103 changes: 83 additions & 20 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os, sys, datetime
import string, random
from peewee import *
import logging

path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
Expand Down Expand Up @@ -43,8 +44,11 @@ class Meta:


class Question(Model):
q_no = IntegerField(unique=True)
test_case_input = TextField()
test_case_output = TextField()
question_statement = CharField()
author = ForeignKeyField(User)
created_date_time = DateTimeField()
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved

class Meta:
database = db
Expand All @@ -71,7 +75,9 @@ class Meta:


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

# dummy contest data
practiceContest = Contest.get_or_create(
Expand Down Expand Up @@ -101,13 +107,13 @@ class Meta:

test = User.get_or_create(username="test", password="test")

q1 = Question.get_or_create(q_no=1, author=test[0])
q2 = Question.get_or_create(q_no=2, author=test[0])
q3 = Question.get_or_create(q_no=3, author=test[0])
q4 = Question.get_or_create(q_no=4, author=test[0])
q5 = Question.get_or_create(q_no=5, author=test[0])
q6 = Question.get_or_create(q_no=6, author=test[0])

q1 = Question.get_or_create(test_case_input="1", test_case_output="1", question_statement="1", author=test[0], created_date_time=datetime.datetime.now())
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
q2 = Question.get_or_create(test_case_input="2", test_case_output="2", question_statement="1", author=test[0], created_date_time=datetime.datetime.now())
q3 = Question.get_or_create(test_case_input="3", test_case_output="3", question_statement="1", author=test[0], created_date_time=datetime.datetime.now())
q4 = Question.get_or_create(test_case_input="4", test_case_output="4", question_statement="1", author=test[0], created_date_time=datetime.datetime.now())
q5 = Question.get_or_create(test_case_input="5", test_case_output="5", question_statement="1", author=test[0], created_date_time=datetime.datetime.now())
q6 = Question.get_or_create(test_case_input="6", test_case_output="6", question_statement="1", author=test[0], created_date_time=datetime.datetime.now())
logging.getLogger().setLevel(logging.INFO)
ContestProblems.get_or_create(contest=practiceContest[0], question=q1[0])
ContestProblems.get_or_create(contest=practiceContest[0], question=q2[0])
ContestProblems.get_or_create(contest=pastContest[0], question=q1[0])
Expand Down Expand Up @@ -175,22 +181,65 @@ def statistics():
)


@app.get("/addQuestion")
@login_required
def addQuestion():
return bottle.template("addQuestion.html")


@app.post("/questionInput")
@login_required
def questionInput():
userid = Session.get(Session.token == bottle.request.get_cookie("s_id")).user
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
time = datetime.datetime.now()
uploaded_question = bottle.request.files.get("question").file.read()
uploaded_answer = bottle.request.files.get("answer").file.read()
uploaded_statement = bottle.request.forms.get("statement")
try:
Question.create(
test_case_input=uploaded_question,
test_case_output=uploaded_answer,
question_statement=uploaded_statement,
author=userid,
created_date_time=time,
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
)
except:
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
bottle.abort(500, "Error in inserting submission to database.")
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
question_bank = (
Question.select(
Question.id,
Question.test_case_input,
Question.question_statement,
User.username,
Question.created_date_time,
)
.join(User, on=(Question.author == User.id))
.order_by(Question.created_date_time.desc())
.dicts()
)
return bottle.template("questionBank.html", question_bank=question_bank)


@app.get("/contest/<code>/<number>")
@login_required
def question(code, number):
if (
not ContestProblems.select()
.where((Contest.code == code) & (Question.q_no == int(number)))
.where((Contest.code == code) & (Question.id == int(number)))
.join(Contest, on=(ContestProblems.contest == Contest.id))
.join(Question, on=(ContestProblems.question == Question.q_no))
.join(Question, on=(ContestProblems.question == Question.id))
.exists()
):
return bottle.abort(404, "no such contest problem")
contest = Contest.get(Contest.code == code)
if contest.start_time > datetime.datetime.now():
return "The contest had not started yet."
with open(os.path.join(question_dir, number, "statement.txt"), "rb") as fl:
statement = fl.read()
statement = (
Question.select(Question.question_statement)
.where(Question.id == number)
.dicts()
.get()
)
return bottle.template(
"question.html", question_number=number, contest=code, question=statement
)
Expand All @@ -205,12 +254,22 @@ def contest(code):
return bottle.abort(404, "no such contest")
if contest.start_time > datetime.datetime.now():
return "The contest had not started yet."
logging.info(contest.questions.get())
return bottle.template("contest.html", contest=contest, questions=contest.questions)


@app.get("/question/<path:path>")
def download(path):
return bottle.static_file(path, root=question_dir)
@app.get("/question/<id>")
def download(id):
try:
question_result = (
Question.select(Question.test_case_input)
.where(Question.id == id)
.dicts()
.get()
)
return question_result["test_case_input"]
except:
bottle.abort(404, "No such question")


@app.get("/static/<filepath:path>")
Expand Down Expand Up @@ -323,16 +382,20 @@ def file_upload(code, number):
try:
contestProblem = ContestProblems.get(
ContestProblems.contest == Contest.get(Contest.code == code),
ContestProblems.question == Question.get(Question.q_no == int(number)),
ContestProblems.question == Question.get(Question.id == int(number)),
)
except:
return bottle.abort(404, "no such contest problem")
user = Session.get(Session.token == bottle.request.get_cookie("s_id")).user
time = datetime.datetime.now()
uploaded = bottle.request.files.get("upload").file.read()
with open(os.path.join(question_dir, number, "output.txt"), "rb") as fl:
expected = fl.read()
expected = expected.strip()
expected = (
Question.select(Question.test_case_output)
.where(Question.id == number)
.dicts()
.get()
)
expected = expected["test_case_output"]
uploaded = uploaded.strip()
ans = uploaded == expected
try:
Expand Down
20 changes: 20 additions & 0 deletions views/addQuestion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% include('base.html', title="Add Question")
<body>
<header class="text-center">
<h1>Question Upload Page</h1>
</header>
<div class="container">
</br>
</br>
<form action="/questionInput" method = "post" enctype = "multipart/form-data">Question Upload : <br/>
<input type="file" name="question" required /><br/><br/>
Answer Upload : <br/>
<input type="file" name="answer" required /><br/><br/>
Statement : <br/>
<textarea name="statement" rows = "3" cols = "80" required>Enter Statement</textarea>
<br/>
<br/>
<button class="btn btn-primary" type="submit">Upload</button>
</form>
</div>
</body>
1 change: 1 addition & 0 deletions views/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h1>Contests</h1>
</table>
</div>
<div class="container">
<a href="/addQuestion" class="btn btn-primary">Add Question</a>
<a href="/stats" class="btn btn-primary">Statistics</a>
<a href="/logout" class="btn btn-primary">Logout</a>
</div>
Expand Down
4 changes: 2 additions & 2 deletions views/question.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<h1>Submission Page</h1>
</header>
<div class="container">
<p>{{question}}</p>
<a href="/question/{{question_number}}/inputs.txt" role="button" class="btn btn-primary">
<p>{{question["question_statement"]}}</p>
<a href="/question/{{question_number}}" role="button" class="btn btn-primary">
Download Test Case
</a>
</div>
Expand Down
38 changes: 38 additions & 0 deletions views/questionBank.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
% include('base.html', title="Question Bank")
<body>
<div class="container text-center">
<div class="container" style="height:3rem;">
<a href="/dashboard" class="btn btn-primary">Dashboard</a>
<a href="/logout" class="btn btn-primary">Logout</a>
</div>
<table class="table">
<thead>
<tr>
<th>List Of Questions</th>
</tr>
</thead>
</table>
<table class="table">
<tbody style="height:20rem;display:inline-block;overflow-y:scroll">
<tr style="font-weight:bold;">
<td style="padding-right:10rem">Question</td>
<td style="padding-right:6rem"> Question Statement</td>
<td style="padding-right:10rem">Author</td>
<td style="padding-right:8rem">Time submitted</td>
</tr>
% for question in question_bank :
<tr>
<td style="padding-right:10rem">
<a href="/question/{{question['id']}}" role="button" class="btn btn-primary">
Download Question
</a>
</td>
<td style="padding-right:10rem">{{question["question_statement"]}}</td>
<td style="padding-right:10rem">{{question["username"]}}</td>
<td style="padding-right:8rem">{{question["created_date_time"].strftime("%d-%m-%Y %H:%M")}}</td>
</tr>
% end
</tbody>
</table>
</div>
</body>