From fca73115a3c5da9c43b9e285af33f5582e9b35e9 Mon Sep 17 00:00:00 2001 From: Michael Kennedy Date: Tue, 13 Jun 2017 22:34:38 -0700 Subject: [PATCH] Starter python code for deployed app. --- .../service_central_deploys/nosql/car.py | 22 +++ .../service_central_deploys/nosql/engine.py | 11 ++ .../nosql/mongo_setup.py | 5 + .../nosql/servicehistory.py | 10 ++ .../service_central_deploys/service_app.py | 135 ++++++++++++++++++ .../nosql/car.py | 22 +++ .../nosql/engine.py | 11 ++ .../nosql/mongo_setup.py | 5 + .../nosql/servicehistory.py | 10 ++ .../service_app.py | 135 ++++++++++++++++++ 10 files changed, 366 insertions(+) create mode 100644 src/09_deploys/service_central_deploys/nosql/car.py create mode 100644 src/09_deploys/service_central_deploys/nosql/engine.py create mode 100644 src/09_deploys/service_central_deploys/nosql/mongo_setup.py create mode 100644 src/09_deploys/service_central_deploys/nosql/servicehistory.py create mode 100644 src/09_deploys/service_central_deploys/service_app.py create mode 100644 src/09_deploys/starter_service_central_deploys/nosql/car.py create mode 100644 src/09_deploys/starter_service_central_deploys/nosql/engine.py create mode 100644 src/09_deploys/starter_service_central_deploys/nosql/mongo_setup.py create mode 100644 src/09_deploys/starter_service_central_deploys/nosql/servicehistory.py create mode 100644 src/09_deploys/starter_service_central_deploys/service_app.py diff --git a/src/09_deploys/service_central_deploys/nosql/car.py b/src/09_deploys/service_central_deploys/nosql/car.py new file mode 100644 index 0000000..886b78b --- /dev/null +++ b/src/09_deploys/service_central_deploys/nosql/car.py @@ -0,0 +1,22 @@ +import uuid +import mongoengine + +from nosql.engine import Engine +from nosql.servicehistory import ServiceHistory + + +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) + service_history = mongoengine.EmbeddedDocumentListField(ServiceHistory) + + meta = { + 'db_alias': 'core', + 'collection': 'cars', + } diff --git a/src/09_deploys/service_central_deploys/nosql/engine.py b/src/09_deploys/service_central_deploys/nosql/engine.py new file mode 100644 index 0000000..dac9cc7 --- /dev/null +++ b/src/09_deploys/service_central_deploys/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/09_deploys/service_central_deploys/nosql/mongo_setup.py b/src/09_deploys/service_central_deploys/nosql/mongo_setup.py new file mode 100644 index 0000000..8f5f319 --- /dev/null +++ b/src/09_deploys/service_central_deploys/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/09_deploys/service_central_deploys/nosql/servicehistory.py b/src/09_deploys/service_central_deploys/nosql/servicehistory.py new file mode 100644 index 0000000..5cc41f2 --- /dev/null +++ b/src/09_deploys/service_central_deploys/nosql/servicehistory.py @@ -0,0 +1,10 @@ +import mongoengine +import datetime + + +class ServiceHistory(mongoengine.EmbeddedDocument): + date = mongoengine.DateTimeField(default=datetime.datetime.now) + description = mongoengine.StringField() + price = mongoengine.FloatField() + customer_rating = mongoengine.IntField(min_value=1, max_value=5) + diff --git a/src/09_deploys/service_central_deploys/service_app.py b/src/09_deploys/service_central_deploys/service_app.py new file mode 100644 index 0000000..3454956 --- /dev/null +++ b/src/09_deploys/service_central_deploys/service_app.py @@ -0,0 +1,135 @@ +import nosql.mongo_setup as mongo_setup +from nosql.car import Car +from nosql.engine import Engine +from nosql.servicehistory import ServiceHistory + + +def main(): + print_header() + config_mongo() + # update_doc_versions() + user_loop() + + +# noinspection PyProtectedMember +# def update_doc_versions(): +# for car in Car.objects(): +# car._mark_as_changed('vi_number') +# car.save() + + +def print_header(): + print('----------------------------------------------') + print('| |') + print('| SERVICE CENTRAL v.02 |') + print('| demo edition |') + print('| |') + print('----------------------------------------------') + print() + + +def config_mongo(): + mongo_setup.global_init() + + +def user_loop(): + while True: + print("Available actions:") + print(" * [a]dd car") + print(" * [l]ist cars") + print(" * [p]oorly serviced") + print(" * perform [s]ervice") + print(" * e[x]it") + print() + ch = input("> ").strip().lower() + if ch == 'a': + add_car() + elif ch == 'l': + list_cars() + elif ch == 's': + service_car() + elif ch == 'p': + show_poorly_serviced_cars() + elif not ch or ch == 'x': + print("Goodbye") + break + + +def 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(): + cars = Car.objects().order_by("-year") + for car in cars: + print("{} -- {} with vin {} (year {})".format( + car.make, car.model, car.vi_number, car.year)) + print("{} of service records".format(len(car.service_history))) + for s in car.service_history: + print(" * ${:,.0f} {}".format(s.price, s.description)) + print() + + +def find_car(): + print("TODO: find_car") + + +def service_car(): + # vin = input("What is the VIN of the car to service? ") + # car = Car.objects(vi_number=vin).first() + # if not car: + # print("Car with VIN {} not found!".format(vin)) + # return + # + # service = ServiceHistory() + # service.price = float(input("What is the price? ")) + # service.description = input("What type of service is this? ") + # service.customer_rating = int(input("How happy is our customer? [1-5] ")) + # + # car.service_history.append(service) + # car.save() + + vin = input("What is the VIN of the car to service? ") + service = ServiceHistory() + service.price = float(input("What is the price? ")) + service.description = input("What type of service is this? ") + service.customer_rating = int(input("How happy is our customer? [1-5] ")) + + updated = Car.objects(vi_number=vin).update_one(push__service_history=service) + if updated == 0: + print("Car with VIN {} not found!".format(vin)) + return + + +def show_poorly_serviced_cars(): + level = int(input("What max level of satisfaction are we looking for? [1-5] ")) + # { "service_history.customer_rating": {$lte: level} } + cars = Car.objects(service_history__customer_rating__lte=level) + for car in cars: + print("{} -- {} with vin {} (year {})".format( + car.make, car.model, car.vi_number, car.year)) + print("{} of service records".format(len(car.service_history))) + for s in car.service_history: + print(" * Satisfaction: {} ${:,.0f} {}".format( + s.customer_rating, s.price, s.description)) + print() + + +if __name__ == '__main__': + main() diff --git a/src/09_deploys/starter_service_central_deploys/nosql/car.py b/src/09_deploys/starter_service_central_deploys/nosql/car.py new file mode 100644 index 0000000..886b78b --- /dev/null +++ b/src/09_deploys/starter_service_central_deploys/nosql/car.py @@ -0,0 +1,22 @@ +import uuid +import mongoengine + +from nosql.engine import Engine +from nosql.servicehistory import ServiceHistory + + +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) + service_history = mongoengine.EmbeddedDocumentListField(ServiceHistory) + + meta = { + 'db_alias': 'core', + 'collection': 'cars', + } diff --git a/src/09_deploys/starter_service_central_deploys/nosql/engine.py b/src/09_deploys/starter_service_central_deploys/nosql/engine.py new file mode 100644 index 0000000..dac9cc7 --- /dev/null +++ b/src/09_deploys/starter_service_central_deploys/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/09_deploys/starter_service_central_deploys/nosql/mongo_setup.py b/src/09_deploys/starter_service_central_deploys/nosql/mongo_setup.py new file mode 100644 index 0000000..8f5f319 --- /dev/null +++ b/src/09_deploys/starter_service_central_deploys/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/09_deploys/starter_service_central_deploys/nosql/servicehistory.py b/src/09_deploys/starter_service_central_deploys/nosql/servicehistory.py new file mode 100644 index 0000000..5cc41f2 --- /dev/null +++ b/src/09_deploys/starter_service_central_deploys/nosql/servicehistory.py @@ -0,0 +1,10 @@ +import mongoengine +import datetime + + +class ServiceHistory(mongoengine.EmbeddedDocument): + date = mongoengine.DateTimeField(default=datetime.datetime.now) + description = mongoengine.StringField() + price = mongoengine.FloatField() + customer_rating = mongoengine.IntField(min_value=1, max_value=5) + diff --git a/src/09_deploys/starter_service_central_deploys/service_app.py b/src/09_deploys/starter_service_central_deploys/service_app.py new file mode 100644 index 0000000..3454956 --- /dev/null +++ b/src/09_deploys/starter_service_central_deploys/service_app.py @@ -0,0 +1,135 @@ +import nosql.mongo_setup as mongo_setup +from nosql.car import Car +from nosql.engine import Engine +from nosql.servicehistory import ServiceHistory + + +def main(): + print_header() + config_mongo() + # update_doc_versions() + user_loop() + + +# noinspection PyProtectedMember +# def update_doc_versions(): +# for car in Car.objects(): +# car._mark_as_changed('vi_number') +# car.save() + + +def print_header(): + print('----------------------------------------------') + print('| |') + print('| SERVICE CENTRAL v.02 |') + print('| demo edition |') + print('| |') + print('----------------------------------------------') + print() + + +def config_mongo(): + mongo_setup.global_init() + + +def user_loop(): + while True: + print("Available actions:") + print(" * [a]dd car") + print(" * [l]ist cars") + print(" * [p]oorly serviced") + print(" * perform [s]ervice") + print(" * e[x]it") + print() + ch = input("> ").strip().lower() + if ch == 'a': + add_car() + elif ch == 'l': + list_cars() + elif ch == 's': + service_car() + elif ch == 'p': + show_poorly_serviced_cars() + elif not ch or ch == 'x': + print("Goodbye") + break + + +def 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(): + cars = Car.objects().order_by("-year") + for car in cars: + print("{} -- {} with vin {} (year {})".format( + car.make, car.model, car.vi_number, car.year)) + print("{} of service records".format(len(car.service_history))) + for s in car.service_history: + print(" * ${:,.0f} {}".format(s.price, s.description)) + print() + + +def find_car(): + print("TODO: find_car") + + +def service_car(): + # vin = input("What is the VIN of the car to service? ") + # car = Car.objects(vi_number=vin).first() + # if not car: + # print("Car with VIN {} not found!".format(vin)) + # return + # + # service = ServiceHistory() + # service.price = float(input("What is the price? ")) + # service.description = input("What type of service is this? ") + # service.customer_rating = int(input("How happy is our customer? [1-5] ")) + # + # car.service_history.append(service) + # car.save() + + vin = input("What is the VIN of the car to service? ") + service = ServiceHistory() + service.price = float(input("What is the price? ")) + service.description = input("What type of service is this? ") + service.customer_rating = int(input("How happy is our customer? [1-5] ")) + + updated = Car.objects(vi_number=vin).update_one(push__service_history=service) + if updated == 0: + print("Car with VIN {} not found!".format(vin)) + return + + +def show_poorly_serviced_cars(): + level = int(input("What max level of satisfaction are we looking for? [1-5] ")) + # { "service_history.customer_rating": {$lte: level} } + cars = Car.objects(service_history__customer_rating__lte=level) + for car in cars: + print("{} -- {} with vin {} (year {})".format( + car.make, car.model, car.vi_number, car.year)) + print("{} of service records".format(len(car.service_history))) + for s in car.service_history: + print(" * Satisfaction: {} ${:,.0f} {}".format( + s.customer_rating, s.price, s.description)) + print() + + +if __name__ == '__main__': + main()