Skip to content

Commit

Permalink
exceptions: introduce ZigguratException class
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo committed Oct 29, 2016
1 parent 780f115 commit bdbf026
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 27 deletions.
4 changes: 4 additions & 0 deletions ziggurat_foundations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
__version__ = {'major': 0, 'minor': 6, 'patch': 8}


class ZigguratException(object):
pass


class ModelProxy(object):
pass

Expand Down
3 changes: 2 additions & 1 deletion ziggurat_foundations/ext/pyramid/get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import importlib

from ziggurat_foundations import ZigguratException
from ziggurat_foundations.models.base import get_db_session

CONFIG_KEY = 'ziggurat_foundations'
Expand All @@ -16,7 +17,7 @@ def includeme(config):
'%s.session_provider_callable' % CONFIG_KEY)

if not user_model_location:
raise Exception('''You need to pass location of user model
raise ZigguratException('''You need to pass location of user model
inside your application eg.:
ziggurat_foundations.user_model_location = youappname.models:User
''')
Expand Down
4 changes: 3 additions & 1 deletion ziggurat_foundations/ext/pyramid/sign_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
import pyramid.security


from ziggurat_foundations import ZigguratException
from ziggurat_foundations.models.base import get_db_session

CONFIG_KEY = 'ziggurat_foundations'
Expand Down Expand Up @@ -54,7 +56,7 @@ def includeme(config):
CONFIG_KEY, 'password')

if not user_model_location:
raise Exception('''You need to pass location of user model
raise ZigguratException('''You need to pass location of user model
inside your application eg.:
ziggurat_foundations.user_model_location = youappname.models:User
''')
Expand Down
8 changes: 7 additions & 1 deletion ziggurat_foundations/models/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from __future__ import unicode_literals
import sqlalchemy as sa

from ziggurat_foundations import ZigguratException


class ZigguratSessionException(ZigguratException):
pass


class BaseModel(object):
""" Basic class that all other classes inherit from that supplies some
Expand Down Expand Up @@ -150,4 +156,4 @@ def get_db_session(session=None, obj=None):
# try global pylons-like session then
elif models.DBSession:
return models.DBSession
raise Exception('No Session found')
raise ZigguratSessionException('No Session found')
7 changes: 4 additions & 3 deletions ziggurat_foundations/models/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from sqlalchemy.ext.declarative import declared_attr
from zope.deprecation import deprecation

from .base import BaseModel
from .services.resource import ResourceService
from ziggurat_foundations import ZigguratException
from ziggurat_foundations.models.base import BaseModel
from ziggurat_foundations.models.services.resource import ResourceService
from .base import get_db_session


Expand Down Expand Up @@ -95,7 +96,7 @@ def __repr__(self):

@property
def __acl__(self):
raise Exception("The model should have implemented __acl__")
raise ZigguratException("Model should implement __acl__")

@sa.orm.validates('user_permissions', 'group_permissions')
def validate_permission(self, key, permission):
Expand Down
34 changes: 20 additions & 14 deletions ziggurat_foundations/models/services/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
from collections import OrderedDict

import sqlalchemy as sa
from . import BaseService
from ..base import get_db_session
from ...permissions import (ANY_PERMISSION,
ALL_PERMISSIONS,
PermissionTuple,
resource_permissions_for_users)
from ziggurat_foundations import ZigguratException
from ziggurat_foundations.models.services import BaseService
from ziggurat_foundations.models.base import get_db_session
from ziggurat_foundations.permissions import (
ANY_PERMISSION,
ALL_PERMISSIONS,
PermissionTuple,
resource_permissions_for_users)


class ResourceTreeException(ZigguratException):
pass


class ResourceService(BaseService):
Expand Down Expand Up @@ -203,15 +209,13 @@ def groups_for_perm(cls, instance, perm_name, group_ids=None,
return group_perms

@classmethod
def subtree_deeper(cls, object_id, limit_depth=1000000, flat=True,
db_session=None):
def subtree_deeper(cls, object_id, limit_depth=1000000, db_session=None):
"""
This returns you subree of ordered objects relative
to the start object id currently only postgresql
:param object_id:
:param limit_depth:
:param flat:
:param db_session:
:return:
"""
Expand Down Expand Up @@ -263,15 +267,13 @@ def build_subtree_strut(self, result):
return root_elem

@classmethod
def path_upper(cls, object_id, limit_depth=1000000, flat=True,
db_session=None):
def path_upper(cls, object_id, limit_depth=1000000, db_session=None):
"""
This returns you path to root node starting from object_id
currently only for postgresql
:param object_id:
:param limit_depth:
:param flat:
:param db_session:
:return:
"""
Expand All @@ -291,14 +293,18 @@ def path_upper(cls, object_id, limit_depth=1000000, flat=True,
resource_id=object_id, depth=limit_depth)
return q

def lock_resource_for_update(self, resource_id, db_session):
db_session = get_db_session(db_session)
pass

@classmethod
def move_to_position(cls, resource_id, position, parent_id=None,
def move_to_position(cls, resource_id, to_position, parent_id=None,
db_session=None):
"""
Moves node to new location in the tree
:param resource_id:
:param position:
:param to_position:
:param parent_id: new parent id
:param db_session:
:return:
Expand Down
61 changes: 54 additions & 7 deletions ziggurat_foundations/tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create_default_tree(db_session):
root = add_resource(
db_session, -1, 'root', ordering=1)
res_a = add_resource(
db_session, 1, 'a', parent_id=root.resource_id, ordering=2)
db_session, 1, 'a', parent_id=root.resource_id, ordering=1)
res_aa = add_resource(
db_session, 5, 'aa', parent_id=res_a.resource_id, ordering=1)
res_ab = add_resource(
Expand All @@ -24,11 +24,11 @@ def create_default_tree(db_session):
res_ad = add_resource(
db_session, 8, 'ad', parent_id=res_a.resource_id, ordering=4)
res_b = add_resource(
db_session, 2, 'b', parent_id=root.resource_id, ordering=3)
db_session, 2, 'b', parent_id=root.resource_id, ordering=2)
res_ba = add_resource(
db_session, 4, 'ba', parent_id=res_b.resource_id, ordering=1)
res_c = add_resource(
db_session, 3, 'c', parent_id=root.resource_id, ordering=4)
db_session, 3, 'c', parent_id=root.resource_id, ordering=3)
return root


