def add_expense(): """ Adds or edits an expense. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) fn = None # Make it mutable. args = {k: v for (k, v) in request.form.items() if v is not ''} table = args.pop('table', None) if not args.get('name'): return jsonify(data={}, errors=['Item name is required.']) if table == 'current': fn = add_current elif table == 'future': fn = add_future elif table == 'history': fn = add_history else: error(f'Attempted to edit an invalid table {table}.') try: print(f'Adding {table} expense: {args}') fn(current_user, args) except Exception as e: error(str(e)) return jsonify(errors=get_errors())
def load_table(): """ Return all current expenses. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) data = {} fn = None table = request.args.get('table', None) if table == 'current': fn = current_table elif table == 'future': fn = future_table elif table == 'history': fn = partial(historical_table, page=int(request.args.get('page', '1'))) if fn: try: data[table] = fn(current_user) data['total'] = table == 'current' and current_user.formatted_total except Exception as e: error(e) else: error(f'Attempted to load invalid table {table}.') return jsonify(data=data, errors=get_errors())
def send_back(): """ Move an expense from History back to Current. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) try: print(f'Sending expense back to current: {request.form}') history_to_current(current_user,request.form.get('id', None, type=int)) except Exception as e: error(str(e)) return jsonify(errors=get_errors())
def advance(): """ Move an expense from Future to Current. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) try: print(f'Advancing future expense: {request.form.get("id")}') advance_future(current_user, request.form.get('id', None, type=int)) except Exception as e: error(str(e)) return jsonify(errors=get_errors())
def settle(): """ Move an expense from Current to History. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) try: print(f'Settling current expense: {request.form.get("id")}') advance_current(current_user, request.form.get('id', None, type=int)) except Exception as e: error(str(e)) return jsonify(errors=get_errors())
def total(): """ Return the total value (in local currency) of all current expenses. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) data = None try: data = current_user.formatted_total except Exception as e: error(str(e)) return jsonify(data=data, errors=get_errors())
def delete(): """ Delete an expense. """ if current_user is None or not current_user.is_authenticated: return jsonify(data={}, errors=['User log in.'], reload=True) table = request.form.get('table', None) if table == 'current': fn = delete_current elif table == 'future': fn = delete_future elif table == 'history': fn = delete_history try: print(f'Deleting {table} expense: {request.form.get("id")}') fn(current_user, request.form.get('id', None, type=int)) except Exception as e: error(str(e)) return jsonify(errors=get_errors())