def alerts_settings(user_id): """ Alerts with settings for Ajustes - module """ try: my_user = storage.getobject(User, "id", user_id, "Dict")["User." + str(user_id)] except Exception: return jsonify({"Error": "User id not found"}) try: list_catsu = my_user.categories_sub list_alert = [] for catsu in list_catsu: if catsu['CategoryMain_id'] == 2: list_tempa = storage.getobject(CategorySub, "id", catsu['id'], "Dict")["CategorySub." + str(catsu['id'])].alerts for tempa in list_tempa: list_alert.append(tempa) list_alertsetting = [] for al in list_alert: alert_dict = {} alert_dict['id'] = al['id'] alert_dict['limit'] = al['mileage_limit'] alert_dict['high'] = al['high'] alert_dict['middle'] = al['middle'] alert_dict['low'] = al['low'] alert_dict['catsub_name'] = storage.getobject( CategorySub, "id", al['CategorySub_id'], "Dict")["CategorySub." + str(al['CategorySub_id'])].name list_alertsetting.append(alert_dict) return jsonify(list_alertsetting) except Exception: abort(404)
def expenses_user(user_id): """ Expenses by user """ try: my_user = storage.getobject(User, "id", user_id, "Dict")["User." + str(user_id)] except Exception: return jsonify({"Error": "User id not found"}) try: list_catsu = my_user.categories_sub list_expen = [] dict_name = {} for catsu in list_catsu: list_tempa = storage.getobject(CategorySub, "id", catsu['id'], "Dict")["CategorySub." + str(catsu['id'])].expenses dict_name[catsu['id']] = storage.getobject( CategorySub, "id", catsu['id'], "Dict")["CategorySub." + str(catsu['id'])].name for tempa in list_tempa: list_expen.append(tempa) for expe in list_expen: if expe['CategorySub_id'] in dict_name: expe['catsu_name'] = dict_name[expe['CategorySub_id']] return jsonify(list_expen) except Exception: abort(404)
def alerts_panel(user_id): """ alerts with colors for main Pannel - Module """ try: my_user = storage.getobject(User, "id", user_id, "Dict")["User." + str(user_id)] except Exception: return jsonify({"Error": "User id not found"}) list_catsu = my_user.categories_sub list_alert = [] #Alerts list from Cat Main type 2 for catsu in list_catsu: if catsu['CategoryMain_id'] == 2: list_tempa = storage.getobject(CategorySub, "id", catsu['id'], "Dict")["CategorySub." + str(catsu['id'])].alerts for tempa in list_tempa: list_alert.append(tempa) #Identify the color, missing mileage, catsub_name for every alert in main panel list_alertpannel = [] for al in list_alert: alert_dict = {} alert_dict['missing'] = al['mileage_limit'] - al['mileage_act'] alert_dict['color'] = return_color(al['mileage_act'], al['high'], al['middle']) alert_dict['catsub_name'] = storage.getobject( CategorySub, "id", al['CategorySub_id'], "Dict")["CategorySub." + str(al['CategorySub_id'])].name list_alertpannel.append(alert_dict) return jsonify(list_alertpannel)
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 list_categories_sub(user_id=None): """ Return all categories_sub for a user """ try: myuser = storage.getobject(User, "id", user_id, "Dict")["User." + str(user_id)] return jsonify(myuser.categories_sub) except Exception: abort(404)
def delete_expense(exp_id): """ Delete an object """ try: my_obj = storage.getobject(Expense, "id", exp_id, "Dict")["Expense." + str(exp_id)] storage.delete(my_obj) return jsonify({}) except Exception: abort(404)
def delete_object(user_id): """ Delete an user """ try: obj = storage.getobject(User, "id", user_id, "Dict")["User." + str(user_id)] storage.delete(obj) return jsonify({}) except Exception: abort(404)
def delete_alert(alert_id): """ Delete an alert """ try: my_alert = storage.getobject(Alert, "id", alert_id, "Dict")["Alert." + str(alert_id)] storage.delete(my_alert) return jsonify({}) except Exception: abort(404)
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 create_category_sub(user_id, catm_id): """ Create a CategorySub """ try: data = request.get_json(force=True) #validations if 'name' not in data: return jsonify({"error": "missing name for CategorySub"}), 404 if len(storage.getobject(CategoryMain, "id", catm_id)) == 0: return jsonify({"error": "CategoryMain id not found"}), 404 if len(storage.getobject(User, "id", user_id)) == 0: return jsonify({"error": "User id not found"}), 404 #new object new_obj = CategorySub() set_object(new_obj, **data) new_obj.CategoryMain_id = catm_id new_obj.User_id = user_id storage.insert(new_obj) return jsonify(new_obj.to_dict()), 201 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)
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 delete_category_sub(cats_id): """ Delete a category_sub """ try: obj = storage.getobject(CategorySub, "id", cats_id, "Dict")["CategorySub." + str(cats_id)] # Delete associated alerts if obj.CategoryMain_id == 2: alerts = obj.alerts for al in alerts: storage.delete( storage.getobject(Alert, "id", al['id'], "Dict")["Alert." + str(al['id'])]) # Delete associated expenses expenses = obj.expenses for ex in expenses: storage.delete( storage.getobject(Expense, "id", ex['id'], "Dict")["Expense." + str(ex['id'])]) # Delete CategorySub storage.delete(obj) return jsonify({}) 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 create_alert(cats_id): """ Create an alert """ try: if len(storage.getobject(CategorySub, "id", cats_id)) == 0: return jsonify({"Error": "CategorySub id not found"}) data = request.get_json(force=True) for at in attr: if at not in data.keys(): return jsonify({"Error": "Missing " + at}) new_obj = Alert() set_obj(new_obj, **data) new_obj.low = (new_obj.middle - 1) new_obj.CategorySub_id = cats_id storage.insert(new_obj) return jsonify(new_obj.to_dict()) except Exception: abort(404)
def create_expense(cats_id): """ Create an expense """ try: if len(storage.getobject(CategorySub, "id", cats_id)) == 0: return jsonify({"Error": "CategorySub id not found"}), 404 data = request.get_json(force=True) for at in attr: if at not in data.keys(): return jsonify({"Error": str("Missing " + at)}) new_obj = Expense() set_obj(new_obj, **data) new_obj.CategorySub_id = cats_id storage.insert(new_obj) #ActualizaciĆ³n de kilometraje alertas second_last = storage.get_second_last(Expense) print("new_obj ", new_obj.mileage) act_alerts(new_obj.mileage, second_last.mileage) return jsonify(new_obj.to_dict()) except Exception: abort(404)