Files
octopus/myproject/run_fix_reservations.py
Andrey Smakotin fcc7f2263d Fix product reservation system for demo orders
PROBLEM ANALYSIS:
- SQL script created orders bypassing Django ORM
- Django signals (post_save) didn't trigger
- No reservations were created automatically
- Found 51 orders with 102 items and 0 reservations

SOLUTION IMPLEMENTED:

1. Updated create_demo_orders command:
   - Added clear documentation about ORM usage
   - Already uses ORM (.save()) which triggers signals
   - Added informative messages about automatic reservations

2. Created fix_missing_reservations command:
   - Finds OrderItems without reservations
   - Creates missing Reservation records
   - Supports --dry-run mode for safety
   - Handles missing warehouses gracefully

3. Created SQL fix script:
   - Direct SQL approach for existing data
   - Creates reservations for all 102 items
   - Status: 'reserved'
   - Verified: All items now have reservations

4. Added verification scripts:
   - check_orders.py: Shows orders/items/reservations count
   - run_fix_reservations.py: Executes SQL fix

RESULTS:
- ✓ 102 reservations created for existing orders
- ✓ Future orders will use ORM and create reservations automatically
- ✓ System now works correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 00:04:55 +03:00

33 lines
983 B
Python

"""
Скрипт для создания резервов для существующих заказов
"""
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from django.db import connection
# Читаем SQL скрипт
with open('fix_reservations.sql', 'r', encoding='utf-8') as f:
sql = f.read()
# Выполняем SQL
with connection.cursor() as cursor:
try:
cursor.execute(sql)
print("[OK] SQL script executed successfully!")
print("\nResults:")
# Получаем результат последнего SELECT
rows = cursor.fetchall()
if rows:
row = rows[0]
print(f" Total items: {row[0]}")
print(f" Items with reservations: {row[1]}")
print(f" Items without reservations: {row[2]}")
print("\n[OK] Reservations created!")
except Exception as e:
print(f"[ERROR] {e}")
raise