def edit_list_to_cart(cls, fam_id, data): # find listtocart object and update the fields target = ListToCartModel.find_by_id(data["list_to_cart_id"]) # no such target if not target: cls.logger.exception( "Invalid edit to list_to_target object requested") return "Ill-formed Request", 400 # Not your family if target.fam_id != fam_id: cls.logger.exception( "WARNING WARNING WARNING BREACH ATTEMPT. At this point it should be pretty obvious that this is someone meddling with our endpoint." ) return "Ill-formed Request", 400 try: target.alias = data['alias'] target.in_cart = data['in_cart'] target.in_store = data['in_store'] target.item_image = data['item_image'] target.item_name = data['item_name'] target.item_price = data['item_price'] target.item_quantity = data['item_quantity'] target.save_to_db() except: cls.logger.exception("Error updating ListToCart object") return "Internal Server Error", 500 return "", 200
def delete_cart(cls, fam_id, victim_ids): for victim in victim_ids: target = ListToCartModel.find_by_id(victim) if not target: cls.logger.exception( "Invalid delete to list_to_cart object requested") return "Ill-formed Request", 400, None if target.fam_id != fam_id: cls.logger.exception( "WARNING WARNING WARNING BREACH ATTEMPT. At this point it should be pretty obvious that this is someone meddling with our endpoint." ) return "Ill-formed Request", 400, None if target.in_cart: try: target.delete_from_db() except: cls.logger.exception("Error deleting target list_to_cart") return "Internal Server Error", 500, None return "", 200, ListToCartModel.get_fam_cart(fam_id)
def switch_list_to_cart(cls, fam_id, list_to_cart_id): target = ListToCartModel.find_by_id(list_to_cart_id) if not target: cls.logger.exception("Invalid list_to_cart object requested") return "Ill-formed Request", 400 if target.fam_id != fam_id: cls.logger.exception( "WARNING WARNING WARNING BREACH ATTEMPT. At this point it should be pretty obvious that this is someone meddling with our endpoint." ) return "Ill-formed Request", 400 try: target.in_cart = not target.in_cart target.in_store = "" target.item_image = "" target.item_name = "" target.item_price = 0 target.item_quantity = 0 target.save_to_db() except: cls.logger.exception("Error toggling and saving list_to_cart") return "Internal Server Error", 500 return "", 200
def checkout(cls, member_id, fam_id, total, items): # make checkout object member = MemberModel.find_by_id(member_id) if member.authority != 100: return "Unauthorized Request", 403 try: new_checkout = CheckoutModel(total, fam_id, member_id) new_checkout.save_to_db() except: cls.logger.exception("Error creating new Checkout object") return "Internal Server Error", 500 item_data = [] for list_to_cart_id in items: target = ListToCartModel.find_by_id(list_to_cart_id) if target: if target.in_cart and target.item_name and target.item_image and target.item_price and target.item_quantity and not target.bought: item_data.append(target.json()) target.bought = True target.save_to_db() else: cls.logger.exception( "Requested a list_to_cart that doens't have sufficient info for checkout" ) return "Ill-formed Request", 400 else: cls.logger.exception( "Reqeusts a list_to_cart that doesn't exist") return "Ill-formed Request", 400 # make the list of objects for product in item_data: try: new_product = ProductModel(product['in_store'], product['item_price'], product['item_image'], product['item_name'], new_checkout.id) new_product.save_to_db() except: cls.logger.exception("Error creating new Product") return "Internal Server Error", 500 family = FamilyModel.find_by_id(fam_id) try: loop = asyncio.get_event_loop() if loop.is_closed(): asyncio.set_event_loop(asyncio.new_event_loop()) except: asyncio.set_event_loop(asyncio.new_event_loop()) loop = asyncio.get_event_loop() # send email to deliver :) tasks = [ asyncio.ensure_future( Emailer.checkout(total, member.first_name + ' ' + member.last_name, family.name, item_data, new_checkout.id)) ] loop.run_until_complete(asyncio.wait(tasks)) loop.close() return "", 201