Skip to content

Commit

Permalink
Make column inspection optional
Browse files Browse the repository at this point in the history
  • Loading branch information
meyt committed Oct 23, 2017
1 parent 6556e53 commit 4337530
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
32 changes: 19 additions & 13 deletions sqlalchemy_dict/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,28 @@ def update_from_dict(self, context):
)

@classmethod
def iter_columns(cls, relationships=True, synonyms=True, composites=True, hybrids=True):
mapper = inspect(cls)
for k, c in mapper.all_orm_descriptors.items():
def iter_columns(cls, relationships=True, synonyms=True, composites=True, use_inspection=True, hybrids=True):
if use_inspection:
mapper = inspect(cls)
for k, c in mapper.all_orm_descriptors.items():

if k == '__mapper__':
continue
if k == '__mapper__':
continue

if c.extension_type == ASSOCIATION_PROXY:
continue
if c.extension_type == ASSOCIATION_PROXY:
continue

if (not hybrids and c.extension_type == HYBRID_PROPERTY) \
or (not relationships and k in mapper.relationships) \
or (not synonyms and k in mapper.synonyms) \
or (not composites and k in mapper.composites):
continue
yield getattr(cls, k)
if (not hybrids and c.extension_type == HYBRID_PROPERTY) \
or (not relationships and k in mapper.relationships) \
or (not synonyms and k in mapper.synonyms) \
or (not composites and k in mapper.composites):
continue
yield getattr(cls, k)

else:
# noinspection PyUnresolvedReferences
for c in cls.__table__.c:
yield c

@classmethod
def iter_dict_columns(cls, include_readonly_columns=True, include_protected_columns=False, **kw):
Expand Down
11 changes: 11 additions & 0 deletions sqlalchemy_dict/tests/test_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ def test_iter_columns(self):
self.assertNotIn('password', columns)
self.assertIn('_password', columns)

columns = {c.key: c for c in Member.iter_columns(
relationships=False,
synonyms=False,
composites=False,
use_inspection=False
)}
self.assertEqual(len(columns), 16)
self.assertNotIn('is_visible', columns)
self.assertNotIn('_password', columns)
self.assertIn('password', columns)

def test_iter_dict_columns(self):
columns = {c.key: c for c in Member.iter_dict_columns(
include_readonly_columns=False, include_protected_columns=False)}
Expand Down

0 comments on commit 4337530

Please sign in to comment.