Skip to content

Commit

Permalink
Merge pull request #22 from EnhancedJax/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EnhancedJax authored Dec 23, 2024
2 parents d8f387c + 97a3e64 commit 5afec04
Show file tree
Hide file tree
Showing 34 changed files with 3,456 additions and 239 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.1.16

- Updated bar visuals for non-rounded variation
- WIP Budgets
- WIP Tests

## 0.1.15

- Fix: Deleted categories visible
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "Bagels"
version = "0.1.15"
version = "0.1.16"
authors = [
{ name = "Jax", email = "enhancedjax@gmail.com" }
]
Expand All @@ -19,7 +19,6 @@ dependencies = [
"blinker==1.8.2",
"click-default-group>=1.2.4",
"click==8.1.7",
"freezegun>=1.5.1",
"frozenlist==1.5.0",
"idna==3.10",
"itsdangerous==2.2.0",
Expand Down Expand Up @@ -73,6 +72,7 @@ dev-dependencies = [
"pytest-xdist>=3.6.1",
"pytest-cov>=5.0.0",
"pytest-textual-snapshot==1.0.0",
"time-machine==2.16.0"
]

[tool.hatch.metadata]
Expand Down
21 changes: 16 additions & 5 deletions src/bagels/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class App(TextualApp):
BINDINGS = [
(CONFIG.hotkeys.toggle_jump_mode, "toggle_jump_mode", "Jump Mode"),
(CONFIG.hotkeys.home.categories, "go_to_categories", "Categories"),
(CONFIG.hotkeys.home.budgets, "go_to_budgets", "Budgets"),
("ctrl+q", "quit", "Quit"),
]
COMMANDS = {AppProvider}
Expand All @@ -44,11 +45,12 @@ class App(TextualApp):
"""True if 'jump mode' is currently active, otherwise False."""

# region init
def __init__(self):
def __init__(self, is_testing=False) -> None:
# Initialize available themes with a default
available_themes: dict[str, Theme] = {"galaxy": BUILTIN_THEMES["galaxy"]}
available_themes |= BUILTIN_THEMES
self.themes = available_themes
self.is_testing = is_testing
super().__init__()

# Get package metadata directly
Expand Down Expand Up @@ -189,12 +191,16 @@ def on_tabs_tab_activated(self, event: Tabs.TabActivated) -> None:
self.mount(page_instance)

def on_resize(self, event: events.Resize) -> None:
console_size: Size = self.console.size
console_size: Size = event.size
aspect_ratio = (console_size.width / 2) / console_size.height
if aspect_ratio < 1:
self.layout = "v"
else:
self.layout = "h"
if self.is_testing:
self.query_one(".version").update(
"Layout: " + self.layout + " " + str(aspect_ratio)
)

# region callbacks
# --------------- Callbacks ------------ #
Expand All @@ -210,15 +216,20 @@ def action_quit(self) -> None:
def action_go_to_categories(self) -> None:
self.push_screen(CategoriesModal(), callback=self.on_categories_dismissed)

def action_go_to_budgets(self) -> None:
self.notify("Work in progress!", title="Budgets")

def on_categories_dismissed(self, _) -> None:
self.app.refresh(recompose=True)

# region view
# --------------- View --------------- #
def compose(self) -> ComposeResult:
version = self.project_info["version"] if not self.is_testing else "vt"
user_host = get_user_host_string() if not self.is_testing else "test"
with Container(classes="header"):
yield Label(f"↪ {self.project_info['name']}", classes="title")
yield Label(self.project_info["version"], classes="version")
yield Label(get_user_host_string(), classes="user")
yield Label(f"↪ {self.project_info["name"]}", classes="title")
yield Label(version, classes="version")
yield Label(user_host, classes="user")
yield Home(classes="content")
yield Footer()
1 change: 1 addition & 0 deletions src/bagels/components/jump_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def on_key(self, key_event: events.Key) -> None:
CONFIG.hotkeys.new,
CONFIG.hotkeys.edit,
CONFIG.hotkeys.delete,
CONFIG.hotkeys.home.new_transfer,
]:
key_event.stop()
key_event.prevent_default()
Expand Down
7 changes: 5 additions & 2 deletions src/bagels/components/modules/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _format_record_fields(self, record, flow_icon: str) -> tuple[str, str]:

