-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
67 lines (43 loc) · 1.69 KB
/
models.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
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_utils import PasswordType, ChoiceType
db = SQLAlchemy()
def connect_db(app):
db.app = app
db.init_app(app)
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(PasswordType(
schemes=['pbkdf2_sha512']
), nullable=False)
email = db.Column(db.String(50), nullable=False)
first_name = db.Column(db.String(30), nullable=False)
last_name = db.Column(db.String(30), nullable=False)
@classmethod
def register(cls, username, pwd, email, first_name, last_name):
'''Create a new user and return the created user'''
return cls(username=username, password=pwd, email=email, first_name=first_name, last_name=last_name)
@classmethod
def authenticate(cls, username, pwd):
'''Authenticate a user when they attempt to login'''
user = User.query.filter(User.username==username).one()
if user and user.password == pwd:
# return the user
return user
else:
return False
class Search(db.Model):
__tablename__ = 'searches'
Types = [
(u'-n', u'-n'),
(u'-v', u'-v'),
(u'-j', u'-j'),
(u'-a', u'-a')
]
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
word = db.Column(db.String(100), nullable=False)
pos = db.Column(ChoiceType(Types), nullable=False, default=u'-n')
note = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship('User', backref="searches")