Files
WYGIWYH/app/apps/export_app/resources/splits.py
T
Asish Kumar a41b32420a feat(export): back up split expenses
Export and restore left split events out, so moving to a new instance or
restoring a backup lost every event, its people, expenses, payments and
the links to the members' own transactions.

- Export has a "Split Expenses" option, on by default. It writes
  split_events, split_participants, split_expenses, split_expense_shares
  and split_payments. Like the bank sync option, it warns that the file
  carries something sensitive: the events' share links, kept so the links
  people already have keep working after a restore.
- split_transaction_links goes along only when transactions are exported
  too, the same way sub-transactions follow their parents, so a restore
  never points at transactions missing from the backup.
- Restore reads the new files after users, currencies and transactions,
  in dependency order. Everything is matched by id, except the event's
  currency (by name, like DCA strategies) and the linked transaction,
  which is looked up across every user: restore runs as the admin, who
  can't see the other members' transactions.
- Guests' usernames and password hashes are restored, so they can still
  sign in through the share link.
- Attachments stay out, as transaction attachments do: they're files,
  not rows.
2026-12-05 05:46:57 +05:30

93 lines
2.3 KiB
Python

from import_export import fields, resources
from import_export.widgets import ForeignKeyWidget
from apps.currencies.models import Currency
from apps.export_app.resources.transactions import AnyOwnerForeignKeyWidget
from apps.export_app.widgets.numbers import UniversalDecimalWidget
from apps.splits.models import (
SplitEvent,
SplitExpense,
SplitExpenseShare,
SplitParticipant,
SplitPayment,
SplitTransactionLink,
)
from apps.transactions.models import Transaction
class SplitEventResource(resources.ModelResource):
currency = fields.Field(
attribute="currency",
column_name="currency",
widget=ForeignKeyWidget(Currency, "name"),
)
class Meta:
model = SplitEvent
def get_queryset(self):
return SplitEvent.objects.select_related("currency")
class SplitParticipantResource(resources.ModelResource):
class Meta:
model = SplitParticipant
class SplitExpenseResource(resources.ModelResource):
amount = fields.Field(
attribute="amount",
column_name="amount",
widget=UniversalDecimalWidget(),
)
class Meta:
model = SplitExpense
class SplitExpenseShareResource(resources.ModelResource):
amount = fields.Field(
attribute="amount",
column_name="amount",
widget=UniversalDecimalWidget(),
)
percentage = fields.Field(
attribute="percentage",
column_name="percentage",
widget=UniversalDecimalWidget(),
)
shares = fields.Field(
attribute="shares",
column_name="shares",
widget=UniversalDecimalWidget(),
)
class Meta:
model = SplitExpenseShare
class SplitPaymentResource(resources.ModelResource):
amount = fields.Field(
attribute="amount",
column_name="amount",
widget=UniversalDecimalWidget(),
)
class Meta:
model = SplitPayment
class SplitTransactionLinkResource(resources.ModelResource):
# By id across every user: the restoring admin can't see the others' transactions
transaction = fields.Field(
attribute="transaction",
column_name="transaction",
widget=AnyOwnerForeignKeyWidget(Transaction),
)
class Meta:
model = SplitTransactionLink
def get_queryset(self):
return SplitTransactionLink.objects.select_related("transaction")