fix: start with empty budgets and savings categories
This commit is contained in:
+54
-18
@@ -137,6 +137,12 @@ EXAMPLE_ENTRY_NAMES = {
|
||||
for entry_name in entries
|
||||
}
|
||||
|
||||
EXAMPLE_CATEGORY_KEYS = {
|
||||
(account_slug, category_slug)
|
||||
for account_slug, account_data in ACCOUNT_TREE.items()
|
||||
for category_slug in account_data["categories"].keys()
|
||||
}
|
||||
|
||||
|
||||
def slugify(value: str) -> str:
|
||||
return (
|
||||
@@ -150,28 +156,53 @@ def slugify(value: str) -> str:
|
||||
)
|
||||
|
||||
|
||||
def _entry_has_user_data(entry: Entry) -> bool:
|
||||
for value in entry.monthly_values:
|
||||
if (
|
||||
to_decimal(value.planned_amount) != Decimal("0.00")
|
||||
or value.note
|
||||
or value.created_by is not None
|
||||
or value.updated_by is not None
|
||||
):
|
||||
return True
|
||||
if entry.share_rules:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _deactivate_placeholder_entries() -> None:
|
||||
example_entries = (
|
||||
Entry.query.filter(Entry.name.in_(sorted(EXAMPLE_ENTRY_NAMES)), Entry.is_active.is_(True)).all()
|
||||
)
|
||||
example_entries = Entry.query.filter(
|
||||
Entry.name.in_(sorted(EXAMPLE_ENTRY_NAMES)),
|
||||
Entry.is_active.is_(True),
|
||||
).all()
|
||||
for entry in example_entries:
|
||||
has_user_data = False
|
||||
for value in entry.monthly_values:
|
||||
if (
|
||||
to_decimal(value.planned_amount) != Decimal("0.00")
|
||||
or value.note
|
||||
or value.created_by is not None
|
||||
or value.updated_by is not None
|
||||
):
|
||||
has_user_data = True
|
||||
break
|
||||
if entry.share_rules:
|
||||
has_user_data = True
|
||||
if has_user_data:
|
||||
if _entry_has_user_data(entry):
|
||||
continue
|
||||
entry.is_active = False
|
||||
|
||||
|
||||
def _deactivate_placeholder_categories() -> None:
|
||||
categories = (
|
||||
Category.query.join(Account)
|
||||
.filter(Category.is_active.is_(True), Account.is_active.is_(True))
|
||||
.all()
|
||||
)
|
||||
for category in categories:
|
||||
account = category.account
|
||||
if account is None or (account.slug, category.slug) not in EXAMPLE_CATEGORY_KEYS:
|
||||
continue
|
||||
active_entries = [entry for entry in category.entries if entry.is_active]
|
||||
if not active_entries:
|
||||
category.is_active = False
|
||||
category.community_account_id = None
|
||||
continue
|
||||
if all(entry.name in EXAMPLE_ENTRY_NAMES and not _entry_has_user_data(entry) for entry in active_entries):
|
||||
for entry in active_entries:
|
||||
entry.is_active = False
|
||||
category.is_active = False
|
||||
category.community_account_id = None
|
||||
|
||||
|
||||
def seed_data(include_example_entries: bool = False) -> None:
|
||||
# Basisdaten nur für die fachliche Grundstruktur, ohne Demo-Benutzer,
|
||||
# Beispiel-Personen oder vorausgefüllte Monatsdaten.
|
||||
@@ -225,6 +256,8 @@ def seed_data(include_example_entries: bool = False) -> None:
|
||||
sort_order += 1
|
||||
category_sort = 1
|
||||
account_categories[account_slug] = {}
|
||||
if not include_example_entries:
|
||||
continue
|
||||
for category_slug, entries in account_data["categories"].items():
|
||||
category = Category.query.filter_by(account_id=account.id, slug=category_slug).first()
|
||||
legacy_slug = LEGACY_CATEGORY_SLUGS.get((account_slug, category_slug))
|
||||
@@ -254,8 +287,6 @@ def seed_data(include_example_entries: bool = False) -> None:
|
||||
category.community_account_id = community_accounts["hauptkonto"].id
|
||||
account_categories[account_slug][category_slug] = category
|
||||
category_sort += 1
|
||||
if not include_example_entries:
|
||||
continue
|
||||
for index, entry_name in enumerate(entries, start=1):
|
||||
default_amount = Decimal("0.00")
|
||||
if entry_name == "Miete":
|
||||
@@ -362,6 +393,7 @@ def seed_data(include_example_entries: bool = False) -> None:
|
||||
"Person 2",
|
||||
}
|
||||
else:
|
||||
_deactivate_placeholder_categories()
|
||||
_deactivate_placeholder_entries()
|
||||
for category in gemeinschaft.categories:
|
||||
if category.slug not in ACCOUNT_TREE["gemeinschaftskonto"]["categories"]:
|
||||
@@ -369,6 +401,10 @@ def seed_data(include_example_entries: bool = False) -> None:
|
||||
elif category.community_account_id is None:
|
||||
category.community_account_id = community_accounts["hauptkonto"].id
|
||||
|
||||
if not include_example_entries:
|
||||
_deactivate_placeholder_categories()
|
||||
_deactivate_placeholder_entries()
|
||||
|
||||
db.session.commit()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user