Skip to content

Commit

Permalink
Added DBManager() class. Major reorg of UserManager, UserManager__Vie…
Browse files Browse the repository at this point in the history
…ws and all DbAdapters.
  • Loading branch information
lingthio committed Sep 12, 2017
1 parent 4b2076d commit 8e89a5b
Show file tree
Hide file tree
Showing 23 changed files with 382 additions and 591 deletions.
6 changes: 3 additions & 3 deletions example_apps/dynamodb_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ class User(Model, UserMixin):

id = Field(type=int)

# Setup Flask-User
user_manager = UserManager(app, db, User)

# Create all database tables
db.engine.register(User)
print('create_schema()')
db.engine.create_schema()
print('created_schema()')

# Setup Flask-User
user_manager = UserManager(app, db, User)

# The Home page is accessible to anyone
@app.route('/')
def home_page():
Expand Down
6 changes: 0 additions & 6 deletions example_apps/mongodb_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ class User(db.Document, UserMixin):
# roles = ListField(StringField(), required=False, default_empty=True)
roles = db.ListField(db.StringField(), default=[])

# Customize Flask-User to use the provided MongoDbAdapter
class CustomUserManager(UserManager):
def customize(self, app):
from flask_user.db_adapters import MongoDbAdapter
self.db_adapter = MongoDbAdapter(app, db)

# Setup Flask-User and specify the User data-model
user_manager = UserManager(app, db, User)

Expand Down
32 changes: 0 additions & 32 deletions flask_user/db_adapters/db_adapter_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,6 @@ def ifind_first_object(self, ObjectClass, **kwargs):
"""
raise NotImplementedError

def update_object(self, object, **kwargs):
""" Update an existing object, specified by ``object``,
with the fields and values specified in ``**kwargs``.
.. note::
Role management methods.
"""
# Convert name=value kwargs to object.name=value
for key,value in kwargs.items():
if hasattr(object, key):
setattr(object, key, value)
else:
raise KeyError("Object '%s' has no field '%s'." % (type(object), key))


# Role management methods
# -----------------------

def add_user_role(self, user, role_name, RoleClass=None):
""" Add a ``role_name`` role to ``user``."""
raise NotImplementedError

def get_user_roles(self, user):
"""Retrieve a list of user role names.
.. note::
Database management methods.
"""
raise NotImplementedError


# Database management methods
# ---------------------------
Expand Down
54 changes: 5 additions & 49 deletions flask_user/db_adapters/dynamo_db_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,9 @@ def find_first_object(self, ObjectClass, **kwargs):
# Execute query
return query.first(desc=True)

def ifind_first_object(self, ObjectClass, **kwargs):
"""Retrieve the first object of type ``ObjectClass``,
matching the filters specified in ``**kwargs`` -- case insensitive.
"""

# Convert each name/value pair in 'kwargs' into a filter
print("dynamo.ifind_first_object(%s, %s). I don't actually support case insensitive yet" % (ObjectClass, str(kwargs)))

query = self.db.engine.query(ObjectClass)
for field_name, field_value in kwargs.items():

# Make sure that ObjectClass has a 'field_name' property
field = getattr(ObjectClass, field_name, None)
if field is None:
raise KeyError("DynamoDBAdapter.ifind_first_object(): Class '%s' has no field '%s'." % (ObjectClass, field_name))

# Add a case sensitive filter to the query
query = query.filter(field == field_value)

# Execute query
return query.first(desc=True)

def update_object(self, object, **kwargs):
""" Update an existing object, specified by ``object``,
with the fields and values specified in ``**kwargs``.
"""
# Convert name=value kwargs to object.name=value
super(DynamoDbAdapter, self).update_object(object, **kwargs)

def save_object(self, object, **kwargs):
""" Save object. Only for non-session centric Object-Database Mappers."""
pass

def delete_object(self, object):
""" Delete object specified by ``object``. """
Expand All @@ -147,34 +121,16 @@ def commit(self):
# self.db.session.commit()


# Role management methods
# -----------------------

def add_user_role(self, user, role_name, RoleClass=None):
""" Add a ``role_name`` role to ``user``."""
# MongoEngine has a bug where
# user.roles.append(role_name)
# appends role_name to Field.default (instead of user.roles)
# As a workaround, we need to create a new list
user.roles.append(role_name)
user.save()

def get_user_roles(self, user):
""" Retrieve a list of user role names.
"""
return user.roles


# Database management methods
# ---------------------------

def create_all_tables(self):
"""This method does nothing for DynamoDbAdapter."""
pass # TODO
self.db.engine.create_schema()

def drop_all_tables(self):
"""Drop all document collections of the database.
.. warning:: ALL DATA WILL BE LOST. Use only for automated testing.
"""
pass # TODO
self.db.engine.delete_schema()
44 changes: 6 additions & 38 deletions flask_user/db_adapters/mongo_db_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def add_object(self, ObjectClass, **kwargs):
object.save()
return object

def add_object2(self, object):
"""Add object to db session. Only for session-centric object-database mappers."""
object.save()

def get_object(self, ObjectClass, id):
""" Retrieve object of type ``ObjectClass`` by ``id``.
Expand Down Expand Up @@ -73,28 +77,10 @@ def find_first_object(self, ObjectClass, **kwargs):
# Retrieve first object
return ObjectClass.objects(**kwargs).first()

