def act_alerts(new, last): """ Set the alerts with last milleage """ val = new - last alerts = storage.getobject(Alert) for al in alerts: obj = storage.getobject(Alert, "id", al['id'], "Dict")["Alert." + str(al['id'])] setattr(obj, "mileage_act", obj.mileage_act + val) storage.commit()
def update_expense(exp_id): """ Update an expense """ try: my_obj = storage.getobject(Expense, "id", exp_id, "Dict")["Expense." + str(exp_id)] data = request.get_json(force=True) set_obj(my_obj, **data) storage.commit() return jsonify(my_obj.to_dict()) except Exception: abort(404)
def update_alert(alert_id): """ Update an alert """ try: my_alert = storage.getobject(Alert, "id", alert_id, "Dict")["Alert." + str(alert_id)] data = request.get_json(force=True) set_obj(my_alert, **data) if 'high' or 'middle' in data: my_alert.low = (my_alert.middle - 1) storage.commit() return jsonify(my_alert.to_dict()) except Exception: abort(404)
def update_user(user_id): """ updating a user """ try: data = request.get_json(force=True) # attributes validation for key in data.keys(): if key not in attr: return jsonify({"Error": "Invalid action"}), 404 obj = storage.get(User, user_id) set_obj(obj, **data) storage.commit() return jsonify(obj.to_dict()) except Exception: abort(404)
def update_comment(comment_id): """ updating a comment """ try: data = request.get_json(force=True) # attributes validation for key in data.keys(): if key != 'text': return jsonify({"Error": "Invalid action"}), 404 obj = storage.get(Comment, comment_id) obj.text = data['text'] storage.commit() return jsonify(obj.to_dict()) except Exception: abort(404)
def update_user(user_id): """ Update an user """ try: data = request.get_json(force=True) for key in data.keys(): if key not in attr: return jsonify({"error": "Invalid action"}), 404 obj = storage.getobject(User, "id", user_id, "Dict")["User." + str(user_id)] set_obj(obj, **data) storage.commit() return jsonify(obj.to_dict()) except Exception: abort(404)
def update_category_sub(cats_id): """ Update a CategorySub """ """ The User_id it can't be modified """ try: obj = storage.getobject(CategorySub, "id", cats_id, "Dict")["CategorySub." + str(cats_id)] except Exception: return jsonify({"error": "CategorySub id not found"}) try: data = request.get_json(force=True) if 'CategoryMain_id' in data: if len( storage.getobject(CategoryMain, "id", data['CategoryMain_id'])) == 0: return jsonify({"error": "CategoryMain id not found"}), 404 if 'User_id' in data: return jsonify({"error": "User id - invalid action"}), 404 set_object(obj, **data) storage.commit() return jsonify(obj.to_dict()), 200 except Exception: abort(404)