Skip to content

Commit

Permalink
return funding_instrument_uris
Browse files Browse the repository at this point in the history
  • Loading branch information
keryc committed Feb 18, 2021
1 parent 141a534 commit 4c36ba3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
13 changes: 10 additions & 3 deletions agave/lib/mongoengine/model_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def mongo_to_dict(obj, exclude_fields: list = None) -> dict:
exclude_fields = []

for field_name in obj._fields:

if field_name in exclude_fields:
continue

if field_name == 'id':
continue
data = obj._data[field_name]

if isinstance(obj._fields[field_name], ListField):
field_name = (
f'{field_name}_uris'
Expand Down Expand Up @@ -71,7 +71,7 @@ def mongo_to_dict(obj, exclude_fields: list = None) -> dict:
)
else:
return_data[field_name] = mongo_to_python_type(
obj._fields[field_name], data
obj._fields[field_name], data, field_name
)

return return_data
Expand All @@ -93,7 +93,7 @@ def list_field_to_dict(list_field: list) -> list:
return return_data


def mongo_to_python_type(field, data):
def mongo_to_python_type(field, data, field_name: str = ''):
rv = None
field_type = type(field)
if data is None:
Expand All @@ -110,6 +110,13 @@ def mongo_to_python_type(field, data):
rv = bool(data)
elif field_type is DecimalField:
rv = data
elif field_name == 'funding_instrument':
mapper = {
'BA': '/accounts/',
'CA': '/cards/',
'SP': '/service_providers/'
}
return f'{mapper[data[:2]]}{data}'
else:
rv = str(data)

Expand Down
2 changes: 1 addition & 1 deletion agave/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.0'
__version__ = '0.1.1.dev0'
8 changes: 6 additions & 2 deletions tests/lib/test_model_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime as dt
from enum import Enum
from bson import SON, DBRef

from mongoengine import (
BooleanField,
Expand Down Expand Up @@ -33,7 +34,6 @@ class EnumType(Enum):
class Embedded(EmbeddedDocument):
name = StringField()


class TestModel(Document):
str_field = StringField()
int_field = IntField(default=1)
Expand All @@ -51,6 +51,7 @@ class TestModel(Document):
lazzy_field = LazyReferenceField(Reference)
lazzy_list_field = ListField(LazyReferenceField(Reference))
generic_lazzy_field = GenericLazyReferenceField()
funding_instrument = StringField()

__test__ = False

Expand All @@ -60,7 +61,9 @@ def test_mongo_to_dict():
reference = Reference()
reference.save()
model = TestModel(
embedded_list_field=[Embedded(name='')], lazzy_list_field=[reference]
embedded_list_field=[Embedded(name='')],
lazzy_list_field=[reference],
funding_instrument='CAXXXX'
)
model.save()
model_dict = mongo_to_dict(model, exclude_fields=['str_field'])
Expand All @@ -80,4 +83,5 @@ def test_mongo_to_dict():
assert model_dict['embedded_field'] == {}
assert model_dict['lazzy_field_uri'] is None
assert model_dict['generic_lazzy_field_uri'] is None
assert model_dict['funding_instrument'] == '/cards/CAXXXX'
assert model_dict['lazzy_list_field_uris'] == ["Reference object"]

0 comments on commit 4c36ba3

Please sign in to comment.