From 851ecdbc57b69ac5a332493dd086b2b4a4b274f9 Mon Sep 17 00:00:00 2001 From: fleaz Date: Mon, 30 Mar 2020 21:24:11 +0200 Subject: [PATCH] Added categry table to website --- serve.py | 22 +++++++++++++++++++++- sort.py | 1 + views/categories.tpl | 20 +++++++++++++------- views/index.tpl | 18 +++++++++++++++++- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/serve.py b/serve.py index 3a82a80..a3ea3e5 100644 --- a/serve.py +++ b/serve.py @@ -2,6 +2,8 @@ from bottle import route, run, template, redirect, request, post from models import Category, Transaction from helper import get_session +from datetime import datetime +from sqlalchemy import func import click session = None @@ -9,13 +11,31 @@ session = None @route("/") def index(): - return template("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": s.total / 100,} + ) + else: + categories.append({"name": "Unsorted", "amount": s.total / 100}) + + categories = sorted(categories, key=lambda i: i["name"]) + return template("index", categories=categories) @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 template("categories", categories=categories) diff --git a/sort.py b/sort.py index b17a675..079ce9f 100644 --- a/sort.py +++ b/sort.py @@ -7,6 +7,7 @@ import click from prompt_toolkit.completion import FuzzyWordCompleter from prompt_toolkit.shortcuts import prompt + @click.command(name="sort") @click.option("--profile", "-p") def command(profile): diff --git a/views/categories.tpl b/views/categories.tpl index ce6b69f..e40f96d 100644 --- a/views/categories.tpl +++ b/views/categories.tpl @@ -1,11 +1,17 @@ % rebase("base.tpl") -List of categories: +

Category overview

+ + % end + \ No newline at end of file diff --git a/views/index.tpl b/views/index.tpl index 07bc924..5084148 100644 --- a/views/index.tpl +++ b/views/index.tpl @@ -1,3 +1,19 @@ % rebase("base.tpl") -

Hello World

+

Monatsübersicht

+ + + + + + + + + +% for c in categories: + + + + +% end + \ No newline at end of file
CategorieSumme
{{ c["name"] }}{{ c["amount"] }} €