Exemplo n.º 1
0
def reg():
    # args url里的
    # form body里的
    # values 全部
    username = request.values.get('username').strip()
    password = request.values.get('password').strip()
    # 校验
    user = User.query.filter_by(username=username).first()
    if username == '' or password == '':
        return redirect_with_msg('/regloginpage', u'用户名和密码不能为空', 'reglogin')
    if user != None:
        return redirect_with_msg('/regloginpage', u'用户名已存在', 'reglogin')

    salt = ''.join(
        random.sample(
            '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            10))
    m = hashlib.md5()
    m.update((password + salt).encode('utf8'))
    password = m.hexdigest()
    user = User(username, password, salt)
    db.session.add(user)
    db.session.commit()
    login_user(user)

    user = User.query.filter_by(username=username).first()
    if user is not None:
        return redirect('/profile/' + str(user.get_id()) + '/')
    return redirect('/')
Exemplo n.º 2
0
def reg():
    # request.args
    # request.form
    username = request.values.get('username').strip()
    password = request.values.get('password').strip()

    if username == '' or password == '':
        return redirect_with_msg('/regloginpage/', u'用户名或密码不能为空', 'reglogin')

    user = User.query.filter_by(username=username).first()
    if user != None:
        return redirect_with_msg('/regloginpage/', u'用户名已经存在', 'reglogin')

    # 更多判断

    salt = '.'.join(random.sample('01234567890abcdefghigABCDEFGHI', 10))
    m = hashlib.md5()
    m.update((password + salt).encode('utf8'))
    password = m.hexdigest()
    headurl = 'http://images.nowcoder.com/head/' + str(random.randint(
        0, 1000)) + 'm.png'
    user = User(username=username,
                password=password,
                salt=salt,
                headurl=headurl)
    db.session.add(user)
    db.session.commit()

    login_user(user)

    next = request.values.get('next')
    if next != None and next.startswith('/'):
        return redirect(next)

    return redirect('/')
Exemplo n.º 3
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'a' + str(i)))
        for j in range(0, 6):
            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), 6 * i + j + 1,
                            i + 1))
    db.session.commit()
    '''
    print 1,User.query.all()
    print 2,User.query.get(3)
    print 3,User.query.filter_by(id=5).all()
    print 4,User.query.filter(User.username.endswith('0')).limit(3).all()
    print 5,User.query.filter(and_(User.id>88,User.id<90)).all()
    print 6,User.query.paginate(page=1,per_page=11).items

    image = Image.query.get(1)
    print 8,image.user

    User.query.filter_by(id=2).update({'username':'******'})
    db.session.commit()

    for i in range(50,100,2):
        comment = Comment.query.get(i+1)
        db.session.delete(comment)
    db.session.commit()
    '''
    for i in range(0, 4):
        image = Image.query.get(600 - i)
        db.session.delete(image)
    db.session.commit()
Exemplo n.º 4
0
def reg():
    username = request.form['username'].strip()
    password = request.form['password'].strip()

    if username == "" or password == "":
        return redirect_massage('/reloginpage',
                                '用户名/密码不能为空',
                                category='relogin')

    user = User.query.filter_by(username=username).first()
    if user is not None:
        return redirect_massage('/reloginpage', '用户名已存在', category='relogin')

    salt = '.'.join(random.sample('0123456789abcdefgABCDEFG', k=3))
    m = hashlib.md5()
    m.update((password + salt).encode('utf8'))
    password = m.hexdigest()

    user = User(username, password, salt)
    db.session.add(user)
    db.session.commit()

    # 用户登陆记录
    login_user(user)

    return redirect('/')
Exemplo n.º 5
0
def init_database():

    for i in range(0, 10):  # 添加用户
        db.session.add(User('user' + str(i), 'a' + str(i)))
        for j in range(0, 3):
            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,
                            j + 1))
    db.session.commit()

    for i in range(5, 10):
        user = User.query.get(i)
        user.username = '******' + user.username
    db.session.commit()

    print(1, User.query.all())
    print(2, User.query.get(4))

    image = Image.query.get(26)
    image.url = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1268348955,3754655044&fm=26&gp=0.jpg'
    db.session.commit()

    for i in range(0, 10):
        image = Image(get_image_url(), 10)
        db.session.add(image)
        db.session.commit()

    xx_url = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1268348955,3754655044&fm=26&gp=0.jpg'
    xx_image = Image(xx_url, 11)
    db.session.add(xx_image)
    db.session.commit()
