Skip to content

Commit c7f0c47

Browse files
authored
Merge pull request #235 from SlivTime/remove-unused
Fixes for current sqlalchemy and pip
2 parents 633b28f + 1532edf commit c7f0c47

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
venv-%:
33
test -d venv-${*} || virtualenv -p ${*} venv-${*}
4-
venv-${*}/bin/pip install -e .[tests,web,fcgi,sqla,memcached,cleanhtml,renderhtml,images] --process-dependency-links
4+
venv-${*}/bin/pip install -e .[tests,web,fcgi,sqla,memcached,cleanhtml,renderhtml,images]
55
touch venv-${*}/bin/activate
66

77
devbuild-%: venv-%
@@ -12,16 +12,18 @@ devbuild-%: venv-%
1212

1313
test-%: name ?= tests
1414
test-%: devbuild-%
15-
venv-${*}/bin/py.test $(name) -q -r fEsxXw --strict
15+
venv-${*}/bin/python -m pytest $(name) -q -r fEsxXw --strict
1616

1717
coverage-%: devbuild-%
18-
venv-${*}/bin/py.test tests --cov=venv-${*}/lib/${*}/site-packages/iktomi | sed -e "s/^venv-${*}\\/lib\\/${*}\\/site-packages\\///"
18+
venv-${*}/bin/python -m pytest tests --cov=venv-${*}/lib/${*}/site-packages/iktomi | sed -e "s/^venv-${*}\\/lib\\/${*}\\/site-packages\\///"
1919

2020
test2: test-python2.7
2121
test3: test-python3.5
2222

2323
test: test2 test3
2424
coverage: coverage-python2.7
2525

26+
clean:
27+
rm -rf venv-python2.7 venv-python3.5
2628
#docs-%: devbuild
2729
# cd doc && make SPHINXBUILD=../venv/bin/sphinx-build $*

iktomi/unstable/db/sqla/public_query.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
import sqlalchemy
12
from sqlalchemy.orm.query import Query
23
from sqlalchemy.orm.util import AliasedClass
34
from sqlalchemy.sql import ClauseElement, Join
45
from sqlalchemy.sql.selectable import FromGrouping
56
from sqlalchemy import cast, Boolean
67
from sqlalchemy.orm.util import _class_to_mapper
8+
from sqlalchemy import event
9+
from sqlalchemy import inspect
10+
from packaging.version import parse as version
11+
12+
13+
def init_filter_event(prop_name):
14+
@event.listens_for(Query, "before_compile", retval=True)
15+
def before_compile(query):
16+
"""A query compilation rule that will add limiting criteria for every
17+
subclass of HasPrivate"""
18+
if query._execution_options.get("include_private", False):
19+
return query
20+
21+
for ent in query.column_descriptions:
22+
entity = ent['entity']
23+
if entity is None:
24+
continue
25+
insp = inspect(ent['entity'])
26+
mapper = getattr(insp, 'mapper', None)
27+
if mapper and prop_name in dir(mapper.class_):
28+
query = query.enable_assertions(False).filter(
29+
ent['entity'].public == True)
30+
31+
return query
32+
33+
return before_compile
734

835

936
class PublicQuery(Query):
@@ -17,10 +44,29 @@ class PublicQuery(Query):
1744
1845
A version from recipe combined with our own vision
1946
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/PreFilteredQuery
47+
48+
UPD in v0.6
49+
Old recipe is outdated, we should use new one
50+
https://github.com/sqlalchemy/sqlalchemy/wiki/FilteredQuery
51+
52+
Old functionality is untouched for compatibility
2053
'''
2154

2255
property_name = 'public'
2356

57+
# Sqlalchemy version should be lower than this version to be able
58+
# to handle all cases correctly
59+
# For newer versions we will use more modern and simpler solution
60+
# that cannot filter eager loading via joinedjoad
61+
max_sqla_version = '1.2'
62+
63+
def __init__(self, *args, **kwargs):
64+
if version(sqlalchemy.__version__) >= version(self.max_sqla_version):
65+
init_filter_event(self.property_name)
66+
super(PublicQuery, self).__init__(*args, **kwargs)
67+
68+
69+
2470
def get(self, ident):
2571
prop = self.property_name
2672
if self._criterion: # pragma: no cover

iktomi/unstable/db/sqla/replication.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ def replicate_no_merge(source, model, cache=None):
191191
elif source in cache:
192192
return cache[source]
193193
db = object_session(source)
194-
cls, ident = identity_key(instance=source)
194+
ident = identity_key(instance=source)[1]
195+
assert ident is not None
195196
target = db.query(model).get(ident)
196197
if target is None:
197198
target = model()

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
extras_requires = {
88
'web': ['webob'],
99
'fcgi': ['flup6'],
10-
'sqla': ['sqlalchemy'],
10+
'sqla': ['sqlalchemy', 'packaging'],
1111
'memcached': ['python-memcached'],
1212
'cleanhtml': ['lxml'],
1313
'renderhtml': ['jinja2'],
@@ -16,10 +16,10 @@
1616

1717
tests_requires = [
1818
'pymysql',
19-
'testalchemy==0.4',
19+
'testalchemy @ https://github.com/ods/testalchemy/tarball/master#egg=testalchemy-0.4',
2020
'pytest',
2121
'pytest-cov',
22-
'mockcache==1.0.3_alpha',
22+
'mockcache @ https://github.com/lunant/mockcache/tarball/master#egg=mockcache-1.0.3_alpha',
2323
'webtest',
2424
]
2525
if sys.version_info[0] < 3:
@@ -34,7 +34,7 @@
3434

3535
setup(
3636
name='iktomi',
37-
version='0.5.2',
37+
version='0.6',
3838
packages=['iktomi',
3939
'iktomi.utils',
4040
'iktomi.forms',

0 commit comments

Comments
 (0)