Добавлены кнопки Импорт и Экспорт на страницу списка клиентов
- Добавлены кнопки Импорт и Экспорт в header страницы customers/ - Создан URL-маршрут для customer-import и customer-export - Реализована функция customer_export: экспорт всех клиентов в CSV файл с BOM для Excel - Экспортируются поля: ID, Имя, Фамилия, Email, Телефон, Баланс кошелька, Дата создания - Создан шаблон customer_import.html с инструкцией и формой загрузки файла - Функция customer_import пока заглушка (TODO: реализовать парсинг CSV/Excel) - Кнопки оформлены в btn-group с иконками Bootstrap Icons - Системный клиент исключён из экспорта
This commit is contained in:
@@ -591,3 +591,69 @@ def wallet_withdraw(request, pk):
|
||||
messages.error(request, '; '.join(e.messages) if hasattr(e, 'messages') else str(e))
|
||||
|
||||
return redirect('customers:customer-detail', pk=pk)
|
||||
|
||||
|
||||
@login_required
|
||||
@manager_or_owner_required
|
||||
def customer_import(request):
|
||||
"""
|
||||
Импорт клиентов из CSV/Excel файла.
|
||||
TODO: Реализовать логику импорта
|
||||
"""
|
||||
if request.method == 'POST':
|
||||
# TODO: Обработка загруженного файла
|
||||
messages.info(request, 'Функция импорта в разработке')
|
||||
return redirect('customers:customer-list')
|
||||
|
||||
context = {
|
||||
'title': 'Импорт клиентов',
|
||||
}
|
||||
return render(request, 'customers/customer_import.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
@manager_or_owner_required
|
||||
def customer_export(request):
|
||||
"""
|
||||
Экспорт клиентов в CSV/Excel файл.
|
||||
TODO: Реализовать логику экспорта
|
||||
"""
|
||||
import csv
|
||||
from django.http import HttpResponse
|
||||
from django.utils import timezone
|
||||
|
||||
# Создаём HTTP ответ с CSV файлом
|
||||
response = HttpResponse(content_type='text/csv; charset=utf-8')
|
||||
response['Content-Disposition'] = f'attachment; filename="customers_export_{timezone.now().strftime("%Y%m%d_%H%M%S")}.csv"'
|
||||
|
||||
# Добавляем BOM для корректного открытия в Excel
|
||||
response.write('\ufeff')
|
||||
|
||||
writer = csv.writer(response)
|
||||
|
||||
# Заголовки
|
||||
writer.writerow([
|
||||
'ID',
|
||||
'Имя',
|
||||
'Фамилия',
|
||||
'Email',
|
||||
'Телефон',
|
||||
'Баланс кошелька',
|
||||
'Дата создания',
|
||||
])
|
||||
|
||||
# Данные (исключаем системного клиента)
|
||||
customers = Customer.objects.filter(is_system_customer=False).order_by('-created_at')
|
||||
|
||||
for customer in customers:
|
||||
writer.writerow([
|
||||
customer.id,
|
||||
customer.first_name or '',
|
||||
customer.last_name or '',
|
||||
customer.email or '',
|
||||
str(customer.phone) if customer.phone else '',
|
||||
str(customer.wallet_balance),
|
||||
customer.created_at.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
])
|
||||
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user