Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add flask-peewee support

  • Loading branch information...
commit 94ac607d235651edbaf7d1800fc2162ac20c7ea0 1 parent c4a7c6e
@mattupstate authored
View
2  .travis.yml
@@ -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;'
View
2  flask_social/__init__.py
@@ -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
View
30 flask_social/datastore.py
@@ -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):
@@ -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)
View
5 setup.py
@@ -34,7 +34,7 @@
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',
@@ -42,7 +42,8 @@
'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'
View
7 tests/functional_tests.py
@@ -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():
@@ -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):
@@ -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'
View
2  tests/test_app/__init__.py
@@ -6,6 +6,8 @@
class Config(object):
+ CSRF_ENABLED = False
+
SOCIAL_TWITTER = {
'consumer_key': 'xxxx',
'consumer_secret': 'xxxx'
View
74 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()
View
1  tests/test_app/sqlalchemy.py
@@ -59,7 +59,6 @@ def before_first_request():
db.drop_all()
db.create_all()
populate_data()
- pass
return app
Please sign in to comment.
Something went wrong with that request. Please try again.