Files
vontor-cz/backend/commerce/management/commands/migrate_to_global_currency.py
Brunobrno 775709bd08 Migrate to global currency system in commerce app
Removed per-product currency in favor of a global site currency managed via SiteConfiguration. Updated models, views, templates, and Stripe integration to use the global currency. Added migration, management command for migration, and API endpoint for currency info. Improved permissions and filtering for orders, reviews, and carts. Expanded supported currencies in configuration.
2026-01-24 21:51:56 +01:00

74 lines
2.8 KiB
Python

"""
Management command to migrate from per-product currency to global currency system.
Usage: python manage.py migrate_to_global_currency
"""
from django.core.management.base import BaseCommand
from commerce.models import Product, Order
from configuration.models import SiteConfiguration
class Command(BaseCommand):
help = 'Migrate from per-product currency to global currency system'
def add_arguments(self, parser):
parser.add_argument(
'--target-currency',
type=str,
default='EUR',
help='Target currency to migrate to (default: EUR)'
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Show what would be changed without making changes'
)
def handle(self, *args, **options):
target_currency = options['target_currency']
dry_run = options['dry_run']
self.stdout.write(
self.style.SUCCESS(f"Migrating to global currency: {target_currency}")
)
# Check current state
config = SiteConfiguration.get_solo()
self.stdout.write(f"Current site currency: {config.currency}")
if hasattr(Product.objects.first(), 'currency'):
# Products still have currency field
product_currencies = Product.objects.values_list('currency', flat=True).distinct()
self.stdout.write(f"Product currencies found: {list(product_currencies)}")
if len(product_currencies) > 1:
self.stdout.write(
self.style.WARNING(
"Multiple currencies detected in products. "
"Consider currency conversion before migration."
)
)
order_currencies = Order.objects.values_list('currency', flat=True).distinct()
order_currencies = [c for c in order_currencies if c] # Remove empty strings
self.stdout.write(f"Order currencies found: {list(order_currencies)}")
if not dry_run:
# Update site configuration
config.currency = target_currency
config.save()
self.stdout.write(
self.style.SUCCESS(f"Updated site currency to {target_currency}")
)
# Update orders with empty currency
orders_updated = Order.objects.filter(currency='').update(currency=target_currency)
self.stdout.write(
self.style.SUCCESS(f"Updated {orders_updated} orders to use {target_currency}")
)
else:
self.stdout.write(self.style.WARNING("DRY RUN - No changes made"))
self.stdout.write(
self.style.SUCCESS("Migration completed successfully!")
)