Exemplo n.º 6
0
def reg():
    # request.args 参数
    # request.form 提交过来的
    # strip去前后空格
    username = request.values.get('username').strip()
    password = request.values.get('password').strip()

    # 如果发现用户名已经被注册,就返回到注册页上面
    user = User.query.filter_by(username=username).first()

    if username == '' or password == '':
        return redirect_with_msg('/regloginpage/', u'用户名或密码不能为空', 'reglogin')
    if user != None:
        return redirect_with_msg('/regloginpage/', u'用户名已存在', 'reglogin')

    # 更多判断
    # 对新注册用户密码做加盐的MD5加密
    salt = '.'.join(random.sample('01234567890abcdefghigABCDEFGHI',
                                  10))  # 加盐加密
    m = hashlib.md5()
    m.update((password +
              salt).encode("utf8"))  # 在python3.5中在哈希之前必须要重新进行编码,用utf8要不然会报错
    password = m.hexdigest()  # 生成16进制的新密码
    user = User(username, password, salt)  # 把新注册的用户写入数据库
    db.session.add(user)
    db.session.commit()

    login_user(user)  # 自动登录,登录状态

    next = request.values.get('next')
    if next != None and next.startswith('/'):
        return redirect(next)

    return redirect('/')  # 返回首页
Exemplo n.º 7
0
def reg():
    # request.args
    # request.form
    username = request.values.get('username').strip()
    password = request.values.get('password').strip()

    if username == '' or password == '':
        return redirect_with_msg('/regloginpage/', u'用户名或密码不能为空', 'reglogin')

    user = User.query.filter_by(username=username).first()
    if user != None:
        return redirect_with_msg('/regloginpage/', u'用户名已经存在', 'reglogin')

    # 更多判断

    salt = '.'.join(random.sample('0123456789abcdefghigABCDEFGHI', 10))
    m = hashlib.md5()
    m.update(password + salt)
    password = m.hexdigest()
    user = User(username, password, salt)
    db.session.add(user)
    db.session.commit()

    login_user(user)

    next = request.values.get('next')
    if next != None and next.startswith('/'):
        return redirect(next)

    return redirect('/')
Exemplo n.º 8
0
def reg():
    username = request.values.get('username').strip()
    print "username:"******"password", password

    if username == '' or password == '':
        print 22222
        return redirect_with_msg('/regloginpage/', u'用户名或密码不能为空', 'reglogin')
    if User.query.filter_by(username=username).first():
        print 1111
        return redirect_with_msg('/regloginpage/', u'用户名已存在', 'reglogin')

    salt = ''.join(random.sample('haiugeughcnuagwhqi148916511', 5))
    m = hashlib.md5()
    m.update(password + salt)
    password = m.hexdigest()
    print 33333

    user = User(username, password, salt)
    db.session.add(user)
    db.session.commit()
    print 44444

    login_user(user)

    return redirect('/')
Exemplo n.º 9
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('newke' + str(i), 'a' + str(i)))
        for j in range(0, 3):  #每人发三张图
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(
                    Comment('comment' + str(k), 1 + 3 * i + j, i + 1))
    db.session.commit()

    for i in range(60, 100, 10):
        # 通过update函数
        User.query.filter_by(id=i + 1).update({'username': '******' + str(i)})
    db.session.commit()
    for m in range(5, 50, 2):
        # 通过设置属性
        newname = User.query.get(m)
        newname.username = '******' + newname.username
    db.session.commit()
    User.query.filter_by(id=11).update({'username': '******'})
    db.session.commit()

    for i in range(51, 100, 2):
        comment = Comment.query.get(i + 1)
        db.session.delete(comment)
    db.session.commit()
Exemplo n.º 10
0
def init_database():
    db.drop_all()  # 删除所有的表
    db.create_all()  # 根据user中的代码创建表(根据列)
    for i in range(0,100):
        # 创建用户(用户名,密码)
        db.session.add(User('User'+str(i),'a'+str(i)))
        # 给每个人加几张图片,db.session.add(class)——创建类
        for j in range(0,10):
            db.session.add(Image(get_image_url(),i+1))
            for k in range(0,3):
                db.session.add(Comment('This is comment '+str(k),1+10*i+j,i+1))

    db.session.commit()  #数据库事物的提交,没提交没用!

    #更新数据
    #直接取.username更新;update+字典形式更新
    for i in range(50,100,2):
        user = User.query.get(i)
        user.username = '******'+user.username
    for i in range(51,100,2):
        User.query.filter(User.id==i).update({'username':'******'+str(i)})
    db.session.commit()


    #删除评论
    for i in range(50,100,2):
        comment = Comment.query.get(i)
        db.session.delete(comment)
    db.session.commit()
