Skip to content

Commit

Permalink
Merge branch '432-gips-orm-setting' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "move GIPS_ORM to gips.settings"

Closes #432

See merge request appliedgeosolutions/gips!489
  • Loading branch information
Ian Cooke committed Jan 24, 2018
2 parents 5d65d00 + 106891f commit 4dd5a21
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 45 deletions.
23 changes: 7 additions & 16 deletions gips/inventory/orm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@

import django

from gips.utils import verbose_out, error_handler
from gips import utils


def use_orm():
"""Check GIPS_ORM to see if the user wants to use the Django ORM.
Use the ORM only if the user sets GIPS_ORM to 'true' (case
insensitive) or any nonzero number.
Defaults to True.
"""
raw_val = os.environ.get('GIPS_ORM', 'true')
use_orm = raw_val.lower() == 'true'
if not use_orm:
try:
use_orm = bool(float(raw_val))
except ValueError:
pass # "anything else" is considered False
return use_orm

return getattr(utils.settings(), 'GIPS_ORM', True)

setup_complete = False
driver_for_dbinv_feature_toggle = 'unspecified'
Expand All @@ -37,10 +28,10 @@ def setup():
busted_drivers = ('sarannual',)

if driver_for_dbinv_feature_toggle in busted_drivers:
msg = ("Inventory database does not support '{}'. "
"Set GIPS_ORM=false to use the filesystem inventory instead.")
raise Exception(msg.format(driver_for_dbinv_feature_toggle))
with error_handler("Error initializing Django ORM"):
raise Exception("Inventory database does not support '{}'. Set"
" GIPS_ORM = False to use the filesystem inventory"
" instead.".format(driver_for_dbinv_feature_toggle))
with utils.error_handler("Error initializing Django ORM"):
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gips.inventory.orm.settings")
django.setup()
setup_complete = True
3 changes: 2 additions & 1 deletion gips/scripts/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def main():

if args.rectify:
if not orm.use_orm():
raise ValueError("--rectify can only be used if GIPS_ORM=true.")
raise ValueError("--rectify can only be used if"
" GIPS_ORM = True.")
for k, v in vars(args).items():
# Let the user know not to expect other options to effect rectify
if v and k not in ('rectify', 'verbose', 'command'):
Expand Down
3 changes: 2 additions & 1 deletion gips/test/int/t_sentinel2_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def t_archive_old_asset(archive_setup, mocker):
m_cloud_cover = mocker.patch.object(
sentinel2.sentinel2Asset, 'cloud_cover')
gippy.Options.SetVerbose(4) # to get tracebacks
sentinel2.sentinel2Asset.archive(stage_path) # touches db if GIPS_ORM=true, hence django_db
# touches db if use_orm(), hence django_db
sentinel2.sentinel2Asset.archive(stage_path)

assert (not os.path.exists(staged_asset_fn)
and all([os.path.exists(fn) for fn in archived_asset_fns]))
27 changes: 0 additions & 27 deletions gips/test/unit/t_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,6 @@

"""Test inventory.orm.* for correctness."""

@pytest.mark.parametrize("env_var, expected", (
('true', True),
('TrUe', True),
('TRUE', True),
('false', False),
('99agmavfa95', False),
('1', True),
('1.0', True),
('1.1', True),
('-1.0', True),
('-1.1', True),
('0', False),
('-0', False),
('0.0', False),
('', False),
(' ', False),
))
def t_use_orm(mocker, env_var, expected):
"""Test various inputs for the env var."""
mocker.patch.dict('gips.inventory.orm.os.environ', GIPS_ORM=env_var)
assert expected == orm.use_orm()

def t_use_orm_unset_var(mocker):
"""Unset env var should result in ORM use."""
mocker.patch.dict('gips.inventory.orm.os.environ', clear=True)
assert orm.use_orm()

@pytest.mark.parametrize('setup_complete, use_orm, expected', (
(False, True, True),
(False, False, False),
Expand Down

0 comments on commit 4dd5a21

Please sign in to comment.