Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
billyrrr committed Sep 1, 2019
1 parent 2474cba commit b4ded3c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
25 changes: 15 additions & 10 deletions examples/luggage_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from src import serializable, schema, fields, domain_model, view_model
from src.firestore_object import FirestoreObjectClsFactory

Schema = schema.Schema

Expand All @@ -18,23 +19,22 @@ class LuggageCollectionSchema(Schema):
total_weight = fields.Integer(dump_to="total_weight", dump_only=True)


class LuggageItem(domain_model.DomainModel):
class LuggageItemBase(domain_model.DomainModel):

_schema_cls = LuggageItemSchema
_collection_name = "LuggageItem"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.luggage_type = str()
self.weight_in_lbs = int()
self.owner_id = str()

LuggageItem = FirestoreObjectClsFactory.create(
name="LuggageItem",
schema=LuggageItemSchema,
base=LuggageItemBase
)

class Luggages(view_model.ViewModel):

class LuggagesBase(view_model.ViewModel):
""" Keeps track of luggage amount
"""

_schema_cls = LuggageCollectionSchema

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._luggage_d = dict()
Expand Down Expand Up @@ -90,3 +90,8 @@ def _get_weight(self) -> int:
return weight


Luggages = FirestoreObjectClsFactory.create(
name="Luggages",
schema=LuggageCollectionSchema,
base=LuggagesBase
)
11 changes: 7 additions & 4 deletions examples/view_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from src.view import GenericView, document_as_view, default_mapper
from src import fields
from src.schema import Schema
from src.firestore_object import FirestoreObjectClsFactory
from src.view_model import ViewModel
from google.cloud import firestore
from functools import partial
Expand All @@ -35,9 +36,11 @@ class Palette(Schema):
palette_name = fields.Str()
colors = fields.Nested(Color, many=True)

class PaletteViewModel(ViewModel):
_schema_cls = Palette

PaletteViewModel = FirestoreObjectClsFactory.create(
name="PaletteViewModel",
schema=Palette,
base=ViewModel
)

description = "A list of colors (may be filtered by palette)"

Expand Down Expand Up @@ -73,7 +76,7 @@ class PaletteViewModel(ViewModel):
obj = document_as_view(
view_model_cls=PaletteViewModel,
app=app,
endpoint="palettes/<doc_id:string>",
endpoint="/palettes/<string:doc_id>",
mapper=palette_doc_mapper)

app.run(debug=True)
7 changes: 7 additions & 0 deletions src/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ def default_value(self):
return str()


class Nested(fields.Nested, Field):

@property
def default_value(self):
return None


Str = String
3 changes: 2 additions & 1 deletion src/serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ def to_dict(self):
def initializer(obj, d):
for key, val in d.items():
assert isinstance(val, fields.Field)
if not hasattr(obj, key):
# if not hasattr(obj, key):
if key not in dir(obj):
setattr(obj, key, val.default_value)


Expand Down

0 comments on commit b4ded3c

Please sign in to comment.