-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_model.py
50 lines (38 loc) · 1.54 KB
/
no_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
import config
import bcrypt
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Integer, String, DateTime, Text
from sqlalchemy.orm import sessionmaker, scoped_session, relationship, backref
from flask.ext.login import UserMixin
engine = create_engine(config.DB_URI, echo=False)
session = scoped_session(sessionmaker(bind=engine,
autocommit = False,
autoflush = False))
Base = declarative_base()
Base.query = session.query_property()
class User(Base, UserMixin):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
email = Column(String(64), nullable=False)
password = Column(String(64), nullable=False)
salt = Column(String(64), nullable=False)
posts = relationship("Post", uselist=True)
def set_password(self, password):
self.salt = bcrypt.gensalt()
password = password.encode("utf-8")
self.password = bcrypt.hashpw(password, self.salt)
def authenticate(self, password):
password = password.encode("utf-8")
return bcrypt.hashpw(password, self.salt.encode("utf-8")) == self.password
# def create_tables():
# Base.metadata.create_all(engine)
# u = User(email="[email protected]")
# u.set_password("unicorn")
# session.add(u)
# p = Post(title="This is a test post", body="This is the body of a test post.")
# u.posts.append(p)
# session.commit()
# if __name__ == "__main__":
# create_tables()