if record.splits and not self.show_splits:
amount_self = round(
record.amount - get_record_total_split_amount(record.id)
record.amount - get_record_total_split_amount(record.id), 2
)
amount_string = f"{flow_icon} {amount_self}"
else:
Expand All @@ -234,7 +234,10 @@ def _add_group_header_row(

def _add_split_rows(self, table: DataTable, record, flow_icon: str) -> None:
color = record.category.color.lower()
amount_self = round(record.amount - get_record_total_split_amount(record.id), 2)
amount_self = round(
record.amount - get_record_total_split_amount(record.id),
CONFIG.defaults.round_decimals,
)
split_flow_icon = (
f"[red]{CONFIG.symbols.amount_negative}[/red]"
if record.isIncome
Expand Down
2 changes: 1 addition & 1 deletion src/bagels/components/modules/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def check_result(result) -> None:
else:
self.app.notify(
title="Success",
message=f"Template created",
message=f"Template edited",
severity="information",
timeout=3,
)
Expand Down
5 changes: 4 additions & 1 deletion src/bagels/components/modules/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def __init__(self, *args, **kwargs) -> None:
self.welcome_text = file.read()

def on_mount(self) -> None:
self.set_interval(1 / 10, self.update_bagel)
if not self.app.is_testing:
self.set_interval(1 / 10, self.update_bagel)
else:
self.update_bagel()

def update_bagel(self) -> None:
bagel = self.query_one("#bagel")
Expand Down
7 changes: 6 additions & 1 deletion src/bagels/components/percentage_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,20 @@ def rebuild(self) -> None:
width = "1fr"

item_widget.styles.width = width
item_widget.styles.background = background_color
if self.rounded:
item_widget.background = background_color
if i > 0:
prev_background_color = Color.from_rich_color(
RichColor.parse(self.items[i - 1].color)
).hex
item_widget.update(
f"[{prev_background_color} on {background_color}][/{prev_background_color} on {background_color}]"
)
else:
item_widget.styles.hatch = (
"/",
background_color,
)

self.bar.mount(item_widget)

Expand Down
2 changes: 2 additions & 0 deletions src/bagels/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Defaults(BaseModel):
period: Literal["day", "week", "month", "year"] = "week"
first_day_of_week: int = Field(ge=0, le=6, default=6)
date_format: str = "%d/%m"
round_decimals: int = 2


class DatemodeHotkeys(BaseModel):
Expand All @@ -19,6 +20,7 @@ class DatemodeHotkeys(BaseModel):

class HomeHotkeys(BaseModel):
categories: str = "c"
budgets: str = "b"
new_transfer: str = "t"
toggle_splits: str = "s"
display_by_date: str = "q"
Expand Down
6 changes: 5 additions & 1 deletion src/bagels/forms/record_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rich.text import Text

from bagels.components.autocomplete import Dropdown
from bagels.config import CONFIG
from bagels.managers.accounts import get_all_accounts_with_balance
from bagels.managers.categories import get_all_categories_by_freq
from bagels.managers.persons import create_person, get_all_persons
Expand Down Expand Up @@ -198,7 +199,10 @@ def get_filled_form(self, recordId: int) -> tuple[list, list]:
match fieldKey:
case "amount":
field.default_value = str(
round(value - get_record_total_split_amount(recordId), 2)
round(
value - get_record_total_split_amount(recordId),
CONFIG.defaults.round_decimals,
)
)
case "date":
# if value is this month, simply set %d, else set %d %m %y
Expand Down
10 changes: 4 additions & 6 deletions src/bagels/index.tcss
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,6 @@ Button {
height: auto;
width: 1fr;
padding: 0 2;

&.v {
layout: vertical;

}

&.h {
layout: horizontal;
Expand All @@ -552,7 +547,10 @@ Button {
width: 60%
}
}


&.v {
layout: vertical;
}
}

.module-container {
Expand Down
3 changes: 2 additions & 1 deletion src/bagels/managers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sqlalchemy import select
from sqlalchemy.orm import sessionmaker

from bagels.config import CONFIG
from bagels.models.account import Account
from bagels.models.record import Record
from bagels.models.split import Split
Expand Down Expand Up @@ -74,7 +75,7 @@ def get_account_balance(accountId, session=None):
else:
balance += split.amount

return round(balance, 2)
return round(balance, CONFIG.defaults.round_decimals)
finally:
if should_close:
session.close()
Expand Down
8 changes: 5 additions & 3 deletions src/bagels/managers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def get_start_end_of_period(offset: int = 0, offset_type: str = "month"):
# -------------- figure -------------- #


def get_period_figures(accountId=None, offset_type=None, offset=None, isIncome=None, session=None):
def get_period_figures(
accountId=None, offset_type=None, offset=None, isIncome=None, session=None
):
"""Returns the income / expense for a given period.
Rules:
Expand Down Expand Up @@ -142,7 +144,7 @@ def get_period_figures(accountId=None, offset_type=None, offset=None, isIncome=N
else:
total -= record_amount

return abs(round(total, 2))
return abs(round(total, CONFIG.defaults.round_decimals))
finally:
if should_close:
session.close()
Expand All @@ -160,4 +162,4 @@ def _get_days_in_period(offset: int = 0, offset_type: str = "month"):

def get_period_average(net: int = 0, offset: int = 0, offset_type: str = "month"):
days = _get_days_in_period(offset, offset_type)
return round(net / days, 2)
return round(net / days, CONFIG.defaults.round_decimals)
17 changes: 17 additions & 0 deletions src/bagels/models/budget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, String, Float, Boolean
from sqlalchemy.orm import relationship
from .database.db import Base


class Budget(Base):
__tablename__ = "budget"

createdAt = Column(DateTime, nullable=False, default=datetime.now)
updatedAt = Column(
DateTime, nullable=False, default=datetime.now, onupdate=datetime.now
)

id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
amount = Column(Float, nullable=False)
1 change: 1 addition & 0 deletions src/bagels/models/database/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from bagels.models.record import Record
from bagels.models.record_template import RecordTemplate
from bagels.models.split import Split
from bagels.models.budget import Budget

from bagels.models.database.db import Base

Expand Down
6 changes: 5 additions & 1 deletion src/bagels/textualrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from bagels.locations import set_custom_root

if __name__ == "__main__":
# set_custom_root(Path("./instance/"))
set_custom_root(Path("../../../bagels_instance/"))

from bagels.config import load_config

load_config()

from bagels.models.database.app import init_db

Expand Down
Loading

0 comments on commit 5afec04

Please sign in to comment.