def test_deferred_database(self):
        app = Flask(__name__)
        app.config.update({
            'DATABASE': {
                'name': ':memory:',
                'engine': 'peewee.SqliteDatabase'
            }
        })

        # Defer initialization of the database.
        database = FlaskDB()

        # Ensure we can access the Model attribute.
        Model = database.Model
        model_db = Model._meta.database

        # Because the database is not initialized, the models will point
        # to an uninitialized Proxy object.
        self.assertTrue(isinstance(model_db, Proxy))
        self.assertRaises(AttributeError, lambda: model_db.database)

        class User(database.Model):
            username = CharField(unique=True)

        # Initialize the database with our Flask app.
        database.init_app(app)

        # Ensure the `Model` property points to the same object as it
        # did before.
        PostInitModel = database.Model
        self.assertTrue(Model is PostInitModel)

        # Ensure that the proxy is initialized.
        self.assertEqual(model_db.database, ':memory:')

        # Ensure we can use our database.
        User.create_table()
        for username in ['charlie', 'huey', 'zaizee']:
            User.create(username=username)

        self.assertEqual(User.select().count(), 3)
        users = User.select().order_by(User.username)
        self.assertEqual([user.username for user in users],
                         ['charlie', 'huey', 'zaizee'])

        self.assertEqual(User._meta.database, database.database)
示例#2
0
    def test_deferred_database(self):
        app = Flask(__name__)
        app.config.update({
            'DATABASE': {
                'name': ':memory:',
                'engine': 'peewee.SqliteDatabase'}})

        # Defer initialization of the database.
        database = FlaskDB()

        # Ensure we can access the Model attribute.
        Model = database.Model
        model_db = Model._meta.database

        # Because the database is not initialized, the models will point
        # to an uninitialized Proxy object.
        self.assertTrue(isinstance(model_db, Proxy))
        self.assertRaises(AttributeError, lambda: model_db.database)

        class User(database.Model):
            username = CharField(unique=True)

        # Initialize the database with our Flask app.
        database.init_app(app)

        # Ensure the `Model` property points to the same object as it
        # did before.
        PostInitModel = database.Model
        self.assertTrue(Model is PostInitModel)

        # Ensure that the proxy is initialized.
        self.assertEqual(model_db.database, ':memory:')

        # Ensure we can use our database.
        User.create_table()
        for username in ['charlie', 'huey', 'zaizee']:
            User.create(username=username)

        self.assertEqual(User.select().count(), 3)
        users = User.select().order_by(User.username)
        self.assertEqual(
            [user.username for user in users],
            ['charlie', 'huey', 'zaizee'])

        self.assertEqual(User._meta.database, database.database)
示例#3
0
from playhouse.shortcuts import model_to_dict

import logging
logger = logging.getLogger('peewee')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

APP_DIR = os.path.dirname(os.path.realpath(__file__))
DATABASE = 'sqliteext:///%s' % os.path.join(APP_DIR, 'test.db')
DEBUG = False

app = Flask(__name__)
app.config.from_object(__name__)

flaskDb = FlaskDB()
flaskDb.init_app(app)

class Account(flaskDb.Model):
    username = CharField(primary_key = True)
    password = CharField()
    auth_service = CharField()
    last_fail_time = DateTimeField(default = datetime(2010,01,01))
    last_used_time = DateTimeField(default = datetime(2010,01,01))
    pause_until = DateTimeField(default = datetime(2010,01,01))
    needs_captcha = BooleanField(default = False)
    is_banned = BooleanField(default = False)
    uuid = CharField()
    atomic = CharField()

    def ping(self, force=False):
        if not force and self.is_banned:
    CharField,
    IntegerField,
    BooleanField,
    SqliteDatabase,
    IntegrityError,
    DoesNotExist,
)
from playhouse.flask_utils import FlaskDB
from playhouse.shortcuts import model_to_dict, update_model_from_dict

app = Flask(__name__, template_folder="", static_folder="", static_url_path="")

db = SqliteDatabase("todo.db")

flask_db = FlaskDB(database=db)
flask_db.init_app(app)


class ToDo(Model):
    class Meta:
        database = db

    title = CharField(max_length=255)
    order = IntegerField()
    done = BooleanField(default=False)


if not db.table_exists("todo"):
    db.create_tables([ToDo])