feat(orders): add recipient management and enhance order forms
- Introduced Recipient model to manage order recipients separately from customers. - Updated Order model to link to Recipient, replacing recipient_name and recipient_phone fields. - Enhanced OrderForm to include recipient selection modes: customer, history, and new. - Added AJAX endpoint to fetch recipient history for customers. - Updated admin interface to manage recipients and display recipient information in order details. - Refactored address handling to accommodate new recipient logic. - Improved demo order creation to include random recipients.
This commit is contained in:
77
myproject/orders/migrations/0011_migrate_recipient_data.py
Normal file
77
myproject/orders/migrations/0011_migrate_recipient_data.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# Generated by Django 5.0.10 on 2025-12-22 19:32
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def migrate_recipient_data_forward(apps, schema_editor):
|
||||
"""
|
||||
Перенос данных получателей из старых полей Order в новую модель Recipient.
|
||||
Так как поля recipient_name и recipient_phone уже удалены,
|
||||
мы используем HistoricalOrder для восстановления данных.
|
||||
"""
|
||||
# Получаем модели
|
||||
HistoricalOrder = apps.get_model('orders', 'HistoricalOrder')
|
||||
Recipient = apps.get_model('orders', 'Recipient')
|
||||
Order = apps.get_model('orders', 'Order')
|
||||
|
||||
# Словарь для кэширования recipient'ов
|
||||
recipients_cache = {}
|
||||
|
||||
# Обрабатываем каждый заказ
|
||||
for order in Order.objects.all():
|
||||
# Находим последнюю историческую запись для этого заказа
|
||||
hist = HistoricalOrder.objects.filter(
|
||||
order_number=order.order_number
|
||||
).order_by('-history_date').first()
|
||||
|
||||
if not hist:
|
||||
continue
|
||||
|
||||
# Проверяем, есть ли данные получателя
|
||||
recipient_name = getattr(hist, 'recipient_name', None)
|
||||
recipient_phone = getattr(hist, 'recipient_phone', None)
|
||||
|
||||
# Если получатель не указан или customer_is_recipient=True, пропускаем
|
||||
if not recipient_name or not recipient_phone or order.customer_is_recipient:
|
||||
continue
|
||||
|
||||
# Создаем ключ для кэша
|
||||
cache_key = f"{recipient_name}|{recipient_phone}"
|
||||
|
||||
# Проверяем, есть ли уже такой получатель в кэше
|
||||
if cache_key in recipients_cache:
|
||||
recipient = recipients_cache[cache_key]
|
||||
else:
|
||||
# Создаем нового получателя
|
||||
recipient, created = Recipient.objects.get_or_create(
|
||||
name=recipient_name,
|
||||
phone=recipient_phone
|
||||
)
|
||||
recipients_cache[cache_key] = recipient
|
||||
|
||||
# Привязываем получателя к заказу
|
||||
order.recipient = recipient
|
||||
order.save(update_fields=['recipient'])
|
||||
|
||||
|
||||
def migrate_recipient_data_backward(apps, schema_editor):
|
||||
"""
|
||||
Обратная миграция - просто очищаем recipient поле в Order.
|
||||
Данные вернутся из HistoricalOrder при повторном apply.
|
||||
"""
|
||||
Order = apps.get_model('orders', 'Order')
|
||||
Order.objects.all().update(recipient=None)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('orders', '0010_remove_address_recipient_name_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
migrate_recipient_data_forward,
|
||||
migrate_recipient_data_backward
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user