diff --git a/cli b/cli index 4e7d1e7..18c5a79 100755 --- a/cli +++ b/cli @@ -5,7 +5,7 @@ import sys from pathlib import Path import importer -# import serve +import serve import autosort import validate # import info @@ -47,7 +47,7 @@ if __name__ == '__main__': create_dirs() # cli.add_command(sort.command) cli.add_command(importer.command) - # cli.add_command(serve.command) + cli.add_command(serve.command) cli.add_command(autosort.command) cli.add_command(validate.command) # cli.add_command(info.command) diff --git a/models.py b/models.py index 753fd9b..9e1c253 100644 --- a/models.py +++ b/models.py @@ -2,14 +2,16 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Table from sqlalchemy.orm import relationship, backref +from helper import format_amount +from hashlib import md5 Base = declarative_base() association_table = Table( "association", Base.metadata, - Column("left_id", Integer, ForeignKey("Transaction.id")), - Column("right_id", Integer, ForeignKey("Tag.id")), + Column("transaction_id", Integer, ForeignKey("Transaction.id")), + Column("tag_id", Integer, ForeignKey("Tag.id")), ) @@ -47,3 +49,7 @@ class Tag(Base): def __repr__(self): return f"" + + def color(self): + hash = md5(self.name.encode()).hexdigest()[-6:] + return f"#{hash}" diff --git a/serve.py b/serve.py index 2b657f5..c0e9e85 100644 --- a/serve.py +++ b/serve.py @@ -1,5 +1,5 @@ #! /usr/bin/env python3 -from flask import Flask, render_template, request +from flask import Flask, render_template, request, redirect from helper import get_session, format_amount from datetime import datetime from sqlalchemy import func @@ -12,84 +12,58 @@ app = Flask(__name__) @app.route("/") def index(): - fom = datetime.today().replace(day=1) - sum_of_categories = ( - session.query(Transaction.category_id, func.sum(Transaction.amount).label("total")) - .filter(Transaction.date > fom) - .group_by(Transaction.category_id) - .all() - ) - categories = [] - for s in sum_of_categories: - if s.category_id: - categories.append( - {"name": session.query(Category).get(s.category_id).full_name(), "amount": format_amount(s.total)} - ) - else: - categories.append({"name": "Unsorted", "amount": format_amount(s.total)}) - - categories = sorted(categories, key=lambda i: i["name"]) - return render_template("index.html", categories=categories) + return render_template("index.html") -@app.route("/categories") -def categories(): - categories = session.query(Category).all() - categories = [c for c in categories if c.is_child()] - categories = sorted(categories, key=lambda i: i.full_name()) - return render_template("categories.html", categories=categories) +@app.route("/tags") +def tags(): + tags = session.query(Tag).all() + return render_template("tags.html", tags=tags) -@app.route("/graph") -def graph(): - data = [] - return render_template("graph.html", data=data) +@app.route("/tag/") +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("/category/") -def category(name): - c = session.query(Category).filter(Category.name == name).first() - if c: - transactions = session.query(Transaction).filter(Transaction.category_id == c.id).all() - else: - transactions = [] - - return render_template("category.html", category=c, transactions=transactions) - - -@app.route("/category//delete") -def delete_category(name): - c = session.query(Category).filter(Category.name == name).first() - if c: - session.delete(c) +@app.route("/tag//delete") +def delete_tag(name): + x = session.query(Tag).filter(Tag.name == name).first() + if x: + session.delete(x) session.commit() - return redirect("/categories") + return redirect("/tags") @app.route("/transactions") def transactions(): + transactions = session.query(Transaction) + if request.args.get("unsorted", None): - print("Unsorted") - transactions = session.query(Transaction).filter(Transaction.category_id == None).all() - else: - print("all") - transactions = session.query(Transaction).all() - categories = session.query(Category).all() - childs = [c for c in categories if c.is_child()] - return render_template("transactions.html", transactions=transactions, categories=childs) + 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") +@app.route("/bulksort", methods=["POST"]) def buksort(): - cid = request.forms.get("category") - transaction_ids = request.forms.getall("transaction") + 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.category__id = cid + t.tags.append(tag) session.add(t) - print(f"Sorted {len(transaction_ids)} transactions into category {cid}") session.commit() + return redirect("/transactions") diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..e69de29 diff --git a/templates/base.html b/templates/base.html index e667d1d..b114fd6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -8,6 +8,7 @@ Schmeckels + @@ -16,17 +17,14 @@
  • SCHMECKELS
  • -
  • +
  • Transactions
  • -
  • +
  • Unsorted Transactions
  • -
  • - Categories -
  • -
  • - Monthly Graph +
  • + Tags
  • diff --git a/templates/graph.html b/templates/graph.html deleted file mode 100644 index 91f5ff2..0000000 --- a/templates/graph.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "base.html" %} - -{% block main %} - -

    Last 12 months

    - -{% endblock %} diff --git a/templates/index.html b/templates/index.html index 8323fe4..fee384d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,28 +1,7 @@ {% extends "base.html" %} {% block main %} -

    Monatsübersicht

    +

    Start

    -{% if categories|length > 0 %} - - - - - - kk - - - {% for c in categories %} - - - - - {% endfor %} - -{% else %} - -No transactions this month - -{% endif %} {% endblock %} diff --git a/templates/category.html b/templates/tag.html similarity index 76% rename from templates/category.html rename to templates/tag.html index db23cf2..2aabff1 100644 --- a/templates/category.html +++ b/templates/tag.html @@ -2,16 +2,15 @@ {% block main %} -

    Transaktionen aus {{ category.full_name() }}

    +

    Transaktionen aus {{ tag.name }}

    CategorieSumme
    {{ c["name"] }}{{ c["amount"] }} €
    - + - @@ -21,8 +20,9 @@ - {% endfor %}
    DateDatum Sender/Empfänger Verwendungszweck Betrag
    {{ t.name }} {{ t.description }} {{ t.pretty_amount() }}Remove category
    + +{% endblock %} diff --git a/templates/categories.html b/templates/tags.html similarity index 53% rename from templates/categories.html rename to templates/tags.html index 8864193..b54ee14 100644 --- a/templates/categories.html +++ b/templates/tags.html @@ -2,16 +2,16 @@ {% block main %} -

    Category overview

    +

    Tag overview

      - {% for c in categories %} + {% for tag in tags %}
    • - {{ c.full_name() }} - + {{ tag.name }} + - +
    • diff --git a/templates/transactions.html b/templates/transactions.html index 4ee4654..5fad594 100644 --- a/templates/transactions.html +++ b/templates/transactions.html @@ -3,9 +3,9 @@ {% block main %}
      - + {% for tag in tags %} + {% endfor %} @@ -28,11 +28,11 @@ {{ t.name }}
      {{t.iban}} {{ t.description }} {{ t.pretty_amount() }} € - {% if t.category %} - {{ t.category.full_name()}} - {% else %} - - - {% endif %} + + {% for tag in t.tags %} + {{ tag.name }} + {% endfor %} + {% endfor %}