Exemplo n.º 1
0
 def initdb(drop):
     if drop:
         click.confirm(
             'This operation will delete the database, doyou want to continue?',
             abort=True)
         db.drop_all()
         click.echo('Drop tables.')
     db.create_all()
     click.echo('Initialized database.')
Exemplo n.º 2
0
    def setUp(self):
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        user = User(nickname='Grey Li', email='*****@*****.**')
        user.set_password('123')
        db.session.add(user)
        db.session.commit()
Exemplo n.º 3
0
    def setUp(self) -> None:
        app = create_app('testing')
        self.context = app.test_request_context()  # 为了url_for()的正常使用需要注册请求上下文
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        user = User(email='*****@*****.**', nickname='guoxy2016')
        user.password = '******'
        message = Message(body='Test Message 1', author=user)

        normal_user = User(email='*****@*****.**', nickname='normal_user')
        normal_user.password = '******'
        db.session.add_all([message, normal_user])
        db.session.commit()
Exemplo n.º 4
0
    def forge(message):
        """Generate fake data."""
        import random
        from sqlalchemy.exc import IntegrityError

        from faker import Faker

        fake = Faker()

        click.echo('Initializing the database...')
        db.drop_all()
        db.create_all()

        click.echo('Forging the data')
        admin = User(nickname="admin", email="*****@*****.**")
        admin.password = "******"
        db.session.add(admin)
        db.session.commit()

        click.echo('Generating users...')
        for i in range(50):
            user = User(nickname=fake.name(),
                        bio=fake.sentence(),
                        github=fake.url(),
                        website=fake.url(),
                        email=fake.email())
            user.password = user.email
            db.session.add(user)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

        click.echo('Generating messages...')
        for i in range(message):
            message = Message(
                author=User.query.get(random.randint(1, User.query.count())),
                body=fake.sentence(),
                timestamp=fake.date_time_between('-30d', '-2d'),
            )
            db.session.add(message)
        db.session.commit()
        click.echo('Done.')
Exemplo n.º 5
0
    def forge(message):
        import random
        from sqlalchemy.exc import IntegrityError

        from faker import Faker

        fake = Faker('zh_CN')

        click.echo('Initializing the database...')
        db.drop_all()
        db.create_all()

        click.echo('Forging the data...')
        admin = User(nickname='CoderShui', email='*****@*****.**')
        admin.set_password('123456')
        db.session.add(admin)
        db.session.commit()

        click.echo('Generating users...')
        for i in range(50):
            user = User(nickname=fake.name(),
                        bio=fake.sentence(),
                        github=fake.url(),
                        website=fake.url(),
                        email=fake.email())
            user.set_password('123456')
            db.session.add(user)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

        click.echo('Generating messges....')
        for i in range(message):
            message = Message(
                author=User.query.get(random.randint(1, User.query.count())),
                body=fake.sentence(),
                timestamp=fake.date_time_between('-30d', '-2d'),
            )
            db.session.add(message)
        db.session.commit()
        click.echo('Done.')
Exemplo n.º 6
0
    def forge(message):
        """Generate fake data."""
        import random
        from sqlalchemy.exc import IntegrityError

        from faker import Faker

        fake = Faker()

        click.echo('Initializing the database...')
        db.drop_all()
        db.create_all()

        click.echo('Forging the data...')
        admin = User(nickname='Grey Li', email='*****@*****.**')
        admin.set_password('helloflask')
        db.session.add(admin)
        db.session.commit()

        click.echo('Generating users...')
        for i in range(2, 52):
            user = User(id=i,
                        nickname=fake.name(),
                        bio=fake.sentence(),
                        github=fake.url(),
                        website=fake.url(),
                        email=fake.email())
            db.session.add(user)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

        click.echo("creating a room containing these users.")
        room = Room(id=1,
                    name="Family - All",
                    cur_user_total=0,
                    user_total=0,
                    owner=admin.id)
        for i in range(2, 52):
            user_room = UserRoom(
                user_id=i,
                room_id=1,
            )
            db.session.add(user_room)
            room.user_total += 1
        db.session.add(room)

        db.session.commit()

        click.echo('Generating messages...')
        for i in range(message):
            message = Message(author=User.query.get(
                random.randint(1, User.query.count())),
                              body=fake.sentence(),
                              timestamp=fake.date_time_between('-30d', '-2d'),
                              room_id=1)
            db.session.add(message)

        db.session.commit()
        click.echo('Done.')