Exemplo n.º 1
0
    def post(self):
        if 'name' in request.form.keys():
            name = request.form['name']
            comment = request.form['comment']
        else:
            request_data = json.loads(request.data)
            name = request_data['name']
            comment = request_data['comment']

        errors = validate(item_name=name, item_comment=comment)

        if len(errors) > 0:
            return {"errors": [error.to_json() for error in errors]}, 422
        item = ItemModel(name, comment)
        item.save_to_db()
        return item.to_json(), 201
Exemplo n.º 2
0
 def delete(self):
     name = request.args.get('name') 
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
         return {'message': 'Item deleted.'}
     return {'message': 'Item not found.'}, 404
Exemplo n.º 3
0
 def get(self, group_id, item_id):
     errors = validate(group_id=group_id, item_id=item_id, method="GroupItemResource.get")
     if len(errors) > 0:
         all_errors_in_json = [error.to_json() for error in errors]
         return {'errors': all_errors_in_json}, 500
     item = ItemModel.find_by_id(item_id)
     return item.to_json(), 200
Exemplo n.º 4
0
 def get(self):
     name = request.args.get('name')        
     item = ItemModel.find_by_name(name)
     
     if item:
         return item.json()
     return {'message': 'Item not found'}, 404
Exemplo n.º 5
0
 def patch(self, usage_id, new_value):
     usage = UsageModel.find_by_id(usage_id)
     new_value = float(new_value)
     if usage.min_value > new_value or usage.max_value < new_value:
         return {
             "errors":
             Error(
                 "New value does not fall within the expected range. ({} - {})"
                 .format(usage.min_value, usage.max_value),
                 "{} is outside of ({} - {})".format(
                     new_value, usage.min_value,
                     usage.max_value), 422, "").to_json()
         }, 422
     print("url is: " + "{}/{}/{}".format(
         app.config['HOMELYNK_URI'], usage.external_item_id, new_value))
     response = requests.get(
         url="{}/{}/{}".format(app.config['HOMELYNK_URI'],
                               usage.external_item_id, int(new_value)))
     home_response = response.json()
     item = ItemModel.find_by_id(usage.item_id)
     item_in_json = item.to_json()
     fake_event = {
         'last_use_timestamp': datetime.now().timestamp(),
         'data_type': usage.unit.value,
         'data': float(home_response["current_value"]),
         'usage_id': usage.id
     }
     item_in_json['last_use'] = fake_event
     return item_in_json, 200
Exemplo n.º 6
0
    def get(self, name):

        item = ItemModel.find_by_name(name)

        if item:
            return item.json()
        # item = next(filter(lambda x: x['name'] == name, items), None)
        return {'item': item}, 200 if item else 404
Exemplo n.º 7
0
    def get(self, item_id):
        errors = validate(item_id=item_id)
        if len(errors) > 0:
            return {'errors': [error.to_json() for error in errors]}, 404

        item = ItemModel.find_by_id(item_id)
        usages_in_json = [usage.to_json() for usage in item.usages]
        return {"usages": usages_in_json}, 200
Exemplo n.º 8
0
    def delete(self, name):
        data = Item.parser.parse_args()
        returned_item = ItemModel.find_by_name(name, store_id=1)
        if returned_item is None:
            return ({"message": "{} doesn't exists".format(name)})

        returned_item.delete()
        return ({"message": "{} deleted".format(name)}, 200)
Exemplo n.º 9
0
    def delete(self, name):
        # global items
        # items = list(filter(lambda x : x['name'] != name, items))

        item = ItemModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {'message': 'Item deleted!'}
Exemplo n.º 10
0
    def get(self, group_id):
        errors = validate(group_id=group_id)
        if len(errors) > 0:
            all_errors_in_json = [error.to_json() for error in errors]
            return {'errors': all_errors_in_json}, 500

        item_groups_from_group = ItemGroupModel.find_by_group_id(group_id)or []
        all_in_json = [ItemModel.find_by_id(item_group.item_id).to_json() for item_group in item_groups_from_group]
        return {"items": all_in_json}, 200
Exemplo n.º 11
0
 def put(self, name):
     parser = reqparse.RequestParser()
     parser.add_argument("price",
                         type=float,
                         required=True,
                         help="Price Fields Required")
     # data = request.get_json()
     data = parser.parse_args()
     if ItemModel.find_by_name(name) is None:
         ItemModel.insert_item(name, data['price'])
         return {"Message": "Item Added Successfully"}
     else:
         connection = sqlite3.connect("data.db")
         cursor = connection.cursor()
         query = f"UPDATE items SET price=? WHERE name=?"
         cursor.execute(query, (data['price'], name))
         connection.commit()
         connection.close()
         return {"name": name, "price": data['price']}
Exemplo n.º 12
0
    def post(self, name):
        if next(filter(lambda x: x['name'] == name, items), None) is not None:
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }

        data = Item.parser.parse_args()

        item = ItemModel(name, data['price'])

        try:
            item.insert()
            #items.append(item)
        except:
            return {'messege': "something wrong probably item already exist"}

        #sql postres try
        return {'messege': "item insert into the data base"}
Exemplo n.º 13
0
    def post(self, name):
        # if next(filter(lambda x: x["name"] == name, items), None) is not None:
        if ItemModel.find_by_name(name):
            return (
                {
                    "message": f"An item with name {name} already exists."
                },
                400,
            )  # 400: request problem

        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return (
                {
                    "message": "An error occured inserting the item."
                },
                500,
            )  # 500: server problem

        return item.json(), 201
