Esempio n. 1
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
Esempio n. 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
Esempio n. 3
0
    def delete_list(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 not 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_list(fam_id)
Esempio n. 4
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