From 6a4a0bb8b3cca48e2d076bcdb6b79bcf9adcf0aa Mon Sep 17 00:00:00 2001 From: Michael Kennedy Date: Thu, 8 Jun 2017 14:11:49 -0700 Subject: [PATCH] Dealership with engine embedded in car document. --- .gitignore | 1 + .../service_central/nosql/car.py | 20 +++++++++++++ .../service_central/nosql/engine.py | 11 ++++++++ .../service_central/nosql/mongo_setup.py | 5 ++++ .../service_central/service_app.py | 28 ++++++++++++++++++- 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/07_mongoengine/service_central/nosql/car.py create mode 100644 src/07_mongoengine/service_central/nosql/engine.py create mode 100644 src/07_mongoengine/service_central/nosql/mongo_setup.py diff --git a/.gitignore b/.gitignore index d545238..feb8e4b 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,4 @@ src/07_mongoengine/service_central/.idea/misc.xml src/07_mongoengine/service_central/.idea/modules.xml src/07_mongoengine/service_central/.idea/service_central.iml src/07_mongoengine/service_central/.idea/inspectionProfiles/profiles_settings.xml +screencaster.xml diff --git a/src/07_mongoengine/service_central/nosql/car.py b/src/07_mongoengine/service_central/nosql/car.py new file mode 100644 index 0000000..e8120e0 --- /dev/null +++ b/src/07_mongoengine/service_central/nosql/car.py @@ -0,0 +1,20 @@ +import uuid +import mongoengine + +from nosql.engine import Engine + + +class Car(mongoengine.Document): + model = mongoengine.StringField(required=True) + make = mongoengine.StringField(required=True) + year = mongoengine.IntField(required=True) + mileage = mongoengine.FloatField(default=0.0) + vi_number = mongoengine.StringField( + default=lambda: str(uuid.uuid4()).replace('-', '')) + + engine = mongoengine.EmbeddedDocumentField(Engine, required=True) + + meta = { + 'db_alias': 'core', + 'collection': 'cars', + } diff --git a/src/07_mongoengine/service_central/nosql/engine.py b/src/07_mongoengine/service_central/nosql/engine.py new file mode 100644 index 0000000..dac9cc7 --- /dev/null +++ b/src/07_mongoengine/service_central/nosql/engine.py @@ -0,0 +1,11 @@ +import uuid +import mongoengine + + +class Engine(mongoengine.EmbeddedDocument): + horsepower = mongoengine.IntField(required=True) + liters = mongoengine.FloatField(required=True) + mpg = mongoengine.FloatField(required=True) + serial_number = mongoengine.StringField( + default=lambda: str(uuid.uuid4()) + ) diff --git a/src/07_mongoengine/service_central/nosql/mongo_setup.py b/src/07_mongoengine/service_central/nosql/mongo_setup.py new file mode 100644 index 0000000..8f5f319 --- /dev/null +++ b/src/07_mongoengine/service_central/nosql/mongo_setup.py @@ -0,0 +1,5 @@ +import mongoengine + + +def global_init(): + mongoengine.register_connection(alias='core', name='demo_dealership') diff --git a/src/07_mongoengine/service_central/service_app.py b/src/07_mongoengine/service_central/service_app.py index 0bc6a62..52b3f8c 100644 --- a/src/07_mongoengine/service_central/service_app.py +++ b/src/07_mongoengine/service_central/service_app.py @@ -1,5 +1,11 @@ +import nosql.mongo_setup as mongo_setup +from nosql.car import Car +from nosql.engine import Engine + + def main(): print_header() + config_mongo() user_loop() @@ -13,6 +19,10 @@ def print_header(): print() +def config_mongo(): + mongo_setup.global_init() + + def user_loop(): while True: print("Available actions:") @@ -37,7 +47,23 @@ def user_loop(): def add_car(): - print("TODO: add_car") + model = input("What is the model? ") + make = 'Ferrari' # input("What is the make? ") + year = int(input("Year built? ")) + + car = Car() + car.year = year + car.make = make + car.model = model + + engine = Engine() + engine.horsepower = 590 + engine.mpg = 22 + engine.liters = 4.0 + + car.engine = engine + + car.save() def list_cars():