Added categry table to website
This commit is contained in:
parent
1f54614ae7
commit
851ecdbc57
4 changed files with 52 additions and 9 deletions
22
serve.py
22
serve.py
|
@ -2,6 +2,8 @@
|
||||||
from bottle import route, run, template, redirect, request, post
|
from bottle import route, run, template, redirect, request, post
|
||||||
from models import Category, Transaction
|
from models import Category, Transaction
|
||||||
from helper import get_session
|
from helper import get_session
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy import func
|
||||||
import click
|
import click
|
||||||
|
|
||||||
session = None
|
session = None
|
||||||
|
@ -9,13 +11,31 @@ session = None
|
||||||
|
|
||||||
@route("/")
|
@route("/")
|
||||||
def index():
|
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")
|
@route("/categories")
|
||||||
def categories():
|
def categories():
|
||||||
categories = session.query(Category).all()
|
categories = session.query(Category).all()
|
||||||
categories = [c for c in categories if c.is_child()]
|
categories = [c for c in categories if c.is_child()]
|
||||||
|
categories = sorted(categories, key=lambda i: i.full_name())
|
||||||
return template("categories", categories=categories)
|
return template("categories", categories=categories)
|
||||||
|
|
||||||
|
|
||||||
|
|
1
sort.py
1
sort.py
|
@ -7,6 +7,7 @@ import click
|
||||||
from prompt_toolkit.completion import FuzzyWordCompleter
|
from prompt_toolkit.completion import FuzzyWordCompleter
|
||||||
from prompt_toolkit.shortcuts import prompt
|
from prompt_toolkit.shortcuts import prompt
|
||||||
|
|
||||||
|
|
||||||
@click.command(name="sort")
|
@click.command(name="sort")
|
||||||
@click.option("--profile", "-p")
|
@click.option("--profile", "-p")
|
||||||
def command(profile):
|
def command(profile):
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
% rebase("base.tpl")
|
% rebase("base.tpl")
|
||||||
|
|
||||||
List of categories:
|
<h1 class="text-2xl font-bold text-indigo-500">Category overview</h1>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
% for c in categories:
|
% for c in categories:
|
||||||
<li>
|
<li class="p-2">
|
||||||
<a href="/category/{{ c.name }}">{{ c.full_name() }}</a>
|
{{ c.full_name() }}
|
||||||
<a class="text-red-600 ml-2" href="/category/{{ c.name }}/delete">❌</a>
|
<a class="float-right" href="/category/{{ c.name }}">
|
||||||
|
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold mx-2 y-1 px-2 rounded"> Transactions </button>
|
||||||
|
</a>
|
||||||
|
<a class="float-right" href="/category/{{ c.name }}/delete">
|
||||||
|
<button class="bg-red-500 hover:bg-red-700 text-white font-bold y-1 px-2 rounded"> Delete </button>
|
||||||
|
</a>
|
||||||
</li>
|
</li>
|
||||||
% end
|
% end
|
||||||
</ul>
|
</ul>
|
|
@ -1,3 +1,19 @@
|
||||||
% rebase("base.tpl")
|
% rebase("base.tpl")
|
||||||
|
|
||||||
<h1 class="text-2xl font-bold text-indigo-500">Hello World</h1>
|
<h1 class="text-2xl font-bold text-indigo-500">Monatsübersicht</h1>
|
||||||
|
|
||||||
|
<table class="table-auto border-none">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th class="px-4 py-2">Categorie</th>
|
||||||
|
<th class="px-4 py-2 text-right">Summe</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
% for c in categories:
|
||||||
|
<tr>
|
||||||
|
<td class="border px-4 py-2">{{ c["name"] }}</td>
|
||||||
|
<td class="border px-4 py-2 text-right {{ 'text-green-500' if c['amount'] > 0 else 'text-red-500' }}">{{ c["amount"] }} €</td>
|
||||||
|
</tr>
|
||||||
|
% end
|
||||||
|
</tbody>
|
Loading…
Add table
Reference in a new issue