fix: restore hidden budget categories during seed
This commit is contained in:
+45
-11
@@ -204,6 +204,44 @@ def _deactivate_placeholder_categories() -> None:
|
|||||||
category.community_account_id = None
|
category.community_account_id = None
|
||||||
|
|
||||||
|
|
||||||
|
def _restore_existing_standard_structure() -> None:
|
||||||
|
for account_slug, account_data in ACCOUNT_TREE.items():
|
||||||
|
account = Account.query.filter_by(slug=account_slug).first()
|
||||||
|
if account is None:
|
||||||
|
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))
|
||||||
|
if category is None and legacy_slug:
|
||||||
|
category = Category.query.filter_by(account_id=account.id, slug=legacy_slug).first()
|
||||||
|
if category is None:
|
||||||
|
continue
|
||||||
|
category.is_active = True
|
||||||
|
for entry_name in entries:
|
||||||
|
entry_slug = slugify(entry_name)
|
||||||
|
entry = Entry.query.filter_by(category_id=category.id, slug=entry_slug).first()
|
||||||
|
legacy_entry_name = LEGACY_ENTRY_NAMES.get((account_slug, category_slug, entry_name))
|
||||||
|
if entry is None and legacy_entry_name:
|
||||||
|
entry = Entry.query.filter_by(
|
||||||
|
category_id=category.id,
|
||||||
|
slug=slugify(legacy_entry_name),
|
||||||
|
).first()
|
||||||
|
if entry is not None:
|
||||||
|
entry.is_active = True
|
||||||
|
|
||||||
|
|
||||||
|
def _restore_existing_budget_visibility() -> None:
|
||||||
|
gemeinschaft = Account.query.filter_by(slug="gemeinschaftskonto").first()
|
||||||
|
if gemeinschaft is None:
|
||||||
|
return
|
||||||
|
for category in gemeinschaft.categories:
|
||||||
|
if category.is_active:
|
||||||
|
continue
|
||||||
|
active_entries = [entry for entry in category.entries if entry.is_active]
|
||||||
|
if active_entries:
|
||||||
|
category.is_active = True
|
||||||
|
|
||||||
|
|
||||||
def seed_data(include_example_entries: bool = False) -> None:
|
def seed_data(include_example_entries: bool = False) -> None:
|
||||||
# Basisdaten nur für die fachliche Grundstruktur, ohne Demo-Benutzer,
|
# Basisdaten nur für die fachliche Grundstruktur, ohne Demo-Benutzer,
|
||||||
# Beispiel-Personen oder vorausgefüllte Monatsdaten.
|
# Beispiel-Personen oder vorausgefüllte Monatsdaten.
|
||||||
@@ -393,18 +431,14 @@ def seed_data(include_example_entries: bool = False) -> None:
|
|||||||
"Person 1",
|
"Person 1",
|
||||||
"Person 2",
|
"Person 2",
|
||||||
}
|
}
|
||||||
|
for category in gemeinschaft.categories:
|
||||||
|
if category.slug not in ACCOUNT_TREE["gemeinschaftskonto"]["categories"]:
|
||||||
|
category.is_active = False
|
||||||
|
elif category.community_account_id is None:
|
||||||
|
category.community_account_id = community_accounts["hauptkonto"].id
|
||||||
else:
|
else:
|
||||||
_deactivate_placeholder_categories()
|
_restore_existing_standard_structure()
|
||||||
_deactivate_placeholder_entries()
|
_restore_existing_budget_visibility()
|
||||||
for category in gemeinschaft.categories:
|
|
||||||
if category.slug not in ACCOUNT_TREE["gemeinschaftskonto"]["categories"]:
|
|
||||||
category.is_active = False
|
|
||||||
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()
|
db.session.commit()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from app.extensions import db
|
||||||
|
from app.models import Account, Category, Entry
|
||||||
|
from app.seed import seed_data
|
||||||
|
|
||||||
|
|
||||||
|
def test_seed_restores_hidden_budget_categories_with_active_entries(app):
|
||||||
|
budget_account = Account.query.filter_by(slug="gemeinschaftskonto").first()
|
||||||
|
category = Category(
|
||||||
|
account_id=budget_account.id,
|
||||||
|
name="Legacy Budget",
|
||||||
|
slug="legacy-budget",
|
||||||
|
is_active=False,
|
||||||
|
sort_order=999,
|
||||||
|
)
|
||||||
|
db.session.add(category)
|
||||||
|
db.session.flush()
|
||||||
|
db.session.add(
|
||||||
|
Entry(
|
||||||
|
category_id=category.id,
|
||||||
|
name="Legacy Eintrag",
|
||||||
|
slug="legacy-eintrag",
|
||||||
|
default_amount=0,
|
||||||
|
amount_type="fixed",
|
||||||
|
benefit_scope="all-users",
|
||||||
|
is_active=True,
|
||||||
|
sort_order=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
seed_data()
|
||||||
|
db.session.expire_all()
|
||||||
|
|
||||||
|
restored = Category.query.filter_by(account_id=budget_account.id, slug="legacy-budget").first()
|
||||||
|
assert restored is not None
|
||||||
|
assert restored.is_active is True
|
||||||
Reference in New Issue
Block a user