Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added python3 support #14

Merged
merged 7 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.egg-info
dist
cover
.coverage
.coverage
*.pyc
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: python
python: "2.7"
python:
- "2.7"
- "3.5"
install:
- pip install -r requirements.txt --allow-external twill --allow-unverified twill
- pip install coverage nose lxml requests cssselect
script: nosetests --with-coverage --cover-package=flask_permissions
- pip install -r requirements.txt
- pip install -r requirements_test.txt
script: nosetests --with-coverage --cover-package=flask_permissions
13 changes: 7 additions & 6 deletions flask_permissions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,21 @@ def _id_column_name(self):
# the list of the class's columns (with attributes like
# 'primary_key', etc.) is accessible in different places
# before and after table definition.
if self.__tablename__ in self.metadata.tables.keys():
if self.__tablename__ in list(self.metadata.tables.keys()):
# after definition, it's here
columns = self.metadata.tables[self.__tablename__]._columns
else:
# before, it's here
columns = self.__dict__

for k, v in columns.items():
for k, v in list(columns.items()):
if getattr(v, 'primary_key', False):
return k

@declared_attr
def _roles(self):
user_role_table = make_user_role_table(self.__tablename__, self._id_column_name)
user_role_table = make_user_role_table(self.__tablename__,
self._id_column_name.fget(self))
return db.relationship(
'Role', secondary=user_role_table, backref='users')

Expand All @@ -138,7 +139,7 @@ def roles(self):
def __init__(self, roles=None, default_role='user'):
# If only a string is passed for roles, convert it to a list containing
# that string
if roles and isinstance(roles, basestring):
if roles and isinstance(roles, str):
roles = [roles]

# If a sequence is passed for roles (or if roles has been converted to
Expand Down Expand Up @@ -167,7 +168,7 @@ def remove_roles(self, *roles):
self.roles = [role for role in self.roles if role not in roles]

def get_id(self):
return unicode(getattr(self, self._id_column_name))
return str(getattr(self, self._id_column_name))

def __repr__(self):
return '<{} {}>'.format(self.__tablename__.capitalize(), self.get_id())
return '<{} {}>'.format(self.__tablename__.capitalize(), self.get_id())
25 changes: 18 additions & 7 deletions flask_permissions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
db_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'app.db')

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

Expand Down Expand Up @@ -83,7 +84,7 @@ def test_user_get_id(self):
user = User()
db.session.add(user)
db.session.commit()
self.assertEqual(user.get_id(), unicode(1))
self.assertEqual(user.get_id(), str(1))

def test_user_repr(self):
user = User()
Expand Down Expand Up @@ -126,7 +127,8 @@ def test_add_roles_with_nonexisting_roles(self):
db.session.add(user)
db.session.commit()
test_user = User.query.get(1)
self.assertItemsEqual(new_roles, test_user.roles)
# self.assertItemsEqual(new_roles, test_user.roles)
self.assertEqual(sorted(new_roles), sorted(test_user.roles))

def test_add_roles_with_existing_roles(self):
user = User(default_role=None)
Expand All @@ -139,7 +141,8 @@ def test_add_roles_with_existing_roles(self):
db.session.add(user)
db.session.commit()
test_user = User.query.get(1)
self.assertItemsEqual(new_roles, test_user.roles)
# self.assertItemsEqual(new_roles, test_user.roles)
self.assertEqual(sorted(new_roles), sorted(test_user.roles))

def test_remove_roles(self):
user = User()
Expand All @@ -152,7 +155,8 @@ def test_remove_roles(self):
db.session.add(user)
db.session.commit()
test_user = User.query.get(1)
self.assertItemsEqual(test_user.roles, ['superadmin'])
# self.assertItemsEqual(test_user.roles, ['superadmin'])
self.assertEqual(sorted(test_user.roles), sorted(['superadmin']))

def test_add_abilities_with_nonexisting_abilities(self):
role = Role('admin')
Expand All @@ -162,7 +166,8 @@ def test_add_abilities_with_nonexisting_abilities(self):
db.session.commit()
test_role = Role.query.get(1)
abilities = [ability.name for ability in test_role.abilities]
self.assertItemsEqual(new_abilities, abilities)
# self.assertItemsEqual(new_abilities, abilities)
self.assertEqual(sorted(new_abilities), sorted(abilities))

def test_add_abilities_with_existing_abilities(self):
role = Role('admin')
Expand All @@ -176,7 +181,8 @@ def test_add_abilities_with_existing_abilities(self):
db.session.commit()
test_role = Role.query.get(1)
abilities = [ability.name for ability in test_role.abilities]
self.assertItemsEqual(new_abilities, abilities)
# self.assertItemsEqual(new_abilities, abilities)
self.assertEqual(sorted(new_abilities), sorted(abilities))

def test_remove_abilities(self):
role = Role('admin')
Expand All @@ -190,7 +196,8 @@ def test_remove_abilities(self):
db.session.commit()
test_role = Role.query.get(1)
abilities = [ability.name for ability in test_role.abilities]
self.assertItemsEqual(abilities, ['create_users'])
# self.assertItemsEqual(abilities, ['create_users'])
self.assertEqual(sorted(abilities), sorted(['create_users']))


class DecoratorsTests(DatabaseTests):
Expand Down Expand Up @@ -271,3 +278,7 @@ def test_is_tuple_a_sequence(self):
def test_is_dict_a_sequence(self):
dict_var = {'a': 1, 'b': 2, 'c': 3}
self.assertTrue(is_sequence(dict_var))


if __name__ == '__main__': # optional, but makes import and reuse easier
unittest.main()
6 changes: 3 additions & 3 deletions flask_permissions/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def is_sequence(arg):
return (not hasattr(arg, "strip") and
hasattr(arg, "__getitem__") or
hasattr(arg, "__iter__"))
if hasattr(arg, "strip"):
return False
return (hasattr(arg, "__getitem__") or hasattr(arg, "__iter__"))
14 changes: 9 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Flask==0.10.1
Flask-Testing==0.4
Flask-SQLAlchemy==1.0
Werkzeug==0.9.4
wsgiref==0.1.2
click==6.7
Flask==0.12.1
Flask-SQLAlchemy==2.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
SQLAlchemy==1.1.9
Werkzeug==0.12.1

3 changes: 3 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage==4.4
Flask-Testing==0.6.2
nose==1.3.7