def ifind_first_object(self, ObjectClass, **kwargs):
"""Retrieve the first object of type ``ObjectClass``,
matching the filters specified in ``**kwargs`` -- case insensitive.
"""

# convert kwarg name=value to name__iexact=value
ikwargs = {}
for key,value in kwargs.items():
ikwargs[key+'__iexact']=value

# Retrieve first object
return ObjectClass.objects(**ikwargs).first()

def update_object(self, object, **kwargs):
""" Update an existing object, specified by ``object``,
with the fields and values specified in ``**kwargs``.
"""
# Convert name=value kwargs to object.name=value
super(MongoDbAdapter, self).update_object(object, **kwargs)
def save_object(self, object, **kwargs):
""" Save object. Only for non-session centric Object-Database Mappers."""
object.save()


def delete_object(self, object):
""" Delete object specified by ``object``. """
object.delete()
Expand All @@ -105,24 +91,6 @@ def commit(self):
pass


# Role management methods
# -----------------------

def add_user_role(self, user, role_name, RoleClass=None):
""" Add a ``role_name`` role to ``user``."""
# MongoEngine has a bug where
# user.roles.append(role_name)
# appends role_name to Field.default (instead of user.roles)
# As a workaround, we need to create a new list
user.roles.append(role_name)
user.save()

def get_user_roles(self, user):
""" Retrieve a list of user role names.
"""
return user.roles


# Database management methods
# ---------------------------

Expand Down
33 changes: 9 additions & 24 deletions flask_user/db_adapters/sql_db_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def add_object(self, ObjectClass, **kwargs):
self.db.session.add(object)
return object

def add_object2(self, object):
"""Add object to db session. Only for session-centric object-database mappers."""
self.db.session.add(object)
pass

def get_object(self, ObjectClass, id):
""" Retrieve object of type ``ObjectClass`` by ``id``.
Expand Down Expand Up @@ -114,15 +119,12 @@ def ifind_first_object(self, ObjectClass, **kwargs):
# Execute query
return query.first()

def update_object(self, object, **kwargs):
""" Update an existing object, specified by ``object``,
with the fields and values specified in ``**kwargs``.
"""
# Convert name=value kwargs to object.name=value
super(SQLDbAdapter, self).update_object(object, **kwargs)
def save_object(self, object):
""" Save object. Only for non-session centric Object-Database Mappers."""
pass

def delete_object(self, object):
""" Delete object specified by ``object``."""
""" Delete object."""
self.db.session.delete(object)

def commit(self):
Expand All @@ -131,23 +133,6 @@ def commit(self):
self.db.session.commit()


# Role management methods
# -----------------------

def add_user_role(self, user, role_name, RoleClass=None):
""" Add a ``role_name`` role to ``user``."""
# Get or add role
role = self.find_first_object(RoleClass, name=role_name)
if not role:
role = self.add_object(RoleClass, name=role_name)
user.roles.append(role)

def get_user_roles(self, user):
""" Retrieve a list of user role names.
"""
return [role.name for role in user.roles]


# Database management methods
# ---------------------------

Expand Down
Loading

0 comments on commit 8e89a5b

Please sign in to comment.