Skip to content

Commit

Permalink
Add bpstore object instantiation kwargs forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
billyrrr committed Apr 7, 2020
1 parent 1ede77b commit 0ff67be
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 10 deletions.
24 changes: 20 additions & 4 deletions flask_boiler/business_property_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ def structural_ref_fields(self):
class BusinessPropertyStore:

@classmethod
def from_snapshot_struct(cls, snapshot_struct):
def from_snapshot_struct(cls, snapshot_struct, **kwargs):
struct, container = snapshot_struct.to_struct_and_container()
store = BusinessPropertyStore(struct=struct,
snapshot_container=container)
snapshot_container=container,
**kwargs
)
store.refresh()
return store

def __init__(self, struct, snapshot_container):
def __init__(self, struct, snapshot_container, obj_options=None):
"""
:param struct:
:param snapshot_container:
:param obj_options: keyword arguments to pass to snapshot_to_obj
(eventually applied to obj_cls.from_dict)
"""
super().__init__()
if obj_options is None:
obj_options = dict()
self._obj_options = obj_options
self._container = snapshot_container
self.struct = struct
self.schema_obj = struct.schema_obj
Expand All @@ -54,7 +66,11 @@ def bprefs(self):

def refresh(self):
for doc_ref in self._manifest:
self.objs[doc_ref] = snapshot_to_obj(self._container.get(doc_ref))
self.objs[doc_ref] = snapshot_to_obj(
self._container.get(doc_ref),
super_cls=None,
**self._obj_options
)

def __getattr__(self, item):

Expand Down
7 changes: 5 additions & 2 deletions flask_boiler/firestore_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_vm(doc_ref):
return super()._export_val_view(val)

@classmethod
def _import_val(cls, val, to_get=False, transaction=None):
def _import_val(cls, val, to_get=False, must_get=False, transaction=None):

def is_nested_relationship(val):
return isinstance(val, RelationshipReference) and val.nested
Expand All @@ -138,7 +138,10 @@ def nest_relationship(val: RelationshipReference):
else:
return val.doc_ref
elif is_ref_only_relationship(val):
return val.doc_ref
if must_get:
return nest_relationship(val)
else:
return val.doc_ref
else:
return super()._import_val(
val, to_get=to_get, transaction=transaction)
Expand Down
16 changes: 14 additions & 2 deletions flask_boiler/serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def update_vals(self, with_dict=None, with_raw=None, **kwargs):
setattr(self, key, val)

@classmethod
def from_dict(cls, d, to_get=True, **kwargs):
def from_dict(cls, d, to_get=True, must_get=False, **kwargs):
"""
TODO: fix to_get not applying to new(**kwargs)
Expand All @@ -166,8 +166,20 @@ def from_dict(cls, d, to_get=True, **kwargs):
.format(obj_type, super_cls))

d = obj_cls.get_schema_obj().load(d)

def apply(val):
if isinstance(val, dict):
return {k: obj_cls._import_val(v, to_get=to_get, must_get=must_get)
for k, v in val.items()
}
elif isinstance(val, list):
return [obj_cls._import_val(v, to_get=to_get, must_get=must_get)
for v in val]
else:
return obj_cls._import_val(val, to_get=to_get, must_get=must_get)

d = {
key: obj_cls._import_val(val, to_get=to_get)
key: apply(val)
for key, val in d.items()
}

Expand Down
12 changes: 11 additions & 1 deletion tests/color_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,18 @@ def fin():
schema=ColorSchema,
base=ColorDomainModelBase
)


PaletteViewModel = factory.ClsFactory.create(
name="PaletteViewModel",
schema=Palette,
base=PaletteViewModelBase
)
)


PaletteDomainModel = factory.ClsFactory.create(
name="PaletteDomainModel",
schema=Palette,
base=domain_model.DomainModel
)

62 changes: 61 additions & 1 deletion tests/test_bpstore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from google.cloud.firestore_v1 import DocumentReference

from flask_boiler.snapshot_container import SnapshotContainer
from flask_boiler.struct import Struct
from .color_fixtures import color_refs, Color
from .color_fixtures import color_refs, Color, Palette, PaletteDomainModel
from .fixtures import CTX
from flask_boiler import fields
from flask_boiler.business_property_store import BusinessPropertyStore, BPSchema
Expand Down Expand Up @@ -45,3 +47,61 @@ class Store(BusinessPropertyStore):
)
store.refresh()
assert isinstance(store.favorite_color, Color)


def test_obj_options(CTX, color_refs, request):
palette = PaletteDomainModel.new(
doc_id="partial_rainbow_palette",
palette_name="partial_rainbow",
colors=color_refs.copy()
)
palette.save()

def fin():
palette.delete()

request.addfinalizer(fin)

class PaletteStoreSchema(BPSchema):
favorite_palette = fields.StructuralRef(dm_cls=PaletteDomainModel)

struct = Struct(schema_obj=PaletteStoreSchema())
struct["favorite_palette"] = (PaletteDomainModel, palette.doc_id)

class Store(BusinessPropertyStore):
pass

"""
Original: non-nested retrieves DocumentReference
"""

store = Store(
struct=struct,
snapshot_container=SnapshotContainer()
)
store._container.set(
'projects/flask-boiler-testing/databases/(default)/documents/PaletteDomainModel/partial_rainbow_palette',
CTX.db.document('projects/flask-boiler-testing/databases/(default)/documents/PaletteDomainModel/partial_rainbow_palette').get()
)
store.refresh()
assert isinstance(store.favorite_palette, PaletteDomainModel)
assert isinstance(store.favorite_palette.colors[0], DocumentReference)

"""
New: nested retrieves objects
"""
store = Store(
struct=struct,
snapshot_container=SnapshotContainer(),
obj_options=dict(
must_get=True
)
)
store._container.set(
'projects/flask-boiler-testing/databases/(default)/documents/PaletteDomainModel/partial_rainbow_palette',
CTX.db.document(
'projects/flask-boiler-testing/databases/(default)/documents/PaletteDomainModel/partial_rainbow_palette').get()
)
store.refresh()
assert isinstance(store.favorite_palette, PaletteDomainModel)
assert isinstance(store.favorite_palette.colors[0], Color)

0 comments on commit 0ff67be

Please sign in to comment.