Exemplo n.º 14
0
    def put(self, name):

        data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name, data['store_id'])
        if item is None:
            item = ItemModel(name, data['price'], data['store_id'])
        else:
            item.price = data['price']
        item.save_to_db()

        return (item.json())
Exemplo n.º 15
0
    def delete(self, name):
        #global items
        #items = list(filter(lambda x: x['name'] != name, items))
        #data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)

        try:
            item.delete_item()
        except:
            return {
                'messege': "Item could not be delete probably does not exist"
            }

        return {'messege': "item deleted"}
Exemplo n.º 16
0
    def post(self, name):

        data = Item.parser.parse_args()
        returned_item = ItemModel.find_by_name(name, data['store_id'])
        if returned_item:
            return ({"message": "Item already exists"})
        item = ItemModel(name, data['price'], data['store_id'])
        item.save_to_db()
        return (item.json())
Exemplo n.º 17
0
    def put(self, name):
        data = Item.parser.parse_args()

        item = ItemModel.findItemByName(name)

        if item is None:
            # item=ItemModel(name,data['price'],data['store_id'])
            '''way2'''
            item = ItemModel(name, **data)
        else:
            item.price = data['price']

        item.save_to_db()

        return item.json()
Exemplo n.º 18
0
    def put(self, item_id):
        item = ItemModel.find_by_id(item_id)
        if 'name' in request.form.keys():
            name = request.form['name']
            comment = request.form['comment']
        else:
            request_data = json.loads(request.data)
            name = request_data['name']
            comment = request_data['comment']

        errors = validate(item_id=item_id,
                          item_name=name,
                          item_comment=comment)
        if len(errors) > 0:
            return {"errors": [error.to_json() for error in errors]}, 422

        item = item.update(name=name, comment=comment)
        return item.to_json()
Exemplo n.º 19
0
    def put(self, name):
        data = Item.parser.parse_args()
        # Once again, print something not in the args to verify everything works
        #item = next(filter(lambda x: x['name'] == name, items), None)
        #if item is None:
        #    item = {'name': name, 'price': data['price']}
        #    items.append(item)
        #else:
        #    item.update(data)
        #return item
        item = ItemModel.find_by_name(name)
        if item:
            item.price = data['price']
        else:
            return {'message': "Not item found with that name"}

        item.insert()
        return {'messege': "Item price updated"}
Exemplo n.º 20
0
    def put(self, name):
        # data = request.get_json()
        data = Item.parser.parse_args()
        # item = next(filter(lambda x: x["name"] != name, items), None)
        item = ItemModel.find_by_name(name)
        # updated_item = ItemModel(name, data["price"])
        if item is None:
            item = ItemModel(name, **data)
            # try:
            #     updated_item.insert()
            # except:
            #     return {"message": "An error occured inserting the item."}, 500
        else:
            item.price = data['price']
            item.store = data['store']
            # try:
            #     updated_item.update()
            # except:
            #     return {"message": "An error occured inserting the item."}, 500

        item.save_to_db()
        return item.json()
Exemplo n.º 21
0
    def put(self):
        name = request.args.get('name') 
        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item:
            item.name = data['name']
            item.price = data['price']
        else:
            item = ItemModel(**data)

        item.save_to_db()

        return item.json()
Exemplo n.º 22
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }

        data = Item.parser.parse_args()

        item = ItemModel(name, data['price'], data['store_id'])

        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return item.json(), 201
Exemplo n.º 23
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message': 'An item with name {} already exists'.format(name)
            }, 400
        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the item"
            }, 500  # internal server error

        return item.json(), 201
Exemplo n.º 24
0
    def post(self):
        claims = get_jwt()

        if not claims['admin']:
            return {'message': 'You need admin privileges'}, 401

        data = Item.parser.parse_args()
        if ItemModel.find_by_name(data['name']):
            return {'message': "An item with name '{}' already exists.".format(name)}, 400


        item = ItemModel(**data)

        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return item.json(), 201
Exemplo n.º 25
0
    def post(self, name):
        if ItemModel.findItemByName(name):
            return {
                "message":
                "an item with name {} already exists in the itemList.".format(
                    name)
            }

        data = Item.parser.parse_args()

        # item=ItemModel(name,data['price'],data['store_id'])
        '''way2'''
        item = ItemModel(name, **data)
        # print(item.name)
        try:
            # print(item.name)
            item.save_to_db()
        except:
            return {"message": "An error occured inserting the item"}, 500

        return item.json(), 201
Exemplo n.º 26
0
 def get(self, name):
     item = ItemModel.findItemByName(name)
     if item:
         return item.json()
     return {"message": "item not found"}, 404
Exemplo n.º 27
0
    def get(self):

        items = ItemModel.find_all()
        print(items)
        return ({'items': [k.json() for k in items]})
Exemplo n.º 28
0
 def get(self):
     all_items = ItemModel.find_all() or []
     all_in_json = [item.to_json() for item in all_items]
     return {"items": all_in_json}, 200
Exemplo n.º 29
0
    def get(self, item_id):
        errors = validate(item_id=item_id)
        if len(errors) > 0:
            return {"errors": [error.to_json() for error in errors]}, 422

        return ItemModel.find_by_id(item_id).to_json()
Exemplo n.º 30
0
 def get(self, name):
     data = Item.parser.parse_args()
     returned_item = ItemModel.find_by_name(name, data['store_id'])
     return (returned_item.json(), 200 if returned_item else 404)