Skip to content

Commit

Permalink
refactor: got rid of _ziggurat_services on models
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo committed Feb 18, 2018
1 parent da7480e commit 5020496
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 39 deletions.
58 changes: 37 additions & 21 deletions ziggurat_foundations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@

__version__ = {'major': 0, 'minor': 7, 'patch': 3}


class ModelProxy(object):
pass


class NOOP(object):
def __nonzero__(self):
return False

# py3 compat

__bool__ = __nonzero__


noop = NOOP()
from ziggurat_foundations.utils import ModelProxy, noop
from ziggurat_foundations.models.services.user import UserService
from ziggurat_foundations.models.services.group import GroupService
from ziggurat_foundations.models.services.group_permission import \
GroupPermissionService
from ziggurat_foundations.models.services.user_permission import \
UserPermissionService
from ziggurat_foundations.models.services.user_resource_permission import \
UserResourcePermissionService
from ziggurat_foundations.models.services.group_resource_permission import \
GroupResourcePermissionService
from ziggurat_foundations.models.services.resource import ResourceService
from ziggurat_foundations.models.services.resource_tree import \
ResourceTreeService
from ziggurat_foundations.models.services.external_identity import \
ExternalIdentityService

model_service_mapping = {
'User': [UserService],
'Group': [GroupService],
'GroupPermission': [GroupPermissionService],
'UserPermission': [UserPermissionService],
'UserResourcePermission': [
UserResourcePermissionService],
'GroupResourcePermission': [
GroupResourcePermissionService],
'Resource': [ResourceService, ResourceTreeService],
'ExternalIdentity': [ExternalIdentityService]
}


def make_passwordmanager(schemes=None):
Expand Down Expand Up @@ -52,20 +66,22 @@ def ziggurat_model_init(*args, **kwargs):
"""
models = ModelProxy()
for cls2 in args:
setattr(models, cls2.__name__, cls2)
models[cls2.__name__] = cls2

for cls in args:
if cls.__name__ == 'User':
if kwargs.get('passwordmanager'):
cls.passwordmanager = kwargs['passwordmanager']
else:
cls.passwordmanager = make_passwordmanager(kwargs.get('passwordmanager_schemes'))
cls.passwordmanager = make_passwordmanager(
kwargs.get('passwordmanager_schemes'))

for cls2 in args:
setattr(models, cls2.__name__, cls2)

setattr(cls, "_ziggurat_models", models)
# if model has a manager attached attached the class also to manager
if hasattr(cls, '_ziggurat_services'):
for service in cls._ziggurat_services:
setattr(service, 'model', cls)
setattr(service, 'models_proxy', models)
services = model_service_mapping.get(cls.__name__, [])
for service in services:
setattr(service, 'model', cls)
setattr(service, 'models_proxy', models)
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/external_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class ExternalIdentityMixin(BaseModel):
{'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8',
})

_ziggurat_services = [ExternalIdentityService]

@declared_attr
def __tablename__(self):
return 'external_identities'
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
class GroupMixin(BaseModel):
""" Mixin for Group model """

_ziggurat_services = [GroupService]

__table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}

@declared_attr
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/group_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class GroupPermissionMixin(BaseModel):
name='pk_groups_permissions'),
{'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'})

_ziggurat_services = [GroupPermissionService]

@declared_attr
def __tablename__(self):
return 'groups_permissions'
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/group_resource_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class GroupResourcePermissionMixin(BaseModel):
name='pk_users_resources_permissions '),
{'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'})

_ziggurat_services = [GroupResourcePermissionService]

@declared_attr
def __tablename__(self):
return 'groups_resources_permissions'
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class ResourceMixin(BaseModel):

__possible_permissions__ = ()

_ziggurat_services = [ResourceService, ResourceTreeService]

@declared_attr
def __tablename__(self):
return 'resources'
Expand Down
2 changes: 1 addition & 1 deletion ziggurat_foundations/models/services/resource_tree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from ziggurat_foundations import noop
from ziggurat_foundations.utils import noop

__all__ = ['ResourceTreeService']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import sqlalchemy as sa

from ziggurat_foundations import noop
from ziggurat_foundations.utils import noop
from ziggurat_foundations.exc import (
ZigguratResourceTreeMissingException,
ZigguratResourceTreePathException,
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class UserMixin(BaseModel):
__table_args__ = {'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'}

_ziggurat_services = [UserService]

@declared_attr
def __tablename__(self):
return 'users'
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/user_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class UserPermissionMixin(BaseModel):
name='pk_users_permissions'),
{'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'})

_ziggurat_services = [UserPermissionService]

@declared_attr
def __tablename__(self):
return 'users_permissions'
Expand Down
2 changes: 0 additions & 2 deletions ziggurat_foundations/models/user_resource_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class UserResourcePermissionMixin(BaseModel):
name='pk_users_resources_permissions '),
{'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'})

_ziggurat_services = [UserResourcePermissionService]

@declared_attr
def __tablename__(self):
return 'users_resources_permissions'
Expand Down
28 changes: 28 additions & 0 deletions ziggurat_foundations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,31 @@ def match(self, encoded):

def check(self, encoded, password):
return False


class ModelProxy(dict):
"""
Holds model references used in services
"""

def __setattr__(self, key, value):
self[key] = value

def __getattr__(self, key):
return self[key]


class NOOP(object):
"""
For tree manager
"""

def __nonzero__(self):
return False

# py3 compat

__bool__ = __nonzero__


noop = NOOP()

0 comments on commit 5020496

Please sign in to comment.