/
model.py
145 lines (99 loc) · 4.95 KB
/
model.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
##############################################################################
#Externals
from flask import Flask, Response
from flask.ext.login import LoginManager, UserMixin, login_required, login_user, logout_user, confirm_login
from flask_sqlalchemy import SQLAlchemy
import bcrypt
# This is the connection to the postgreSQL database; we get it from the
# Flask-SQLAlchemy helper library. On this, we can find the `session`
# object, where we do most of our interactions (like committing, etc.)
db = SQLAlchemy()
##############################################################################
# Model definitions
class User(db.Model, UserMixin):
"""Stores user id and email information."""
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String, nullable=False)
password_hash = db.Column(db.String, nullable=False)
phone_number = db.Column(db.String(12), nullable=True)
#Inheriting from UserMixin gives you defaults of is_authenticated(), is_active(), is_anonymous(), and get_id() functions required by Flask-Login
def __init__(self, email, password):
print "I am initting"
self.email = email
self.set_password(password)
self.quizzes_taken = 0
def __repr__(self):
return "<User email=%s>" % (self.email)
def set_password(self, password):
self.password_hash = bcrypt.hashpw(password, bcrypt.gensalt()) #salts and hashes a password. Salt is randomly generated by bcrypt
print "The password set on this user is: "
print self.password_hash
def check_password(self, password):
encoded_pw = self.password_hash.encode('utf-8')
return bcrypt.hashpw(password, encoded_pw) == encoded_pw
def add_quiz_taken(self):
"""TODO"""
self.quizzes_taken += 1
def update_average(self, new_score):
"""Updates the user's overall average score."""
new_avg = (self.avg_score * (self.quizzes_taken - 1) + new_score)/(self.quizzes_taken)
self.avg_score = new_avg
class Continent(db.Model):
"""Stores continent id and name, manually seeded from seed.py"""
__tablename__ = "continents"
name = db.Column(db.String, primary_key=True, nullable=False)
def __repr__(self):
return "<Continent name=%s>" % (self.name)
class Country(db.Model):
"""Stores countries and their attributes, seeded from Wikipedia in seed.py"""
__tablename__ = "countries"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
country_name = db.Column(db.String, nullable=False, unique=True)
alpha_code = db.Column(db.String(3), nullable=False)
demonym = db.Column(db.String, nullable=True)
continent_name = db.Column(db.String, db.ForeignKey('continents.name'))
languages = db.Column(db.String, nullable=True)
capital = db.Column(db.String, nullable=True)
continent = db.relationship('Continent', backref='countries')
def __repr__(self):
return "<Country country_name=%s>" % (self.country_name)
class Capquiz(db.Model):
"""Stores the current caps quiz for each user. Only one per user allowed."""
__tablename__ = "capquizzes"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), unique=True, nullable=False)
country_id = db.Column(db.Integer, db.ForeignKey('countries.id'))
A = db.Column(db.String, nullable=False)
B = db.Column(db.String, nullable=False)
C = db.Column(db.String, nullable=False)
D = db.Column(db.String, nullable=False)
def __repr__(self):
return "<capQuiz user_id=%s>" % (self.user_id)
class Quizevent(db.Model):
"""Stores individual quiz event information."""
__tablename__ = "quizevents"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
country_id = db.Column(db.Integer, db.ForeignKey('countries.id'))
continent_name = db.Column(db.String, db.ForeignKey('continents.name'))
score = db.Column(db.Integer, nullable=True)
quiz_type = db.Column(db.String(4), nullable=True)
user = db.relationship('User', backref='quizzes')
country = db.relationship('Country', backref='quizzes')
continent = db.relationship("Continent", backref='quizzes')
##############################################################################
# Helper functions
def connect_to_db(app, db_uri=None):
"""Connect the database to our Flask app."""
# Configure to use posgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri or 'postgresql://localhost/Athena'
app.config['SQLALCHEMY_ECHO'] = True
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print "Connected to DB."
db.create_all()