fix: keep fresh installs free of placeholder entries
This commit is contained in:
+76
-42
@@ -130,6 +130,13 @@ ENTRY_TARGET_CATEGORY = {
|
||||
"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:
|
||||
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,
|
||||
# Beispiel-Personen oder vorausgefüllte Monatsdaten.
|
||||
community_accounts = {}
|
||||
@@ -225,6 +254,8 @@ def seed_data() -> 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":
|
||||
@@ -293,47 +324,50 @@ def seed_data() -> None:
|
||||
|
||||
gemeinschaft = Account.query.filter_by(slug="gemeinschaftskonto").first()
|
||||
if gemeinschaft:
|
||||
target_categories = account_categories["gemeinschaftskonto"]
|
||||
with db.session.no_autoflush:
|
||||
for category in gemeinschaft.categories:
|
||||
for entry in list(category.entries):
|
||||
target_slug = ENTRY_TARGET_CATEGORY.get(entry.name)
|
||||
if not target_slug:
|
||||
continue
|
||||
target_category = target_categories.get(target_slug)
|
||||
if not target_category:
|
||||
continue
|
||||
existing_target_entry = Entry.query.filter_by(
|
||||
category_id=target_category.id,
|
||||
slug=entry.slug,
|
||||
).first()
|
||||
if existing_target_entry and existing_target_entry.id != entry.id:
|
||||
for monthly_value in entry.monthly_values:
|
||||
monthly_value.entry_id = existing_target_entry.id
|
||||
existing_rule_participants = {
|
||||
rule.participant_id for rule in existing_target_entry.share_rules
|
||||
if include_example_entries:
|
||||
target_categories = account_categories["gemeinschaftskonto"]
|
||||
with db.session.no_autoflush:
|
||||
for category in gemeinschaft.categories:
|
||||
for entry in list(category.entries):
|
||||
target_slug = ENTRY_TARGET_CATEGORY.get(entry.name)
|
||||
if not target_slug:
|
||||
continue
|
||||
target_category = target_categories.get(target_slug)
|
||||
if not target_category:
|
||||
continue
|
||||
existing_target_entry = Entry.query.filter_by(
|
||||
category_id=target_category.id,
|
||||
slug=entry.slug,
|
||||
).first()
|
||||
if existing_target_entry and existing_target_entry.id != entry.id:
|
||||
for monthly_value in entry.monthly_values:
|
||||
monthly_value.entry_id = existing_target_entry.id
|
||||
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):
|
||||
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 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:
|
||||
_deactivate_placeholder_entries()
|
||||
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()
|
||||
|
||||
@@ -342,7 +376,7 @@ def seed_demo_data() -> None:
|
||||
from datetime import date
|
||||
from flask import current_app
|
||||
|
||||
seed_data()
|
||||
seed_data(include_example_entries=True)
|
||||
|
||||
admin = User.query.filter_by(username="admin").first()
|
||||
if admin is None:
|
||||
|
||||
Reference in New Issue
Block a user