Exemplo n.º 11
0
def init_database_king():
    urls = get_upload_url()
    print(urls)
    db.drop_all()
    db.create_all()
    with open('.\herolist.json', 'r', encoding='utf-8') as ff:
        jsonFile = json.load(ff)
    #print(jsonFile)
    n = 0
    for i in range(0, len(jsonFile)):
        #英雄编号
        eName = jsonFile[i]['ename']
        # 英雄名字
        cName = jsonFile[i]['cname']
        title = jsonFile[i]['title']
        skinName = jsonFile[i]['skin_name']
        skinName = skinName.split("|")
        # 皮肤数量
        skin_numbers = len(skinName)

        print(str(eName) + cName + title + str(skin_numbers))

        db.session.add(User(cName, 'herorobort'))  #账号密码
        for j in range(1, skin_numbers + 1):
            pictureUrl = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(
                eName) + '/' + str(eName) + '-bigskin-' + str(j) + '.jpg'
            db.session.add(Image(pictureUrl, i + 1))
            n = n + 1
            db.session.add(Comment(skinName[j - 1], n, i + 1))
    db.session.commit()
Exemplo n.º 12
0
def init_database():
    """
    初始化数据库
    """
    db.drop_all()
    db.create_all()
    for i in range(100):
        db.session.add(
            User(
                'User' + str(i), 'a' + str(i),
                'http://jira.internal.hyperchain.cn/secure/useravatar?avatarId='
                + str(random.randint(10344, 10352))))
        db.session.add(
            Image(
                'http://08.imgmini.eastday.com/mobile/20171211/20171211181930_917f4f40b9fbaf2e06769b8fc77eefb2_1.jpeg',
                i + 1))
        for j in range(5):
            db.session.add(
                Image(
                    'http://jira.internal.hyperchain.cn/secure/useravatar?avatarId=10352'
                    + str(random.randint(10344, 10352)), i + 1))
            for k in range(3):
                db.session.add(
                    Comment('This is a comment {0}'.format(k + 1), i + 1,
                            i + 1))
    db.session.commit()
Exemplo n.º 13
0
def reg():
    #注册账号
    username = request.values.get('username').strip()
    password = request.values.get('password').strip()

    if username == '' or password == '':
        return redirect_with_msg('/regloginpage', '用户名和密码不能为空', 'reglogin')

    user= User.query.filter_by(username=username).first()
    if user != None:
        return redirect_with_msg('/regloginpage', '用户名已存在', 'reglogin')
    #更多判断

    salt = '.'.join(random.sample('0123456789abcde', 5))
    m = hashlib.md5()
    m.update((password + salt).encode("gbk"))
    password = m.hexdigest()
    user = User(username, password, salt)
    db.session.add(user)
    db.session.commit()
    login_user(user)

    next = request.values.get('next')
    if next != None and next.startswith('/') > 0:
        return redirect(next)

    return redirect('/')
Exemplo n.º 14
0
def init_database2():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('user' + str(i + 1), 'a' + str(i + 1)))
        for j in range(0, 7):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(Comment('hello' + str(k), 1 + 7 * i + j, i + 1))

    db.session.commit()

    for i in range(1, 16):
        user = User.query.get(i)
        user.username = '******' + user.username
    db.session.commit()
    for i in range(20, 30):
        # User.query.filter_by(id=i).update({'username':'******'+str(i)})
        User.query.filter_by(id=i + 1).update({'username': '******' + str(i)})
    db.session.commit()
    # 两种删除
    for j in range(20, 30):
        comment = Comment.query.get(j)
        db.session.delete(comment)
        # print(1,comment)
    db.session.commit()

    for j in range(40, 50):
        Comment.query.filter_by(id=j).delete()
    db.session.commit()
Exemplo n.º 15
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('user' + str(i), str(i)))
        for j in range(0, 10):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(Comment('小明给你评论了哦  ' + str(k), 1 + 10 * i + j, 1 + i))
    db.session.commit()
Exemplo n.º 16
0
def init_database():
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i), 'a' + str(i)))
        for j in range(0, 5):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 5):
                db.session.add(Comment('这是一条评论' + str(k), 1 + 5 * i + j,
                                       i + 1))
    db.session.commit()
Exemplo n.º 17
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0,100):
        db.session.add(User('User'+str(i+1),'a'+str(i)))
        for j in range(0,3):
            db.session.add(Image(get_image_url(),i+1))
            for k in range(0,3):
                db.session.add(Comment('This is a commnet'+str(k),1+3*i+j,i+1))

    db.session.commit()
