예제 #1
0
    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
예제 #2
0
    def register_list(cls, member_id, fam_id, alias, in_store):
        # make list_to_cart object
        member = MemberModel.find_by_id(member_id)
        already = ListToCartModel.get_fam_list(fam_id)
        for each in already:
            if each.alias == alias:
                return "Duplicate item in list", 400
        try:
            new_list_to_cart = ListToCartModel(
                alias, in_store, fam_id,
                member.first_name + " " + member.last_name)
            new_list_to_cart.save_to_db()
        except:
            cls.logger.exception("Error making a new ListToCart object")
            return "Internal Server Error", 500

        return "", 201
예제 #3
0
    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)
예제 #4
0
    def collect_data(cls, device_id, metadata):
        target_device = DeviceShadowModel.find_by_device_id(device_id)
        if not target_device:
            cls.logger.exception(
                "Tried to add a stamp of a device that doesn't exist")
            return "Invalid Request", 400
        try:
            new_stamp = DeviceDataModel(device_id, metadata)
            new_stamp.save_to_db()
        except:
            cls.logger.exception(
                "Error while creating a new stamp in the Device Data Model")
            return "Internal Server Error", 500

        if target_device.date_updated < new_stamp.date_created:
            try:
                target_device.shadow_metadata = new_stamp.device_metadata
                target_device.date_updated = new_stamp.date_created
                target_device.save_to_db()
                if float(new_stamp.device_metadata['percent']
                         ) < target_device.alert_level:
                    # move that niche thing to list
                    # if the same name is in the listtocart, delete that item
                    make = True
                    already = ListToCartModel.get_fam_list(
                        target_device.fam_id)
                    for each in already:
                        if each.alias == target_device.alias:
                            each.from_niche = True
                            each.in_store = target_device.auto_order_store
                            each.save_to_db()
                            make = False

                    if make:
                        try:
                            new_list_to_cart = ListToCartModel(
                                target_device.alias,
                                target_device.auto_order_store,
                                target_device.fam_id, "Niche")
                            new_list_to_cart.from_niche = True
                            new_list_to_cart.save_to_db()
                        except:
                            cls.logger.exception(
                                "Error while moving niche into listtocart")
                            return "Internal Server Error", 500

            except:
                cls.logger.exception("Error while saving most recent")
                return "Internal Server Error", 500

        return "", 201
예제 #5
0
    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
예제 #6
0
 def get_list(cls, fam_id):
     try:
         return "", 200, ListToCartModel.get_fam_list(fam_id)
     except:
         cls.logger.exception("Failure in getting list")
         return "Internal Server Error", 500, None
예제 #7
0
    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