75 lines
1.8 KiB
Python
75 lines
1.8 KiB
Python
#! /usr/bin/env python3
|
|
from flask import Flask, render_template, request, redirect
|
|
from helper import get_session, format_amount
|
|
from datetime import datetime
|
|
from sqlalchemy import func
|
|
import click
|
|
from models import Transaction, Tag
|
|
|
|
session = None
|
|
app = Flask(__name__)
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
|
|
@app.route("/tags")
|
|
def tags():
|
|
tags = session.query(Tag).all()
|
|
return render_template("tags.html", tags=tags)
|
|
|
|
|
|
@app.route("/tag/<name>")
|
|
def tag(name):
|
|
tag = session.query(Tag).filter(Tag.name == name).first()
|
|
transactions = session.query(Transaction).filter(Transaction.tags.any(id=tag.id)).all()
|
|
|
|
return render_template("tag.html", tag=tag, transactions=transactions)
|
|
|
|
|
|
@app.route("/tag/<name>/delete")
|
|
def delete_tag(name):
|
|
x = session.query(Tag).filter(Tag.name == name).first()
|
|
if x:
|
|
session.delete(x)
|
|
session.commit()
|
|
|
|
return redirect("/tags")
|
|
|
|
|
|
@app.route("/transactions")
|
|
def transactions():
|
|
transactions = session.query(Transaction)
|
|
|
|
if request.args.get("unsorted", None):
|
|
transactions = transactions.filter(Transaction.tags == None)
|
|
|
|
transactions = transactions.all()
|
|
|
|
tags = session.query(Tag).all()
|
|
return render_template("transactions.html", transactions=transactions, tags=tags)
|
|
|
|
|
|
@app.route("/bulksort", methods=["POST"])
|
|
def buksort():
|
|
tag_id = request.form.get("tag")
|
|
tag = session.query(Tag).get(tag_id)
|
|
transaction_ids = request.form.getlist("transaction")
|
|
|
|
for id in transaction_ids:
|
|
t = session.query(Transaction).get(id)
|
|
t.tags.append(tag)
|
|
session.add(t)
|
|
session.commit()
|
|
|
|
return redirect("/transactions")
|
|
|
|
|
|
@click.command(name="serve")
|
|
@click.option("--profile", "-p")
|
|
def command(profile):
|
|
global session
|
|
session = get_session(profile)
|
|
app.run(host="0.0.0.0", port=8080)
|