Skip to content

Commit

Permalink
Upgrade Pyramid >= 2.0 and AnyBlok >= 1.1.0 (#36)
Browse files Browse the repository at this point in the history
* Updated travis configuration + fixed version

* Fixed some import

* Fixed some warnings
  • Loading branch information
jssuzanne committed Apr 15, 2021
1 parent cd346a4 commit 1f2bdd1
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 43 deletions.
17 changes: 9 additions & 8 deletions .travis.yml
Expand Up @@ -6,12 +6,13 @@ python:
- "3.6"
- "3.7"
- "3.8"
- "3.9-dev"
- "3.9"
- "3.10-dev"
- "nightly"

matrix:
allow_failures:
- python: "3.9-dev"
- python: "3.10-dev"
- python: "nightly"

virtualenv:
Expand All @@ -34,18 +35,18 @@ env:
script:
- flake8 anyblok_pyramid
- psql -c 'create database travis_ci_test;' -U postgres
- pytest --cov-report= --cov=anyblok_pyramid anyblok_pyramid/tests
- pytest --cov=anyblok_pyramid anyblok_pyramid/tests
- psql -c 'drop database travis_ci_test;' -U postgres
- anyblok_createdb --install-bloks auth
- pytest --cov-report= --cov=anyblok_pyramid anyblok_pyramid/bloks/auth
- pytest --cov-append --cov=anyblok_pyramid anyblok_pyramid/bloks/auth
- psql -c 'drop database travis_ci_test;' -U postgres
- anyblok_createdb --install-bloks auth-password
- pytest --cov-report= --cov=anyblok_pyramid anyblok_pyramid/bloks/password
- pytest --cov-append --cov=anyblok_pyramid anyblok_pyramid/bloks/password
- psql -c 'drop database travis_ci_test;' -U postgres
- anyblok_createdb --install-bloks authorization
- pytest --cov-report= --cov=anyblok_pyramid anyblok_pyramid/bloks/authorization
- pytest --cov-append --cov=anyblok_pyramid anyblok_pyramid/bloks/authorization
- psql -c 'drop database travis_ci_test;' -U postgres
- anyblok_createdb --install-bloks user-identity
- pytest --cov-report= --cov=anyblok_pyramid anyblok_pyramid/bloks/user_identity
- pytest --cov-append --cov=anyblok_pyramid anyblok_pyramid/bloks/user_identity

after_success: coveralls --data_file=.coverage --config_file=.coveragerc
after_success: coveralls
7 changes: 4 additions & 3 deletions anyblok_pyramid/anyblok.py
Expand Up @@ -183,9 +183,10 @@ def after_bulk_delete(self, session, query, query_context, result):
mark_changed(session, self.transaction_manager, self.keep_session)

def before_commit(self, session):
assert (session.transaction.nested or # noqa
self.transaction_manager.get().status == ZopeStatus.COMMITTING,
"Transaction must be committed using the transaction manager")
assert (
session.transaction.nested or # noqa
self.transaction_manager.get().status == ZopeStatus.COMMITTING
), "Transaction must be committed using the transaction manager"


def register(session, initial_state=STATUS_ACTIVE,
Expand Down
14 changes: 7 additions & 7 deletions anyblok_pyramid/bloks/auth/user.py
Expand Up @@ -9,7 +9,7 @@
from pyramid.httpexceptions import HTTPUnauthorized
from anyblok import Declarations
from anyblok.column import String
from pyramid.security import Allow, ALL_PERMISSIONS
from pyramid.authorization import Allow, ALL_PERMISSIONS


@Declarations.register(Declarations.Model)
Expand All @@ -22,7 +22,7 @@ def get_roles(cls, login):
:param login: str, login attribute of the user
:rtype: list of str (name of the roles)
"""
return cls.registry.Pyramid.User.get_roles(login)
return cls.anyblok.Pyramid.User.get_roles(login)

@classmethod
def get_acl(cls, login, resource, params=None):
Expand All @@ -38,7 +38,7 @@ def get_acl(cls, login, resource, params=None):
:param resource: str, name of a resource
:param params: all options need to compute ACL
"""
return cls.registry.Pyramid.User.get_acl(
return cls.anyblok.Pyramid.User.get_acl(
login, resource, params=params)

@classmethod
Expand All @@ -53,7 +53,7 @@ def check_acl(cls, login, resource, type_):
:param type: str, name of the action
:param params: all options need to compute ACL
"""
return cls.registry.Pyramid.User.check_acl(
return cls.anyblok.Pyramid.User.check_acl(
login, resource, type_)

@classmethod
Expand All @@ -70,11 +70,11 @@ def check_login(cls, **kwargs):
:param kwargs: any options need to validate credential
"""
return cls.registry.Pyramid.User.check_login(**kwargs)
return cls.anyblok.Pyramid.User.check_login(**kwargs)

@classmethod
def check_user_exists(cls, login):
user = cls.registry.Pyramid.User.query().get(login)
user = cls.anyblok.Pyramid.User.query().get(login)
if user is None:
raise KeyError(f"{login} is not a valid login")

Expand All @@ -88,7 +88,7 @@ def get_user(cls, user_id):
invalidation in case of user modification that could impact
restricted query by user id
"""
return cls.registry.Pyramid.User.query().get(user_id)
return cls.anyblok.Pyramid.User.query().get(user_id)


@Declarations.register(Declarations.Model.Pyramid)
Expand Down
20 changes: 10 additions & 10 deletions anyblok_pyramid/bloks/authorization/authorization.py
Expand Up @@ -11,7 +11,7 @@
from anyblok.relationship import Many2One
from anyblok.field import JsonRelated
from .exceptions import AuthorizationValidationException
from pyramid.security import Allow, Deny, ALL_PERMISSIONS
from pyramid.authorization import Allow, Deny, ALL_PERMISSIONS
from sqlalchemy import or_


Expand Down Expand Up @@ -52,8 +52,8 @@ class Authorization:
def get_acl_filter_model(cls):
"""Return the Model to use to check the permission"""
return {
'User': cls.registry.Pyramid.User,
'Role': cls.registry.Pyramid.Role,
'User': cls.anyblok.Pyramid.User,
'Role': cls.anyblok.Pyramid.Role,
}

@classmethod
Expand All @@ -64,8 +64,8 @@ def get_acl(cls, login, resource, params=None):
:param resource: str, name of the resource
"""
# cache the method
User = cls.registry.Pyramid.User
Role = cls.registry.Pyramid.Role
User = cls.anyblok.Pyramid.User
Role = cls.anyblok.Pyramid.Role

query = cls.query()
query = query.filter(
Expand Down Expand Up @@ -123,8 +123,8 @@ def check_acl(cls, login, resource, type_):
:param type: str, name of the action
"""
# cache the method
User = cls.registry.Pyramid.User
Role = cls.registry.Pyramid.Role
User = cls.anyblok.Pyramid.User
Role = cls.anyblok.Pyramid.Role

query = cls.query()
query = query.filter(
Expand Down Expand Up @@ -206,14 +206,14 @@ def ensure_exists(
# pv: at some point adding index on this criteria may boost things
# while setting authorizations
authz = (
cls.registry.Pyramid.Authorization.query()
cls.anyblok.Pyramid.Authorization.query()
.filter_by(
code=code,
)
.one_or_none()
)
if not authz:
authz = cls.registry.Pyramid.Authorization.insert(
authz = cls.anyblok.Pyramid.Authorization.insert(
code=code, **kwargs
)
else:
Expand Down Expand Up @@ -274,7 +274,7 @@ def ensure_exists(
default: capitalized name.
:return: Created or updated role
"""
Pyramid = cls.registry.Pyramid
Pyramid = cls.anyblok.Pyramid
role = Pyramid.Role.query().get(name)
if not role:
if not label:
Expand Down
2 changes: 1 addition & 1 deletion anyblok_pyramid/bloks/authorization/query.py
Expand Up @@ -63,7 +63,7 @@ def get_value_for_relationship(query, entry, keys):
return query, res

if hasattr(entry, '__registry_name__'):
Model = query.registry.get(entry.__registry_name__)
Model = query.anyblok.get(entry.__registry_name__)
if entry is Model:
query = query.join(res)

Expand Down
2 changes: 1 addition & 1 deletion anyblok_pyramid/bloks/authorization/tests/test_get_acl.py
Expand Up @@ -6,7 +6,7 @@
# v. 2.0. If a copy of the MPL was not distributed with this file,You can
# obtain one at http://mozilla.org/MPL/2.0/.
import pytest
from pyramid.security import Allow, Deny, ALL_PERMISSIONS
from pyramid.authorization import Allow, Deny, ALL_PERMISSIONS


@pytest.mark.usefixtures('rollback_registry')
Expand Down
2 changes: 1 addition & 1 deletion anyblok_pyramid/bloks/password/user.py
Expand Up @@ -34,7 +34,7 @@ def check_login(cls, login=None, password=None, **kwargs):
:param password: str
:exception: HTTPUnauthorized
"""
Credential = cls.registry.Pyramid.CredentialStore
Credential = cls.anyblok.Pyramid.CredentialStore
credential = Credential.query().filter(
Credential.login == login
).one_or_none()
Expand Down
6 changes: 3 additions & 3 deletions anyblok_pyramid/bloks/pyramid/model.py
Expand Up @@ -8,7 +8,7 @@
# obtain one at http://mozilla.org/MPL/2.0/.
from pyramid.httpexceptions import HTTPUnauthorized
from anyblok import Declarations
from pyramid.security import Allow, ALL_PERMISSIONS
from pyramid.authorization import Allow, ALL_PERMISSIONS


@Declarations.register(Declarations.Model)
Expand Down Expand Up @@ -125,12 +125,12 @@ def restrict_query_by_user(
have to manage or mind to cache invalidation while using this
method.
"""
for method in cls.registry.restrict_query_by_user_methods.get(
for method in cls.anyblok.restrict_query_by_user_methods.get(
query.Model, []
):

query = getattr(
cls.registry.get(query.Model), method
cls.anyblok.get(query.Model), method
)(query, cls.get_user(user_code))

return query
2 changes: 1 addition & 1 deletion anyblok_pyramid/security.py
Expand Up @@ -6,7 +6,7 @@
# v. 2.0. If a copy of the MPL was not distributed with this file,You can
# obtain one at http://mozilla.org/MPL/2.0/.
from pyramid.httpexceptions import HTTPUnauthorized
from pyramid.security import Deny, Everyone, ALL_PERMISSIONS
from pyramid.authorization import Deny, Everyone, ALL_PERMISSIONS


def group_finder(userid, request):
Expand Down
8 changes: 4 additions & 4 deletions anyblok_pyramid/test_bloks/test2/__init__.py
Expand Up @@ -64,7 +64,7 @@ def pyramid_load_config(cls, config):

def update(self, latest):
if not latest:
self.registry.Pyramid.User.insert(login="admin")
self.registry.Pyramid.User.insert(login="viewer")
self.registry.Pyramid.User.insert(login="user@anyblok.org")
self.registry.Pyramid.User.insert(login="user2@anyblok.org")
self.anyblok.Pyramid.User.insert(login="admin")
self.anyblok.Pyramid.User.insert(login="viewer")
self.anyblok.Pyramid.User.insert(login="user@anyblok.org")
self.anyblok.Pyramid.User.insert(login="user2@anyblok.org")
4 changes: 2 additions & 2 deletions anyblok_pyramid/test_bloks/test2/models.py
Expand Up @@ -7,7 +7,7 @@
# v. 2.0. If a copy of the MPL was not distributed with this file,You can
# obtain one at http://mozilla.org/MPL/2.0/.
from anyblok import Declarations
from pyramid.security import Allow, Authenticated
from pyramid.authorization import Allow, Authenticated
from anyblok_pyramid.bloks.pyramid.restrict import restrict_query_by_user


Expand All @@ -16,7 +16,7 @@ class User:

@classmethod
def check_login(cls, login=None, password=None):
return cls.registry.Pyramid.User.query().get(login)
return cls.anyblok.Pyramid.User.query().get(login)

@classmethod
def get_acl(cls, login, resource, **params):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -12,8 +12,8 @@
version = '1.2.0'

requires = [
'anyblok>=0.9.0',
'pyramid',
'anyblok>=1.1.0',
'pyramid>=2.0.0',
'pyramid_tm',
'zope.sqlalchemy',
'passlib',
Expand Down

0 comments on commit 1f2bdd1

Please sign in to comment.