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

use sqlite as database #126

Merged
merged 17 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ target/

.idea/
venv/

.vscode/
# Folder

.pytest_cache
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bottle = "*"
pip = "==18.0"
pre-commit = "*"
black = "*"
peewee = "*"

[dev-packages]
requests = "*"
Expand Down
64 changes: 39 additions & 25 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added data.db
Binary file not shown.
55 changes: 38 additions & 17 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import bottle
import os, sys, datetime
import string, random

from collections import defaultdict, namedtuple
import shelve
from peewee import *

path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
app = bottle.Bottle()

database_path = "submission_record.db"
user_db = "user_record.db"
sessions_db = "session_record.db"

DATABASE_NAME = "data.db"

questions = {}
contests = {}
question_dir = "files/questions"

Question = namedtuple("Question", "output statement")
rishabhKalakoti marked this conversation as resolved.
Show resolved Hide resolved
Submission = namedtuple("Submission", "question time output is_correct contest")
Contest = namedtuple("Contest", "description questions start_time end_time")
User = namedtuple("User", "password")

db = SqliteDatabase(DATABASE_NAME)


class User(Model):
username = CharField(unique=True)
password = CharField()

class Meta:
database = db


db.connect()
db.create_tables([User])

# dummy contests
contests["PRACTICE"] = Contest(
Expand Down Expand Up @@ -62,8 +77,10 @@ def login_redirect(*args, **kwargs):
if not logggedIn():
return bottle.template("home.html", message="Login required.")
return function(*args, **kwargs)

return login_redirect


@app.route("/")
def changePath():
return bottle.redirect("/home")
Expand Down Expand Up @@ -167,6 +184,7 @@ def rankings():
order = [(user, score, rank) for rank, (user, score) in enumerate(order, start=1)]
return template("rankings.html", people=order)


def logggedIn():
if not bottle.request.get_cookie("s_id"):
return False
Expand All @@ -192,25 +210,27 @@ def createSession(username):
def login():
username = bottle.request.forms.get("username")
password = bottle.request.forms.get("password")
with shelve.open(user_db) as users:
if not username in users:
return bottle.template("home.html", message="User does not exist.")
if users[username].password != password:
return bottle.template("home.html", message="Incorrect password.")
if (
rishabhKalakoti marked this conversation as resolved.
Show resolved Hide resolved
len(
User.select().where(
(User.username == username) & (User.password == password)
)
)
== 0
):
return bottle.template("home.html", message="Invalid credentials.")
return createSession(username)


@app.post("/register")
def register():
username = bottle.request.forms.get("username")
password = bottle.request.forms.get("password")
with shelve.open(user_db) as users:
if username in users:
return bottle.template(
"home.html",
message="Username already exists. Select a different username",
)
users[username] = User(password=password)
if len(User.select().where(User.username == username)) > 0:
rishabhKalakoti marked this conversation as resolved.
Show resolved Hide resolved
return bottle.template(
"home.html", message="Username already exists. Select a different username"
)
User.create(username=username, password=password)
return createSession(username)


Expand Down Expand Up @@ -257,6 +277,7 @@ def file_upload(code, number):

@app.error(404)
def error404(error):
return template("error.html" ,errorcode=error.status_code , errorbody = error.body)
return template("error.html", errorcode=error.status_code, errorbody=error.body)


bottle.run(app, host="localhost", port=8080)
bottle.run(app, host="localhost", port=8080)