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
| class User(db.Model,UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) password_hash = db.Column(db.String(128))
def set_password(self, password): self.password_hash = generate_password_hash(password)
def validate_password(self, password): return check_password_hash(self.password_hash, password)
class Movie(db.Model): id=db.Column(db.Integer,primary_key=True) title=db.Column(db.String(60)) year = db.Column(db.String(4)) class Subscribe(db.Model): id=db.Column(db.Integer,primary_key=True) uid=db.Column(db.Integer) mid =db.Column(db.Integer)
@app.cli.command() @click.option('--drop',is_flag=True) def initdb(drop): if drop: db.drop_all() db.create_all() click.echo('initiate successfully!')
@app.cli.command() def forge(): """Generate fake data.""" db.create_all()
users=[{'username':'Grey Li','password_hash':generate_password_hash('dog')}, {'username':'Derly Qi','password_hash':generate_password_hash('cat')} ] movies = [ {'title': 'My Neighbor Totoro', 'year': '1988'}, {'title': 'Dead Poets Society', 'year': '1989'}, {'title': 'A Perfect World', 'year': '1993'}, {'title': 'Leon', 'year': '1994'}, {'title': 'Mahjong', 'year': '1996'}, {'title': 'Swallowtail Butterfly', 'year': '1996'}, {'title': 'King of Comedy', 'year': '1999'}, {'title': 'Devils on the Doorstep', 'year': '1999'}, {'title': 'WALL-E', 'year': '2008'}, {'title': 'The Pork of Music', 'year': '2012'}, ] subscribe=[ {'uid':1,'mid':1}, {'uid':1,'mid':2}, {'uid':1,'mid':3}, {'uid':2,'mid':3}, {'uid':2,'mid':4}, {'uid':2,'mid':5}, {'uid':3,'mid':5}, {'uid':3,'mid':6}, {'uid':3,'mid':7}, {'uid':4,'mid':7}, {'uid':4,'mid':8}, {'uid':4,'mid':9}, {'uid':4,'mid':10}, ] for u in users: user=User(username=u['username'],password_hash=u['password_hash']) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) for s in subscribe: subscribe=Subscribe(uid=s['uid'],mid=s['mid']) db.session.add(subscribe) db.session.commit() click.echo('Done.')
|