fix: keep fresh installs free of placeholder entries

This commit is contained in:
2026-04-21 21:30:47 +02:00
parent b9212d9c41
commit 35af65dd25
+76 -42
View File
@@ -130,6 +130,13 @@ ENTRY_TARGET_CATEGORY = {
"Kreditrate 2": "finanzen", "Kreditrate 2": "finanzen",
} }
EXAMPLE_ENTRY_NAMES = {
entry_name
for account_data in ACCOUNT_TREE.values()
for entries in account_data["categories"].values()
for entry_name in entries
}
def slugify(value: str) -> str: def slugify(value: str) -> str:
return ( return (
@@ -143,7 +150,29 @@ def slugify(value: str) -> str:
) )
def seed_data() -> None: def _deactivate_placeholder_entries() -> None:
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:
continue
entry.is_active = False
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.
community_accounts = {} community_accounts = {}
@@ -225,6 +254,8 @@ def seed_data() -> None:
category.community_account_id = community_accounts["hauptkonto"].id category.community_account_id = community_accounts["hauptkonto"].id
account_categories[account_slug][category_slug] = category account_categories[account_slug][category_slug] = category
category_sort += 1 category_sort += 1
if not include_example_entries:
continue
for index, entry_name in enumerate(entries, start=1): for index, entry_name in enumerate(entries, start=1):
default_amount = Decimal("0.00") default_amount = Decimal("0.00")
if entry_name == "Miete": if entry_name == "Miete":
@@ -293,47 +324,50 @@ def seed_data() -> None:
gemeinschaft = Account.query.filter_by(slug="gemeinschaftskonto").first() gemeinschaft = Account.query.filter_by(slug="gemeinschaftskonto").first()
if gemeinschaft: if gemeinschaft:
target_categories = account_categories["gemeinschaftskonto"] if include_example_entries:
with db.session.no_autoflush: target_categories = account_categories["gemeinschaftskonto"]
for category in gemeinschaft.categories: with db.session.no_autoflush:
for entry in list(category.entries): for category in gemeinschaft.categories:
target_slug = ENTRY_TARGET_CATEGORY.get(entry.name) for entry in list(category.entries):
if not target_slug: target_slug = ENTRY_TARGET_CATEGORY.get(entry.name)
continue if not target_slug:
target_category = target_categories.get(target_slug) continue
if not target_category: target_category = target_categories.get(target_slug)
continue if not target_category:
existing_target_entry = Entry.query.filter_by( continue
category_id=target_category.id, existing_target_entry = Entry.query.filter_by(
slug=entry.slug, category_id=target_category.id,
).first() slug=entry.slug,
if existing_target_entry and existing_target_entry.id != entry.id: ).first()
for monthly_value in entry.monthly_values: if existing_target_entry and existing_target_entry.id != entry.id:
monthly_value.entry_id = existing_target_entry.id for monthly_value in entry.monthly_values:
existing_rule_participants = { monthly_value.entry_id = existing_target_entry.id
rule.participant_id for rule in existing_target_entry.share_rules existing_rule_participants = {
rule.participant_id for rule in existing_target_entry.share_rules
}
for rule in list(entry.share_rules):
if rule.participant_id in existing_rule_participants:
db.session.delete(rule)
continue
rule.entry_id = existing_target_entry.id
db.session.delete(entry)
continue
entry.category_id = target_category.id
entry.benefit_scope = "all-users"
entry.is_allocation_target = entry.name in {
"Sparziel",
"Reisebudget",
"Freizeitbudget",
"Person 1",
"Person 2",
} }
for rule in list(entry.share_rules): else:
if rule.participant_id in existing_rule_participants: _deactivate_placeholder_entries()
db.session.delete(rule) for category in gemeinschaft.categories:
continue if category.slug not in ACCOUNT_TREE["gemeinschaftskonto"]["categories"]:
rule.entry_id = existing_target_entry.id category.is_active = False
db.session.delete(entry) elif category.community_account_id is None:
continue category.community_account_id = community_accounts["hauptkonto"].id
entry.category_id = target_category.id
entry.benefit_scope = "all-users"
entry.is_allocation_target = entry.name in {
"Sparziel",
"Reisebudget",
"Freizeitbudget",
"Person 1",
"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
db.session.commit() db.session.commit()
@@ -342,7 +376,7 @@ def seed_demo_data() -> None:
from datetime import date from datetime import date
from flask import current_app from flask import current_app
seed_data() seed_data(include_example_entries=True)
admin = User.query.filter_by(username="admin").first() admin = User.query.filter_by(username="admin").first()
if admin is None: if admin is None: