コード例 #1
0
    def setUp(self):
        # Bug workarounds: Flask Admin和Flask Restful扩展中,
        # 它们会为应用生成蓝图对象并在内部保存起来,但在应用销毁时不会主动将其移除。
        admin._views = []
        rest_api.resources = []

        self.app = create_app('test')
        # 必须push context,否则会报错误
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client(use_cookies=True)

        # Bug workaround: 如果不在webapp目录中运行,
        # 则Flask SQLAlchemy的初始化代码就不能正确地在应用对象中进行初始化
        db.app = self.app
        db.create_all()

        # create role and user
        # 由于下面有个test_register_and_login测试,要注册新用户,
        # 在register路由中会默认添加上'poster'和'default'角色,因此这里要先创建两种角色
        poster = Role('poster')
        poster.description = 'poster role'
        default = Role('default')
        default.description = 'default role'
        db.session.add(poster)
        db.session.add(default)

        test_user = User('test')
        test_user.email = '*****@*****.**'
        test_user.password = '******'
        test_user.confirmed = True
        test_user.roles.append(poster)
        db.session.add(test_user)
        db.session.commit()
コード例 #2
0
def setup_db():
    try:
        db.create_all()
        admin_role = Role('admin')
        admin_role.name = "admin"
        admin_role.description = "admin"
        db.session.add(admin_role)

        default_role = Role('default')
        default_role.name = "default"
        default_role.description = "default"
        db.session.add(default_role)

        admin = User('admin')
        admin.username = "******"
        admin.set_password("password")
        admin.roles.append(admin_role)
        admin.roles.append(default_role)
        db.session.add(admin)
        db.session.commit()
    except:
        print("Already ran migration")
        pass
コード例 #3
0
ファイル: manage.py プロジェクト: Arkturian/Mastering-Flask
def setup_db():
    db.create_all()

    admin_role = Role()
    admin_role.name = "admin"
    admin_role.description = "admin"
    db.session.add(admin_role)

    default_role = Role()
    default_role.name = "default"
    default_role.description = "default"
    db.session.add(default_role)

    admin = User()
    admin.username = "******"
    admin.set_password("password")
    admin.roles.append(admin_role)
    admin.roles.append(default_role)
    db.session.add(admin)

    tag_one = Tag('Python')
    tag_two = Tag('Flask')
    tag_three = Tag('SQLAlechemy')
    tag_four = Tag('Jinja')
    tag_list = [tag_one, tag_two, tag_three, tag_four]

    s = "Body text"

    for i in xrange(100):
        new_post = Post("Post " + str(i))
        new_post.user = admin
        new_post.publish_date = datetime.datetime.now()
        new_post.text = s
        new_post.tags = random.sample(tag_list, random.randint(1, 3))
        db.session.add(new_post)

    db.session.commit()
コード例 #4
0
ファイル: manage.py プロジェクト: yaseenomer/Mastering-Flask
def setup_db():
    db.create_all()

    admin_role = Role()
    admin_role.name = "admin"
    admin_role.description = "admin"
    db.session.add(admin_role)

    default_role = Role()
    default_role.name = "default"
    default_role.description = "default"
    db.session.add(default_role)

    admin = User()
    admin.username = "******"
    admin.set_password("password")
    admin.roles.append(admin_role)
    admin.roles.append(default_role)
    db.session.add(admin)

    tag_one = Tag('Python')
    tag_two = Tag('Flask')
    tag_three = Tag('SQLAlechemy')
    tag_four = Tag('Jinja')
    tag_list = [tag_one, tag_two, tag_three, tag_four]

    s = "Body text"

    for i in xrange(100):
        new_post = Post("Post " + str(i))
        new_post.user = admin
        new_post.publish_date = datetime.datetime.now()
        new_post.text = s
        new_post.tags = random.sample(tag_list, random.randint(1, 3))
        db.session.add(new_post)

    db.session.commit()
コード例 #5
0
ファイル: manage.py プロジェクト: willa75/item_catalog
def setup_db():
    'Code to setup initial db with necessary info'
    db.create_all()

    admin_role = Role("admin")
    admin_role.description = "admin of applicaiton"
    db.session.add(admin_role)

    poster_role = Role("poster")
    poster_role.description = "user with ability to post items"
    db.session.add(poster_role)

    admin = User("admin")
    admin.set_password("password")
    admin.roles.append(admin_role)
    admin.roles.append(poster_role)
    db.session.add(admin)

    tag_one = Tag('clothes')
    tag_two = Tag('books')
    tag_three = Tag('technology')
    tag_four = Tag('sports')
    tag_five = Tag('music')
    tag_list = [tag_one, tag_two, tag_three, tag_four, tag_five]

    s = "Description text"

    for i in xrange(100):
        new_item = Item("Item {}".format(i))
        new_item.user = admin
        new_item.added_date = datetime.datetime.now()
        new_item.description = s
        new_item.tags = random.sample(tag_list, random.randint(1, 4))
        db.session.add(new_item)

    db.session.commit()
コード例 #6
0
ファイル: manager.py プロジェクト: ZakBiubiubiu/master_flask
def setup_db():
    db.create_all()

    # 创建管理员角色
    admin_role = Role('admin')
    admin_role.description = 'admin'
    db.session.add(admin_role)
    # 创建默认角色
    default_role = Role('default')
    default_role.description = 'default'
    db.session.add(default_role)

    # 创建管理员用户信息
    admin = User('admin')
    admin.set_password('password')
    admin.roles.append(admin_role)
    admin.roles.append(default_role)
    db.session.add(admin)

    tag_one = Tag('Python')
    tag_two = Tag('Flask')
    tag_three = Tag('SQLAlchemy')
    tag_four = Tag('Jinja')
    tag_list = [tag_one, tag_two, tag_three, tag_four]

    s = 'Body text'

    for i in range(100):
        new_post = Post('Post %s' % i, s)
        new_post.user = admin
        new_post.publish_date = datetime.datetime.now()
        new_post.tags = random.sample(tag_list, random.randint(1, 3))

        db.session.add(new_post)

    db.session.commit()
コード例 #7
0
def insert_data():
    with app.app_context():
        # 不需要在这里创建库,应该使用数据库升级命令`db upgrade`来创建库
        # db.create_all()

        # 这里设定了3种角色
        role_admin = Role(name='admin')
        role_admin.description = "administrator role"
        role_poster = Role(name='poster')
        role_poster.description = "the registered user role"
        role_default = Role(name='default')
        role_default.description = 'the unregistered user role'
        db.session.add(role_admin)
        db.session.add(role_poster)
        db.session.add(role_default)

        # add User
        admin = User(username='******')
        admin.email = '*****@*****.**'
        admin.password = '******'
        admin.confirmed = True
        admin.roles.append(role_admin)
        admin.roles.append(role_poster)
        admin.roles.append(role_default)
        db.session.add(admin)

        user01 = User(username='******')
        user01.email = '*****@*****.**'
        user01.password = '******'
        user01.confirmed = True
        user01.roles.append(role_poster)
        user01.roles.append(role_default)
        db.session.add(user01)

        user02 = User(username='******')
        user02.email = '*****@*****.**'
        user02.password = '******'
        user02.confirmed = True
        user02.roles.append(role_poster)
        user02.roles.append(role_default)
        db.session.add(user02)

        # add Tag and Post
        tag_one = Tag('Python')
        tag_two = Tag('Flask')
        tag_three = Tag('SQLAlechemy')
        tag_four = Tag('Jinja')
        tag_list = [tag_one, tag_two, tag_three, tag_four]

        s = "Example Text"

        for i in xrange(1, 101):
            new_post = Post("Post {}".format(i))
            if i % 2:
                new_post.user = user01
            else:
                new_post.user = user02
            new_post.publish_date = datetime.datetime.utcnow()
            new_post.text = s
            new_post.tags = random.sample(tag_list, random.randint(1, 3))
            db.session.add(new_post)

        # add comment
        comment01 = Comment()
        comment01.name = 'comment01'
        comment01.text = 'comment text'
        comment01.post_id = 99
        comment01.date = datetime.datetime.utcnow()
        db.session.add(comment01)

        comment02 = Comment()
        comment02.name = 'comment02'
        comment02.text = 'comment text'
        comment02.post_id = 100
        comment02.date = datetime.datetime.utcnow()
        db.session.add(comment02)

        db.session.commit()
コード例 #8
0
ファイル: manage.py プロジェクト: Gackle/zako-for-bit-war
def setup():
    db.create_all()

    admin_role = Role("admin")
    admin_role.description = "admin"
    db.session.add(admin_role)

    default_role = Role("default")
    default_role.description = "default"
    db.session.add(default_role)

    guest_role = Role("guest")
    guest_role.description = "guest"
    db.session.add(guest_role)

    db.session.commit()

    admin = User("Admin")
    admin.nickname = "管理员"
    admin.email = "*****@*****.**"
    admin.set_password('admin')
    admin.roles.append(admin_role)
    db.session.add(admin)

    official = User("PublicOfficial")
    official.nickname = "公共库用户"
    official.email = "*****@*****.**"
    official.set_password('official')
    admin.roles.append(admin_role)
    db.session.add(official)

    db.session.commit()

    # 创建原始公共库目录(共5个)
    script1 = Script('always_work.sg')
    script1.content = """Strategy T1:
    return 1"""
    script1.description = "永远合作"
    script1.user = official
    script1.is_private = False
    db.session.add(script1)

    script2 = Script('random_example.sg')
    script2.content = "Strategy T2:i = RANDOM(1);if(i==0){return 0}else{return 1}"
    script2.description = "`听天由命`,背叛与合作的几率为 50% (RANDOM 为系统范围内真随机)"
    script2.user = official
    script2.is_private = False
    db.session.add(script2)

    script3 = Script('slow_work.sg')
    script3.content = "Strategy T3:if(COUNT == 1){return 1}else{return CUR}"
    script3.description = "`慢半拍`:开局合作,随后一直跟随对方上一轮的选择"
    script3.user = official
    script3.is_private = False
    db.session.add(script3)

    script4 = Script('elusive_work.sg')
    script4.content = "Strategy T4:if (COUNT == 1){return 1}else {i = RANDOM(9);if (i == 9){return 0}else{return CUR}}"
    script4.description = "改进版`慢半拍`,开局合作,随后随机自我背叛,大几率跟随对方上一轮的选择"
    script4.user = official
    script4.is_private = False
    db.session.add(script4)

    script5 = Script('never_forgive.sg')
    script5.content = "Strategy T5:if(FLAG){return 0}else{return 1}"
    script5.description = "永不原谅,只要对方存在过背叛行为,就一直背叛"
    script5.user = official
    script5.is_private = False
    db.session.add(script5)

    db.session.commit()

    # 创建文章评论
    comment1 = Comment('comment1')
    comment1.user = admin
    comment2 = Comment('comment2')
    comment2.user = official
    comment3 = Comment('comment3')
    comment3.user = admin
    db.session.add(comment1)
    db.session.add(comment2)
    db.session.add(comment3)
    db.session.commit()

    post1 = Post('Post1', 'Content1')
    post1.user = official
    post1.category = PostCategory.General
    post1.comments.append(comment1)
    post1.comments.append(comment2)

    post2 = Post('Post2', 'Content2')
    post2.user = admin
    post2.category = PostCategory.Feedback
    post2.comments.append(comment3)

    db.session.add(post1)
    db.session.add(post2)

    db.session.commit()