├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.md ├── flask_rethinkdb └── __init__.py ├── setup.py └── tests └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | __pycache__ 21 | 22 | # Installer logs 23 | pip-log.txt 24 | 25 | # Unit test / coverage reports 26 | .coverage 27 | .tox 28 | nosetests.xml 29 | 30 | # Translations 31 | *.mo 32 | 33 | # Mr Developer 34 | .mr.developer.cfg 35 | .project 36 | .pydevproject 37 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | before_install: 3 | - sudo add-apt-repository ppa:rethinkdb/ppa -y 4 | - sudo apt-get update -qq 5 | - sudo apt-get install rethinkdb -y 6 | install: 7 | - pip install . 8 | before_script: 9 | - rethinkdb --daemon 10 | script: 11 | - nosetests tests 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Andrei Horak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | flask-rethinkdb 2 | =============== 3 | 4 | [![Build Status](https://travis-ci.org/linkyndy/flask-rethinkdb.png)](https://travis-ci.org/linkyndy/flask-rethinkdb) 5 | 6 | Adds RethinkDB support to Flask http://rethinkdb.com 7 | -------------------------------------------------------------------------------- /flask_rethinkdb/__init__.py: -------------------------------------------------------------------------------- 1 | import rethinkdb as r 2 | from flask import current_app 3 | 4 | 5 | # Since no older versions than 0.9 are supported for Flask, this is safe 6 | from flask import _app_ctx_stack as stack 7 | 8 | 9 | class RethinkDB(object): 10 | 11 | def __init__(self, app=None, db=None): 12 | self.app = app 13 | self.db = db 14 | if app is not None: 15 | self.init_app(app) 16 | 17 | def init_app(self, app): 18 | app.config.setdefault('RETHINKDB_HOST', 'localhost') 19 | app.config.setdefault('RETHINKDB_PORT', '28015') 20 | app.config.setdefault('RETHINKDB_AUTH', '') 21 | app.config.setdefault('RETHINKDB_DB', 'test') 22 | 23 | @app.teardown_appcontext 24 | def teardown(exception): 25 | ctx = stack.top 26 | if hasattr(ctx, 'rethinkdb'): 27 | ctx.rethinkdb.close() 28 | 29 | def connect(self): 30 | return r.connect(host=current_app.config['RETHINKDB_HOST'], 31 | port=current_app.config['RETHINKDB_PORT'], 32 | auth_key=current_app.config['RETHINKDB_AUTH'], 33 | db=self.db or current_app.config['RETHINKDB_DB']) 34 | 35 | @property 36 | def conn(self): 37 | ctx = stack.top 38 | if ctx is not None: 39 | if not hasattr(ctx, 'rethinkdb'): 40 | ctx.rethinkdb = self.connect() 41 | return ctx.rethinkdb 42 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """ 2 | Flask-RethinkDB 3 | ---------------- 4 | 5 | Adds RethinkDB support to Flask. 6 | 7 | """ 8 | 9 | from setuptools import setup, find_packages 10 | 11 | 12 | setup( 13 | name='Flask-RethinkDB', 14 | version='0.2', 15 | url='http://github.com/linkyndy/flask-rethinkdb', 16 | license='MIT', 17 | author='Andrei Horak', 18 | author_email='linkyndy@yahoo.com', 19 | description='Adds RethinkDB support to Flask', 20 | long_description=__doc__, 21 | packages=find_packages(), 22 | zip_safe=False, 23 | include_package_data=True, 24 | platforms='any', 25 | install_requires=[ 26 | 'flask >= 0.9', 27 | 'rethinkdb', 28 | ], 29 | classifiers=[ 30 | 'Environment :: Web Environment', 31 | 'Intended Audience :: Developers', 32 | 'License :: OSI Approved :: MIT License', 33 | 'Operating System :: OS Independent', 34 | 'Programming Language :: Python', 35 | 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 36 | 'Topic :: Software Development :: Libraries :: Python Modules' 37 | ] 38 | ) 39 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | import rethinkdb as r 3 | from rethinkdb.errors import RqlDriverError, RqlRuntimeError 4 | 5 | from flask import Flask 6 | from flask_rethinkdb import RethinkDB 7 | 8 | 9 | class InitTests(TestCase): 10 | 11 | def test_connection_one(self): 12 | app = Flask(__name__) 13 | db = RethinkDB(app) 14 | 15 | with app.test_request_context(): 16 | try: 17 | # Make sure RethinkDB is turned on! 18 | r.table_create('table').run(db.conn) 19 | except (RqlDriverError, RqlRuntimeError) as e: 20 | self.fail(e) 21 | else: 22 | # Do some cleanup 23 | r.table_drop('table').run(db.conn) 24 | 25 | def test_connection_two(self): 26 | app = Flask(__name__) 27 | db = RethinkDB() 28 | db.init_app(app) 29 | 30 | with app.test_request_context(): 31 | try: 32 | # Make sure RethinkDB is turned on! 33 | r.table_create('table').run(db.conn) 34 | except (RqlDriverError, RqlRuntimeError) as e: 35 | self.fail(e) 36 | else: 37 | # Do some cleanup 38 | r.table_drop('table').run(db.conn) 39 | 40 | def test_connection_with_database(self): 41 | app = Flask(__name__) 42 | db = RethinkDB(app, db='test') 43 | 44 | with app.test_request_context(): 45 | try: 46 | # Make sure RethinkDB is turned on! 47 | r.table_create('table').run(db.conn) 48 | except (RqlDriverError, RqlRuntimeError) as e: 49 | self.fail(e) 50 | else: 51 | # Do some cleanup 52 | r.table_drop('table').run(db.conn) 53 | 54 | def test_connection_with_inexisting_database(self): 55 | app = Flask(__name__) 56 | db = RethinkDB(app, db='doesnotexist') 57 | 58 | with app.test_request_context(): 59 | try: 60 | # Make sure RethinkDB is turned on! 61 | # Specifying an inexisting database should raise an exception 62 | r.table_create('table').run(db.conn) 63 | except (RqlDriverError, RqlRuntimeError): 64 | pass 65 | else: 66 | # Do some cleanup 67 | r.table_drop('table').run(db.conn) 68 | self.fail("Should have raised a RqlDriverError") 69 | --------------------------------------------------------------------------------