Skip to content

Commit

Permalink
Removed app.mail, app.babel, app.db and app.User
Browse files Browse the repository at this point in the history
  • Loading branch information
lingthio committed Apr 12, 2014
1 parent 7227b19 commit 0ff337b
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 78 deletions.
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Flask-User
==========

.. image:: https://pypip.in/v/Flask-User/badge.png
:target: https://pypi.python.org/pypi/Flask-User

.. image:: https://travis-ci.org/lingthio/Flask-User.png?branch=master
:target: https://travis-ci.org/lingthio/Flask-User

.. comment .. image:: https://pypip.in/v/Flask-User/badge.png
.. comment :target: https://pypi.python.org/pypi/Flask-User
.. comment .. image:: https://coveralls.io/repos/lingthio/Flask-User/badge.png?branch=master
.. comment :target: https://coveralls.io/r/lingthio/Flask-User?branch=master
.. image:: https://pypip.in/d/Flask-User/badge.png
:target: https://pypi.python.org/pypi/Flask-User
.. comment .. image:: https://pypip.in/d/Flask-User/badge.png
.. comment :target: https://pypi.python.org/pypi/Flask-User
.. image:: https://pypip.in/license/Flask-User/badge.png
:target: https://pypi.python.org/pypi/Flask-User
.. comment .. image:: https://pypip.in/license/Flask-User/badge.png
.. comment :target: https://pypi.python.org/pypi/Flask-User
Customizable User Account Management for Flask
----------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Feeling generous? `Tip me on Gittip <https://www.gittip.com/lingthio/>`_

Revision History
----------------
* v0.4.8 Removed the need for app.mail, app.babel, app.db and app.User
* v0.4.7 Added 'confirm_email', 'password_changed' and 'username_changed' emails.

::
Expand Down
19 changes: 8 additions & 11 deletions example_apps/basic_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class ConfigClass(object):
MAIL_DEFAULT_SENDER = '"Sender" <noreply@example.com>'

# Configure Flask-User
USER_ENABLE_USERNAME = True # Register and Login with username
USER_ENABLE_CONFIRM_EMAIL = True # Require Email confirmation
USER_ENABLE_USERNAME = True
USER_ENABLE_CONFIRM_EMAIL = True
USER_ENABLE_CHANGE_USERNAME = True
USER_ENABLE_CHANGE_PASSWORD = True
USER_ENABLE_FORGOT_PASSWORD = True
Expand All @@ -36,15 +36,14 @@ def create_app(test_config=None): # For automated tests
try: app.config.from_object('local_settings')
except: pass

# Over-write app config # For automated tests
# Load optional test_config # For automated tests
if test_config:
for key, value in test_config.items():
app.config[key] = value
app.config.update(test_config)

# Setup Flask-Mail, Flask-Babel and Flask-SQLAlchemy
app.mail = Mail(app)
app.babel = babel = Babel(app)
app.db = db = SQLAlchemy(app)
# Initialize Flask extensions
babel = Babel(app) # Initialize Flask-Babel
db = SQLAlchemy(app) # Initialize Flask-SQLAlchemy
mail = Mail(app) # Initialize Flask-Mail

@babel.localeselector
def get_locale():
Expand All @@ -60,7 +59,6 @@ class User(db.Model, UserMixin):
username = db.Column(db.String(50), nullable=False, unique=True)
confirmed_at = db.Column(db.DateTime())
reset_password_token = db.Column(db.String(100), nullable=False, default='')
app.User = User

# Create all database tables
db.create_all()
Expand Down Expand Up @@ -103,7 +101,6 @@ def profile_page():

return app


# Start development web server
if __name__=='__main__':
app = create_app()
Expand Down
6 changes: 3 additions & 3 deletions example_apps/minimal_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class ConfigClass(object):
app = Flask(__name__)
app.config.from_object(__name__+'.ConfigClass')

# Setup Flask-Babel and Flask-SQLAlchemy
app.babel = babel = Babel(app)
app.db = db = SQLAlchemy(app)
# Initialize Flask extensions
babel = Babel(app) # Initialize Flask-Babel
db = SQLAlchemy(app) # Initialize Flask-SQLAlchemy

@babel.localeselector
def get_locale():
Expand Down
1 change: 0 additions & 1 deletion example_apps/register_form_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class User(db.Model, UserMixin):
# Relationships
roles = db.relationship('Role', secondary=user_roles,
backref=db.backref('users', lazy='dynamic'))
app.User = User

# Reset all the database tables
db.create_all()
Expand Down
14 changes: 6 additions & 8 deletions example_apps/roles_required_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ def create_app(test_config=None): # For automated tests
try: app.config.from_object('local_settings')
except: pass