Expand Down Expand Up @@ -72,7 +72,7 @@ def test_branch_data_with_limit(self, db_session):
assert len(tree_struct['children'][7]['children'].values()) == 0

@pytest.mark.skipif(not_postgres, reason="requires postgres")
def test_going_up(self, db_session):
def test_going_up_hierarchy(self, db_session):
root = create_default_tree(db_session=db_session)
result = ResourceService.path_upper(9, db_session=db_session)
assert [r.resource_id for r in result] == [9, 7, 1, -1]
Expand All @@ -81,11 +81,58 @@ def test_going_up(self, db_session):
def test_move_up_on_same_branch(self, db_session):
import pprint
root = create_default_tree(db_session=db_session)
ResourceService.move_to_position(3, position=2, db_session=db_session)
ResourceService.move_to_position(3, to_position=2, db_session=db_session)
result = ResourceService.subtree_deeper(
root['node'].resource_id, limit_depth=2, db_session=db_session)
root.resource_id, limit_depth=2, db_session=db_session)
tree = ResourceService.build_subtree_strut(result)
pprint.pprint(tree)
assert tree['children'][3]['node'].ordering == 2
assert tree['children'][2]['node'].ordering == 4
assert 1 == 2

@pytest.mark.skipif(not_postgres, reason="requires postgres")
def test_move_down_on_same_branch(self, db_session):
import pprint
root = create_default_tree(db_session=db_session)
ResourceService.move_to_position(1, to_position=3, db_session=db_session)
result = ResourceService.subtree_deeper(
root.resource_id, limit_depth=2, db_session=db_session)
tree = ResourceService.build_subtree_strut(result)
pprint.pprint(tree)
assert tree['children'][2]['node'].ordering == 1
assert tree['children'][3]['node'].ordering == 2
assert tree['children'][1]['node'].ordering == 3

@pytest.mark.skipif(not_postgres, reason="requires postgres")
def test_move_before_1_on_same_branch(self, db_session):
import pprint
root = create_default_tree(db_session=db_session)
ResourceService.move_to_position(3, to_position=0, db_session=db_session)
result = ResourceService.subtree_deeper(
root.resource_id, limit_depth=2, db_session=db_session)
tree = ResourceService.build_subtree_strut(result)
pprint.pprint(tree)
assert tree['children'][3]['node'].ordering == 2
assert tree['children'][2]['node'].ordering == 4

@pytest.mark.skipif(not_postgres, reason="requires postgres")
def test_move_after_last_on_same_branch(self, db_session):
import pprint
root = create_default_tree(db_session=db_session)
ResourceService.move_to_position(3, to_position=4, db_session=db_session)
result = ResourceService.subtree_deeper(
root.resource_id, limit_depth=2, db_session=db_session)
tree = ResourceService.build_subtree_strut(result)
pprint.pprint(tree)
assert tree['children'][3]['node'].ordering == 2
assert tree['children'][2]['node'].ordering == 4

@pytest.mark.skipif(not_postgres, reason="requires postgres")
def test_move_to_same_position(self, db_session):
import pprint
root = create_default_tree(db_session=db_session)
ResourceService.move_to_position(1, to_position=1, db_session=db_session)
result = ResourceService.subtree_deeper(
root.resource_id, limit_depth=2, db_session=db_session)
tree = ResourceService.build_subtree_strut(result)
pprint.pprint(tree)
assert tree['children'][3]['node'].ordering == 1

0 comments on commit bdbf026

Please sign in to comment.