def get_transactions_for_user(user_id): year = request.args.get("year") month = request.args.get("month") currency = request.args.get("currency") if currency is None: abort(400, "No currency specified") if year is not None and month is None: # get all transactions for specified year t_months = TransactionMonth.query.filter( extract("year", TransactionMonth.date) == int(year), TransactionMonth.user_id == user_id, ).all() if t_months is None: abort(400, "No transactions exist for this user in the specified year") transactions = [] for t_month in t_months: transactions.extend( filter(lambda t: t.currency == currency, t_month.transactions)) return jsonify(Transaction.serialize_list(transactions)) if year is None and month is None: # return all transactions for user transactions = Transaction.query.filter_by(user_id=user_id, currency=currency).all() return jsonify(Transaction.serialize_list(transactions)) if year is None and month is not None: abort(400, "Must specify a year with optional month") # get transactions for specified month and year t_month = TransactionMonth.query.filter( extract("year", TransactionMonth.date) == int(year), extract("month", TransactionMonth.date) == int(month), TransactionMonth.user_id == user_id, ).first() if t_month is None: return jsonify([]) transactions = filter(lambda t: t.currency == currency, t_month.transactions) return jsonify(Transaction.serialize_list(transactions))
def get_transactions(): transactions = Transaction.query.all() return jsonify(Transaction.serialize_list(transactions))