# Over-write app config # For automated tests
# Load optional test_config # For automated tests
if test_config:
for key, value in test_config.items():
app.config[key] = value
app.config.update(test_config)

# Setup Flask-Mail, Flask-Babel and Flask-SQLAlchemy
app.mail = Mail(app)
app.babel = babel = Babel(app)
app.db = db = SQLAlchemy(app)
# Initialize Flask extensions
babel = Babel(app) # Initialize Flask-Babel
mail = Mail(app) # Initialize Flask-Mail
db = SQLAlchemy(app) # Initialize Flask-SQLAlchemy

@babel.localeselector
def get_locale():
Expand Down Expand Up @@ -75,7 +74,6 @@ class User(db.Model, UserMixin):
# Relationships
roles = db.relationship('Role', secondary=user_roles,
backref=db.backref('users', lazy='dynamic'))
app.User = User

# Reset all the database tables
db.create_all()
Expand Down
1 change: 0 additions & 1 deletion example_apps/user_profile_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class User(db.Model, UserMixin):
user_profile = db.relationship('UserProfile', uselist=False, foreign_keys=[user_profile_id])
roles = db.relationship('Role', secondary=user_roles,
backref=db.backref('users', lazy='dynamic'))
app.User = User

class UserProfile(db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand Down
4 changes: 4 additions & 0 deletions flask_user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def init_app(self, app):
""" Initialize app.user_manager."""
# Bind Flask-USER to app
app.user_manager = self
# Flask seems to also support the current_app.extensions[] list
if not hasattr(app, 'extensions'):
app.extensions = {}
app.extensions['user'] = self

# Set defaults for undefined settings
settings.set_default_settings(self, app.config)
Expand Down
21 changes: 13 additions & 8 deletions flask_user/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@ def send_email(recipient, subject, html_message, text_message):
class SendEmailError(Exception):
pass

# Make sure that Flask-Mail has been installed
try:
from flask_mail import Message
except:
raise SendEmailError("Flask-Mail is missing. Please install Flask-Mail ('pip install Flask-Mail').")
raise SendEmailError("Flask-Mail has not been installed. Use 'pip install Flask-Mail' to install Flask-Mail.")

# Construct Flash-Mail message
message = Message(subject,
recipients=[recipient],
html = html_message,
body = text_message)
# Make sure that Flask-Mail has been initialized
mail_engine = current_app.extensions.get('mail', None)
if not mail_engine:
raise SendEmailError('Flask-Mail has not been initialized.')

# Send email using Flash-Mail
try:
current_app.mail.send(message)

# Construct Flash-Mail message
message = Message(subject,
recipients=[recipient],
html = html_message,
body = text_message)
mail_engine.send(message)

# Print helpful error messages on exceptions
except (socket.gaierror, socket.error) as e:
Expand Down
3 changes: 2 additions & 1 deletion flask_user/tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
# Create app
app = tst_app.create_app(test_config)
app.testing = True # Propagate exceptions (don't show 500 error page)
db = app.user_manager.db_adapter.db

# create client
client = tstutils.TstClient(app.test_client())
client = tstutils.TstClient(app.test_client(), db)

# Create test case
class TestFlaskUserForms(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions flask_user/tests/test_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_authorization(client):
um.enable_email = False
um.enable_confirm_email = False
um.enable_retype_password = False
User = current_app.User

# Test as anonymous user
client.get_valid_page(url_for('home_page'))
Expand All @@ -55,10 +54,11 @@ def test_authorization(client):
client.get_invalid_page(url_for('special_page'), "You do not have permission to access ")

# Delete userX
User = um.db_adapter.UserClass
user = User.query.filter(User.username=='userX').first()
assert(user)
current_app.db.session.delete(user)
current_app.db.session.commit()
client.db.session.delete(user)
client.db.session.commit()

# Log in as user007 without roles 'special' and 'agent'
client.login(username='user007', password='Password1')
Expand Down
30 changes: 15 additions & 15 deletions flask_user/tests/test_invalid_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def test_init(db):
um.enable_retype_password = True

hashed_password = um.hash_password('Password1')
User = current_app.User
User = um.db_adapter.UserClass

# Create user1 with username and email
user1 = User(username='user1', email='user1@example.com', password=hashed_password, active=True)
assert user1
Expand All @@ -98,11 +98,10 @@ def test_init(db):
def test_invalid_register_with_username_form(client):
print("test_invalid_register_with_username_form")

User = current_app.User

# Choose config
um = current_app.user_manager
um.enable_username = True
User = um.db_adapter.UserClass

# Set default values
url = url_for('user.register')
Expand Down Expand Up @@ -143,11 +142,10 @@ def test_invalid_register_with_username_form(client):
def test_invalid_register_with_email_form(client):
print("test_invalid_register_with_email_form")

User = current_app.User

# Choose config
um = current_app.user_manager
um.enable_username = False
User = um.db_adapter.UserClass

# Set default values
url = url_for('user.register')
Expand Down Expand Up @@ -183,8 +181,6 @@ def test_invalid_register_with_email_form(client):
def test_invalid_confirm_email_page(client):
print("test_invalid_confirm_email_page")

User = current_app.User

# Test Invalid token
url = url_for('user.confirm_email', token='InvalidToken')
client.get_invalid_page(url, 'Invalid confirmation token.')
Expand Down Expand Up @@ -374,12 +370,14 @@ def test_invalid_reset_password(client):
new_password = new_password, retype_password='XPassword5')

def test_valid_roles(client):
um = current_app.user_manager
User = um.db_adapter.UserClass

# Perform only for roles_required_app
user007 = current_app.User.query.filter(current_app.User.username=='user007').first()
user007 = User.query.filter(User.username=='user007').first()
if not user007: return

print("test_valid_roles")
um = current_app.user_manager
um.enable_username = True

client.login(username='user007', password='Password1')
Expand All @@ -389,12 +387,14 @@ def test_valid_roles(client):
client.logout()

def test_invalid_roles(client):
um = current_app.user_manager
User = um.db_adapter.UserClass

# Perform only for roles_required_app
user007 = current_app.User.query.filter(current_app.User.username=='user007').first()
user007 = User.query.filter(User.username=='user007').first()
if not user007: return

print("test_invalid_roles")
um = current_app.user_manager
um.enable_username = True

client.login(username='user1', password='Password1')
Expand Down Expand Up @@ -427,7 +427,7 @@ def test_login_without_confirm(client):
password=password)

