def fix_spare_parts(): spare_parts = session.query(SparePart).all() for spare_part in spare_parts: as_dict = spare_part.__dict__ as_dict['manufacturer_id'] = MongoManufacturer.find( manufacturer_id=spare_part.manufacturer_id).first_or_none()._id as_dict['supplier_id'] = MongoSupplier.find( supplier_id=spare_part.supplier_id).first_or_none()._id car_models = [] if spare_part.car_models is not None: for car_model in spare_part.car_models: car_models.append({ 'model_name': car_model.model_name, 'brand_name': car_model.brand_name }) as_dict.update({'car_models': car_models}) stores = [] for store in spare_part.stores: stores.append({ 'store_id': MongoStore.find(store_id=store.store_id).first_or_none()._id, 'stock': store.stock, 'stock_location': store.stock_location }) as_dict.update({'stores': stores}) as_dict['estimated_time_of_arrival'] = datetime.datetime( spare_part.estimated_time_of_arrival.year, spare_part.estimated_time_of_arrival.month, spare_part.estimated_time_of_arrival.day) del as_dict["_sa_instance_state"] mongo_spare_part = MongoSparePart(as_dict) mongo_spare_part.save()
def update_product(product: MongoSparePart, attribute_name, new_value): product[attribute_name] = new_value # product.__dict__[attribute_name] = new_value # product.__dict__.update({attribute_name: new_value}) # product.__setattr__(attribute_name, new_value) print(product.order_quantity) product.save()
def steal_products(thief): available_items = SparePart.find(**{"stores.store_id": thief.store_id}) stolen_item = random.choice(available_items) amount = stolen_item.stores[0]["stock"] stolen_item.stores[0]["stock"] = 0 stolen_item.save() return amount, stolen_item
def cleaning_ids(): customers = MongoCustomer.all() for customer in customers: customer.delete_field("customer_id") employees = MongoEmployee.all() for employee in employees: employee.delete_field("employee_id") manufacturers = MongoManufacturer.all() for man in manufacturers: man.delete_field("manufacturer_id") orders = MongoOrder.all() for order in orders: order.delete_field("order_id") spare_parts = MongoSparePart.all() for spare in spare_parts: spare.delete_field("spare_part_id") stores = MongoStore.all() for store in stores: store.delete_field("store_id") suppliers = MongoSupplier.all() for supplier in suppliers: supplier.delete_field("supplier_id")
def fix_orders(): orders = session.query(Order).all() for order in orders: as_dict = order.__dict__ as_dict["employee_id"] = MongoEmployee.find( employee_id=order.employee_id).first_or_none()._id as_dict["customer_id"] = MongoCustomer.find( customer_id=order.customer_id).first_or_none()._id as_dict["store_id"] = MongoStore.find( store_id=order.store_id).first_or_none()._id order_line = [{ "spare_part_id": MongoSparePart.find( spare_part_id=ol.spare_part_id).first_or_none()._id, "quantity": ol.quantity } for ol in order.order_lines] as_dict.update({"order_detail": order_line}) del as_dict["order_lines"] del as_dict["_sa_instance_state"] mongo_order = MongoOrder(as_dict) for ol in mongo_order.order_detail: del ol["spare_part"] mongo_order.save()
def add_product_interface(): add_product_dict = {f"{i.replace(' ', '_').lower()}": input(f"{i}: ") for i in [ "Product nr", "Description", "Purchase price", "Selling price", "Reorder level", "Order quantity", "Estimated time of arrival" ]} new_product = MongoSparePart(**add_product_dict) new_product.manufacturer, new_product.supplier = add_manufacturer(), add_supplier() [new_product.car_models.append(car_model) for car_model in add_existing_car_models()] [new_product.stores.append(store) for store in add_spare_part_stores()] if add_product(new_product): print(f"You successfully added a product to the database!".center(45, "-")) print(new_product.print_all_information_with_relationships()) os.system("pause") else: print("Something went wrong") os.system("pause")
def choose_action_for_product_menu(product: MongoSparePart): while True: print("Select what you want to do?") print("1. Show all information") print("2. Edit") print("3. Delete") print("0. Exit") selection = input("> ") if selection == "1": print(product.__repr__()) if selection == "2": edit_menu(product) if selection == "3": print_success_message(success=delete_product(product)) product_menu() if selection == "0": break
def get_product_by_product_nr(product_nr: str): return MongoSparePart.find(product_nr=product_nr).first_or_none()
def get_product_by_id(product_id: str): return MongoSparePart.find(_id=product_id).first_or_none()