Exemplo n.º 18
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('newcode' + str(i), 'a' + str(i)))

        for j in range(0, 10):  #每人发十张图
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(
                    Comment('newcode' + str(k), 1 + 10 * i + j, i + 1))
    db.session.commit()
Exemplo n.º 19
0
def init_database():
    #数据库清空
    db.drop_all()
    #数据库创建
    db.create_all()
    #数据库添加数据:用户名和密码(这个顺序见nowstagram.models里面User类的初始化顺序)
    for i in range(0, 100):
        db.session.add(User('User' + str(i), 'a' + str(i)))
        for j in range(0, 10):
            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 + 10 * i + j,
                            i + 1))
    #提交事务,如果不提交的话等于没做
    db.session.commit()

    #更新User表的username属性
    for i in range(50, 100, 2):
        user = User.query.get(i)
        user.username = '******' + user.username
    #for i in range(51, 100, 2):
    #User.query.filter_by(id=i).update({'username':'******'})
    User.query.filter_by(id=51).update({'username': '******'})
    db.session.commit()

    #删除评论
    for i in range(50, 100, 2):
        comment = Comment.query.get(i + 1)
        db.session.delete(comment)
    db.session.commit()

    #查询数据
    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 32, User.query.filter_by(id=5)
    print 42, User.query.order_by('id desc').limit(2)
    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()  #有的话就输出第一个没有的话就输出404
    print 9, User.query.paginate(page=1, per_page=10).items
    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
    image = Image.query.get(1)
    print 11, image.user
Exemplo n.º 20
0
def init_database():  # 有100个用户,每个用户三张图片,每个图片三个评论
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'a' + str(i)))  # 对user进行写入
        for j in range(0, 3):
            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()  # 提交往表里写数据的命令一定记得是在第一个for循环里!!!!!!!要不然没法写入

    # 数据库更新
    for i in range(50, 100, 2):  # 50到100的偶数的用户名进行修改
        user = User.query.get(i)
        user.username = '******' + user.username

    User.query.filter_by(id=51).update({'username': '******'})  # 第二种更新
    db.session.commit()

    # 数据库中删除一些评论
    for i in range(50, 100, 2):  # 编号50到100的双数编号评论删除
        comment = Comment.query.get(i + 1)
        db.session.delete(comment)
    db.session.commit()

    # 数据库数据查询
    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())  # id的降序,排完序列后offset偏移一个,在打出两个limit
    print(5,
          User.query.filter(User.username.endswith('0')).limit(
              3).all())  # 打出结尾是0的,但limit3个,只打出来三个
    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)  # 给数据库数据逆序分页,一页10个人
    # 一对多查询
    user = User.query.get(1)
    print(10, user.images)  # 把user1用户的图片全部查询出来,因为有关联,和外键关联

    image = Image.query.get(1)
    print(11, image, image.user)  # 从图片来查找用户,在models里面要进行一下关联
Exemplo n.º 21
0
def init_database():
    db.drop_all()
    db.create_all()
    get_image_url()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'a' + str(i + 1)))
        for j in range(0, 10):
            db.session.add(Image(imgs_url[random.randint(0, j * 200)], i + 1))
            for k in range(0, 3):
                db.session.add(
                    Comment('This is a comment' + str(k), 1 + 10 * i + j,
                            i + 1))

    db.session.commit()
Exemplo n.º 22
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i), 'a' + str(i)))
        for j in range(0, 3):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(
                    Comment('this is comment ' + str(k), int(1 + 3 * i + j),
                            int(i + 1)))
    db.session.commit()

    print 1, User.query.all()
    print 2, User.query.get(3)
Exemplo n.º 23
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), '0000'))
        for j in range(0, 10):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 5):
                db.session.add(
                    Comment('This is a comment' + str(k), 1 + 10 * i + j,
                            i + 1))

    db.session.commit()

    print "database initialized"
Exemplo n.º 24
0
def init_database1():
    urls = get_upload_url()
    un = 0
    db.drop_all()
    db.create_all()
    for i in range(0, 3):
        db.session.add(User('user' + str(i + 1), 'a' + str(i + 1)))
        for j in range(0, 3):
            if un < len(urls):
                db.session.add(Image(get_image_url(), i + 1))
                un = un + 1
                for k in range(0, 5):
                    db.session.add(
                        Comment('hello' + str(k), 1 + 3 * i + j, i + 1))

    db.session.commit()