# Confirm email manually, but disable account
User = current_app.User
User = um.db_adapter.UserClass
user = User.query.filter(User.email==email).first()
assert(user)
user.active = False
Expand Down Expand Up @@ -457,7 +457,7 @@ def test_cleanup(db):
# Workaround for py.test coverage issue
def run_all_tests(client):
print()
test_init(current_app.db)
test_init(client.db)
test_invalid_register_with_username_form(client)
test_invalid_register_with_email_form(client)
test_invalid_confirm_email_page(client)
Expand All @@ -473,7 +473,7 @@ def run_all_tests(client):

test_login_without_confirm(client)

test_cleanup(current_app.db)
test_cleanup(client.db)

# TODO:
# Register without confirming email and try to log in
Expand Down
6 changes: 3 additions & 3 deletions flask_user/tests/test_valid_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def check_all_valid_forms(um, client):
# ENABLE_CHANGE_USERNAME=True must have ENABLE_USERNAME=True.
if um.enable_change_username and not um.enable_username: return

check_valid_register_form(um, client, current_app.db)
check_valid_register_form(um, client, client.db)
check_valid_confirm_email_page(um, client)
check_valid_login_form(um, client)
check_valid_change_password_form(um, client)
Expand All @@ -112,12 +112,12 @@ def check_all_valid_forms(um, client):
check_valid_forgot_password_form(um, client)
check_valid_reset_password_page(um, client)

delete_user1(current_app.db)
delete_user1(client.db)

def check_valid_register_form(um, client, db):
# Using global variable for speed
global user1
User = current_app.User
User = um.db_adapter.UserClass

# Define defaults
username = 'user1'
Expand Down
14 changes: 6 additions & 8 deletions flask_user/tests/tst_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ def create_app(test_config=None): # For automated tests
try: app.config.from_object('local_settings')
except: pass

# Over-write app config # For automated tests
# Load optional test_config # For automated tests
if test_config:
for key, value in test_config.items():
app.config[key] = value
app.config.update(test_config)

# Setup Flask-Mail, Flask-Babel and Flask-SQLAlchemy
app.mail = Mail(app)
app.babel = babel = Babel(app)
app.db = db = SQLAlchemy(app)
# Initialize Flask extensions
babel = Babel(app) # Initialize Flask-Babel
db = SQLAlchemy(app) # Initialize Flask-SQLAlchemy
mail = Mail(app) # Initialize Flask-Mail

@babel.localeselector
def get_locale():
Expand Down Expand Up @@ -74,7 +73,6 @@ class User(db.Model, UserMixin):
# Relationships
roles = db.relationship('Role', secondary=user_roles,
backref=db.backref('users', lazy='dynamic'))
app.User = User

# Reset all the database tables
db.create_all()
Expand Down
3 changes: 2 additions & 1 deletion flask_user/tests/tstutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class TstClient(object):
"""
Utility class for tests
"""
def __init__(self, client):
def __init__(self, client, db):
self.client = client
self.db = db

def get_valid_page(self, url):
"""
Expand Down
Loading

0 comments on commit 0ff337b

Please sign in to comment.