-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
72 lines (57 loc) · 2.11 KB
/
manage.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
# -*- coding:utf-8 -*-
# user :chauncylee
# time :18-8-26 下午6:01
# filename:manager.py
# IDE :PyCharm
import random
from flask_script import Manager
from arthas import app, db
from arthas.models import User, Image, Comment
from sqlalchemy import or_, and_
manager = Manager(app)
def get_image_url():
return 'http://images.nowcoder.com/head/' + str(random.randint(0, 1000)) + 'm.png'
@manager.command
def init_database():
db.drop_all()
db.create_all()
# create
for i in range(0, 100):
db.session.add(User('User' + str(i + 1), 'a' + str(i), '.'.join(random.sample('01234567890abcdefgABCDEFG', 10))))
t = 7 if i == 99 else 3
for j in range(0, t):
db.session.add(Image(get_image_url(), i + 1))
for k in range(0, 3):
db.session.add(Comment('This is a comment' + str(k), 1 + 3 * i + j, i + 1))
db.session.commit()
# modify
for i in range(50, 100, 2):
user = User.query.get(i)
user.username = '[New1]' + user.username
User.query.filter_by(id=51).update({'username': 'New2'})
db.session.commit()
# delete
for i in range(50, 100, 2):
comment = Comment.query.get(i + 1)
db.session.delete(comment)
db.session.commit()
# query
print 1, User.query.all()
print 2, User.query.get(3)
print 3, User.query.filter_by(id=5).first()
print 4, User.query.order_by(User.id.desc()).offset(1).limit(2).all()
print 5, User.query.filter(User.username.endswith('0')).limit(3).all()
print 6, User.query.filter(or_(User.id == 88, User.id == 99)).all()
print 7, User.query.filter(and_(User.id > 88, User.id < 93)).all()
print 8, User.query.filter(and_(User.id > 88, User.id < 93)).first_or_404()
print 9, User.query.order_by(User.id.desc()).paginate(page=1, per_page=10).items
user = User.query.get(1)
print 10, user.images.all()
image = Image.query.get(1)
print 11, image.user
@manager.option('-n', '--name', dest='name', default='world')
def hello(name):
"""say hello world"""
print 'hello %s' % name
if __name__ == '__main__':
manager.run()