Exemplo n.º 25
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'a' + str(i)))
        for j in range(0, 10):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(
                    Comment('This is comment' + str(k), 1 + i * 10 + j, i + 1))
    db.session.commit()

    for i in range(50, 100, 2):
        user = User.query.get(i)
        user.username = '******' + user.username

    # for i in range(51,100, 2):
    User.query.filter_by(id=51).update({'username': '******'})
    db.session.commit()

    for i in range(50, 100, 2):
        commit = Comment.query.get(i + 1)
        db.session.delete(commit)
    db.session.commit()

    user = User.query.get(2)
    print(10, user.images)

    # test 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(or_(User.id >= 88, User.id <= 99)).all())
    print(8,
          User.query.filter(or_(User.id >= 100, User.id <= 0)).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, get_image_url())
    image = Image.query.get(2)
    print(11, get_image_url(), image.user)
Exemplo n.º 26
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0,100):
        db.session.add(User('User'+str(i+1),'a'+str(i+1)))
        for j in range(0,3):
            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()


    for i in range(50,100,2):
        user=User.query.get(i)
        user.username='******'+user.username


    User.query.filter_by(id=51).update({'username':'******'})
    db.session.commit()


    for i in range(50,100,2):
        comment=Comment.query.get(i+1)
        db.session.delete(comment)
    db.session.commit()

    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<92)).all()
    print 8,User.query.filter(and_(User.id>88,User.id<92)).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


    image=Image.query.get(1)
    print 11,image.user
Exemplo n.º 27
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i), 'a' + str(i)))
        for j in range(0, 10):
            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), 1 + 10 * i + j,
                            i + 1))
    db.session.commit()

    print 1, User.query.all()
    print 2, User.query.get(3)
    user = User.query.get(1)
    print user.images
    image = Image.query.get(1)
    print image.user
Exemplo n.º 28
0
def init_database():
    db.drop_all()
    db.create_all()
    # 100个用户,每个用户3张图片,每张图片3条评论
    for user_i_id in range(0, 30):
        db.session.add(
            User(u'匿名用户' + str(user_i_id + 1),
                 'admin' + unicode(user_i_id) + '@qq.com',
                 'password' + str(random.randint(0, 1000))))  # id从1开始
        for image_j_id in range(0, 7):
            # 看Image类中的构造函数__init__的参数是几个,这里就传入几个,相当于类的实例化(或者说是表中一条数据的建立)
            # 第2个参数就是用的user_id,故从上面的遍历中直接传下来
            db.session.add(Image(get_image_url(), user_i_id + 1))
            for comment_k_id in range(0, 3):
                db.session.add(
                    Comment(u'这是一条评论:你长得真好看' + str(comment_k_id),
                            1 + 7 * user_i_id + image_j_id, user_i_id + 1))
                # 后面两个是image_id user_id,需要计算一下才是对应当前评论id的正确值;
    db.session.commit()  # 将数据插入后,需要commit一下,才能将数据真正的提交的数据库中;
    '''
Exemplo n.º 29
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'a' + str(i)))
        for j in range(0, 3):
            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()

    for i in range(50, 100, 2):
        user = User.query.get(i)
        user.username = '******' + user.username

    #User.query.filter_by(id=51).update({'username':'******'+user.username})
    db.session.commit()
Exemplo n.º 30
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('牛客' + str(i), 'a' + str(i)))

        for j in range(0, 3):  #每人发三张图
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(Comment('这是一条评论' + str(k), 1 + 3 * i + j,
                                       i + 1))
    db.session.commit()

    # 更新
    for i in range(0, 100, 10):
        # 通过update函数
        User.query.filter_by(id=i + 1).update({'username': '******' + str(i)})

    for i in range(1, 100, 2):
        # 通过设置属性
        u = User.query.get(i + 1)
        u.username = '******' + str(i * i)
    db.session.commit()

    # 删除
    for i in range(50, 100, 2):
        Comment.query.filter_by(id=i + 1).delete()
    for i in range(51, 100, 2):
        comment = Comment.query.get(i + 1)
        db.session.delete(comment)
    db.session.commit()

    print 1, User.query.all()
    print 2, User.query.get(3)  # primary key = 3
    print 3, User.query.filter_by(id=2).first()
    print 4, User.query.order_by(User.id.desc()).offset(1).limit(2).all()
    print 5, User.query.paginate(page=1, per_page=10).items
    u = User.query.get(1)
    print 6, u
    print 7, u.images
    print 8, Image.query.get(1).user