-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
106 lines (78 loc) · 2.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import datetime
import logging
import os
import base64
from io import BytesIO
import sqlite3
import json
import cherrypy
from mako.lookup import TemplateLookup
import users
DB_STRING = os.path.join(os.path.dirname(__file__), 'data/database.sqlite3')
class Cookie(object):
#Abstracts cookies so they can be in sessions
def __init__(self, name):
self.name = name
def get(self, default=''):
#Get the value of the cookie or set if doesn't exist
if self.name in cherrypy.session:
return cherrypy.session[self.name]
else:
self.set(default)
return default
def set(self, value):
#Set the value of the cookie
cherrypy.session[self.name] = value
class Scouting(object):
def __init__(self):
self.lookup = TemplateLookup(directories = ['HtmlTemplates'], default_filters=['h'])
def dbConnect(self):
return sqlite3.connect(DB_STRING, detect_types=sqlite3.PARSE_DECLTYPES)
def template(self, template_name, **kwargs):
kwargs['user'] = self.getUser()
return self.lookup.get_template(template_name).render(**kwargs)
def getUser(self):
username = Cookie('username').get()
team = Cookie('team').get()
print(username, team)
if not username or not team:
return None
return users.User(username, team)
def show_loginpage(self, error=''):
"""Clear session and show login page"""
cherrypy.session.regenerate()
return self.template("login.mako", error=error)
def show_mainpage(self, user, error=''):
return self.template('index.mako', user=user)
def createAccount(self, username, team, password):
error = users.Users().createUser(username, team, password)
if(not error):
self.login(username, password)
else:
self.showSignup(error=error)
@cherrypy.expose
def login(self, username, password):
user = users.Users().getUser(username, password)
if user:
Cookie('username').set(user.username)
Cookie('team').set(user.team.teamNum)
return self.show_mainpage(user)
return self.show_loginpage('Not a valid username/password pair')
@cherrypy.expose
def logout(self):
return self.show_loginpage()
@cherrypy.expose
def index(self):
"""Shows main page or forces login if not logged in"""
user = self.getUser()
if not user:
return self.show_loginpage('')
return self.show_mainpage(user)
@cherrypy.expose
def showSignup(self, error=''):
return self.template('signup.mako', error=error)
@cherrypy.expose
def signup(self, username, team, password):
self.createAccount(username, team, password)
if __name__ == "__main__":
cherrypy.quickstart(Scouting(), config='development.conf')