def create_app(config=None, debug=True): app = create_base_app(config, debug) app.config['DATABASE'] = { 'name': 'example2.db', 'engine': 'peewee.SqliteDatabase', } db = Database(app) class Role(db.Model, RoleMixin): name = TextField(unique=True) description = TextField(null=True) class User(db.Model, UserMixin): email = TextField() password = TextField() last_login_at = DateTimeField(null=True) current_login_at = DateTimeField(null=True) last_login_ip = TextField(null=True) current_login_ip = TextField(null=True) login_count = IntegerField(null=True) active = BooleanField(default=True) confirmed_at = DateTimeField(null=True) class UserRoles(db.Model): """ Peewee does not have built-in many-to-many support, so we have to create this mapping class to link users to roles.""" user = ForeignKeyField(User, related_name='roles') role = ForeignKeyField(Role, related_name='users') name = property(lambda self: self.role.name) description = property(lambda self: self.role.description) class Connection(db.Model): user = ForeignKeyField(User, related_name='connections') provider_id = TextField() provider_user_id = TextField() access_token = TextField() secret = TextField(null=True) display_name = TextField() full_name = TextField() profile_url = TextField() image_url = TextField() rank = IntegerField(null=True) app.security = Security(app, PeeweeUserDatastore(db, User, Role, UserRoles)) app.social = Social(app, PeeweeConnectionDatastore(db, Connection)) @app.before_first_request def before_first_request(): for Model in (Role, User, UserRoles, Connection): Model.drop_table(fail_silently=True) Model.create_table(fail_silently=True) populate_data() app.get_user = lambda: User.select().get() return app
def create_app(config, **kwargs): app = create_base_app(config) app.config['DATABASE'] = { 'name': 'peewee.db', 'engine': 'peewee.SqliteDatabase' } db = Database(app) class Role(db.Model, RoleMixin): name = TextField(unique=True) description = TextField(null=True) class User(db.Model, UserMixin): email = TextField() username = TextField() password = TextField() last_login_at = DateTimeField(null=True) current_login_at = DateTimeField(null=True) last_login_ip = TextField(null=True) current_login_ip = TextField(null=True) login_count = IntegerField(null=True) active = BooleanField(default=True) confirmed_at = DateTimeField(null=True) class UserRoles(db.Model): """ Peewee does not have built-in many-to-many support, so we have to create this mapping class to link users to roles.""" user = ForeignKeyField(User, related_name='roles') role = ForeignKeyField(Role, related_name='users') name = property(lambda self: self.role.name) description = property(lambda self: self.role.description) @app.before_first_request def before_first_request(): for Model in (Role, User, UserRoles): Model.drop_table(fail_silently=True) Model.create_table() populate_data(app.config.get('USER_COUNT', None)) app.security = Security(app, datastore=PeeweeUserDatastore(db, User, Role, UserRoles), **kwargs) add_context_processors(app.security) return app
active = BooleanField(default=True) confirmed_at = DateTimeField(null=True) class UserRoles(db.Model): # Because peewee does not come with built-in many-to-many # relationships, we need this intermediary class to link # user to roles. user = ForeignKeyField(User, related_name='roles') role = ForeignKeyField(Role, related_name='users') name = property(lambda self: self.role.name) description = property(lambda self: self.role.description) # Setup Flask-Security user_datastore = PeeweeUserDatastore(db, User, Role, UserRoles) security = Security(app, user_datastore) # Create a user to test with @app.before_first_request def create_user(): for Model in (Role, User, UserRoles): Model.drop_table(fail_silently=True) Model.create_table(fail_silently=True) user_datastore.create_user(email='*****@*****.**', password='******') # Views @app.route('/') @login_required