Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Add flask-peewee support
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Wright committed Apr 4, 2013
1 parent c4a7c6e commit 94ac607
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ python:
install:
- pip install . --quiet --use-mirrors
- "if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install importlib --quiet --use-mirrors; fi"
- pip install nose simplejson Flask-SQLAlchemy Flask-MongoEngine Flask-Mail mock MySQL-python --quiet --use-mirrors
- pip install nose simplejson Flask-SQLAlchemy Flask-MongoEngine Flask-Peewee Flask-Mail mock MySQL-python --quiet --use-mirrors

before_script:
- mysql -e 'create database flask_social_test;'
Expand Down
2 changes: 1 addition & 1 deletion flask_social/__init__.py
Expand Up @@ -14,6 +14,6 @@

from .core import Social
from .datastore import SQLAlchemyConnectionDatastore, \
MongoEngineConnectionDatastore
MongoEngineConnectionDatastore, PeeweeConnectionDatastore
from .signals import connection_created, connection_failed, login_failed, \
connection_removed, login_completed
30 changes: 29 additions & 1 deletion flask_social/datastore.py
Expand Up @@ -9,7 +9,8 @@
:license: MIT, see LICENSE for more details.
"""

from flask_security.datastore import SQLAlchemyDatastore, MongoEngineDatastore
from flask_security.datastore import SQLAlchemyDatastore, MongoEngineDatastore, \
PeeweeDatastore


class ConnectionDatastore(object):
Expand Down Expand Up @@ -83,3 +84,30 @@ def find_connection(self, **kwargs):

def find_connections(self, **kwargs):
return self._query(**kwargs)


class PeeweeConnectionDatastore(PeeweeDatastore, ConnectionDatastore):
"""A Peewee datastore implementation for Flask-Social."""

def __init__(self, db, connection_model):
PeeweeDatastore.__init__(self, db)
ConnectionDatastore.__init__(self, connection_model)

def _query(self, **kwargs):
if 'user_id' in kwargs:
kwargs['user'] = kwargs.pop('user_id')
try:
return self.connection_model.filter(**kwargs).get()
except self.connection_model.DoesNotExist:
return None

def create_connection(self, **kwargs):
if 'user_id' in kwargs:
kwargs['user'] = kwargs.pop('user_id')
return self.put(self.connection_model(**kwargs))

def find_connection(self, **kwargs):
return self._query(**kwargs)

def find_connections(self, **kwargs):
return self._query(**kwargs)
5 changes: 3 additions & 2 deletions setup.py
Expand Up @@ -34,15 +34,16 @@
include_package_data=True,
platforms='any',
install_requires=[
'Flask-Security>=1.5.0',
'Flask-Security>=1.6.0',
'Flask-OAuth>=0.12'
],
test_suite='nose.collector',
tests_require=[
'nose',
'mock',
'Flask-SQLAlchemy',
'Flask-MongoEngine'
'Flask-MongoEngine',
'Flask-Peewee'
],
dependency_links=[
'http://github.com/mattupstate/flask-security/tarball/develop#egg=Flask-Security-1.3.0-dev'
Expand Down
7 changes: 7 additions & 0 deletions tests/functional_tests.py
Expand Up @@ -4,6 +4,7 @@

from tests.test_app.sqlalchemy import create_app as create_sql_app
from tests.test_app.mongoengine import create_app as create_mongo_app
from tests.test_app.peewee_app import create_app as create_peewee_app


def get_mock_twitter_response():
Expand Down Expand Up @@ -49,6 +50,8 @@ def _create_app(self, auth_config):
return create_sql_app(auth_config, False)
if app_type == 'mongo':
return create_mongo_app(auth_config, False)
if app_type == 'peewee':
return create_peewee_app(auth_config, False)


class TwitterSocialTests(SocialTest):
Expand Down Expand Up @@ -126,3 +129,7 @@ def test_remove_connection(self, mock_authorize, mock_handle_oauth1_response, mo

class MongoEngineTwitterSocialTests(TwitterSocialTests):
APP_TYPE = 'mongo'


class PeeweeTwitterSocialTests(TwitterSocialTests):
APP_TYPE = 'peewee'
2 changes: 2 additions & 0 deletions tests/test_app/__init__.py
Expand Up @@ -6,6 +6,8 @@

class Config(object):

CSRF_ENABLED = False

SOCIAL_TWITTER = {
'consumer_key': 'xxxx',
'consumer_secret': 'xxxx'
Expand Down
74 changes: 74 additions & 0 deletions tests/test_app/peewee_app.py
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-

import sys
import os

sys.path.pop(0)
sys.path.insert(0, os.getcwd())

from flask_peewee.db import Database
from flask.ext.security import Security, UserMixin, RoleMixin, \
PeeweeUserDatastore
from flask.ext.social import Social, PeeweeConnectionDatastore
from peewee import *

from tests.test_app import create_app as create_base_app, populate_data


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()
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()

return app

if __name__ == '__main__':
create_app().run()
1 change: 0 additions & 1 deletion tests/test_app/sqlalchemy.py
Expand Up @@ -59,7 +59,6 @@ def before_first_request():
db.drop_all()
db.create_all()
populate_data()
pass

return app

Expand Down

0 comments on commit 94ac607

Please sign in to comment.