-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb_models.py
More file actions
57 lines (43 loc) · 1.9 KB
/
db_models.py
File metadata and controls
57 lines (43 loc) · 1.9 KB
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
# coding:utf-8
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import sql
app = Flask(__name__)
app.secret_key = 'secret database key'
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db_files/db_models.db'
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(20), nullable=False)
email = db.Column(db.String(20))
timestamp = db.Column(db.DateTime(timezone=True), default=sql.func.now())
def __repr__(self):
return u"< {0} , {1} >".format(self.name, self.email)
class Problem(db.Model):
__tablename__ = 'problems'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), unique=True, nullable=False)
detail = db.Column(db.String(500))
creator_id = db.Column(db.Integer, db.ForeignKey('users.id'))
creator = db.relationship('User', backref='problems')
def __repr__(self):
return u"< problem_id:{0},{1},cid: {2} >".format(self.id, self.title, self.creator_id)
class Solution(db.Model):
__tablename__ = 'solutions'
id = db.Column(db.Integer, primary_key=True)
detail = db.Column(db.String(1000), nullable=False)
score = db.Column(db.Integer, default=0)
candidate_id = db.Column(db.Integer, db.ForeignKey('users.id'))
candidate = db.relationship('User', backref='solutions')
problem_id = db.Column(db.Integer, db.ForeignKey('problems.id'))
problem = db.relationship('Problem', backref='solutions')
def __repr__(self):
return u"< solution_id:{0},{1},cid:{2},pid:{3} >".format(self.id, self.detail, self.candidate_id,
self.problem_id)
if __name__ == '__main__':
db.drop_all()
db.create_all()
print('rebuild database')