Expose GET/POST /api/v1/export/ for server dumps and add import UI for relations (CSV/JSON) plus remote backup controls. Co-authored-by: Cursor <cursoragent@cursor.com>
55 lines
2.4 KiB
Python
55 lines
2.4 KiB
Python
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from core.access import use_jwt_auth
|
|
from core.drf_mixins import JwtAuthMixin
|
|
from .dump_services import build_owner_dump, import_owner_dump, is_data_dump, parse_dump_request
|
|
from .services import parse_upload_file, import_contacts_from_rows
|
|
|
|
|
|
class ExportDumpView(JwtAuthMixin, APIView):
|
|
def get(self, request):
|
|
owner = request.user if use_jwt_auth() and request.user.is_authenticated else None
|
|
return Response(build_owner_dump(owner))
|
|
|
|
def post(self, request):
|
|
data, error = parse_dump_request(request)
|
|
if error:
|
|
return Response({'error': error}, status=status.HTTP_400_BAD_REQUEST)
|
|
if not is_data_dump(data):
|
|
return Response(
|
|
{'error': 'Некорректный формат бэкапа: ожидаются массивы contacts и relations.'},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
replace = str(request.query_params.get('replace', '')).lower() in ('1', 'true', 'yes')
|
|
owner = request.user if use_jwt_auth() and request.user.is_authenticated else None
|
|
try:
|
|
summary = import_owner_dump(owner, data, replace=replace)
|
|
return Response(summary)
|
|
except ValueError as exc:
|
|
return Response({'error': str(exc)}, status=status.HTTP_400_BAD_REQUEST)
|
|
except Exception as exc:
|
|
return Response(
|
|
{'error': f'Ошибка импорта бэкапа: {exc}'},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
|
|
class ImportContactsView(JwtAuthMixin, APIView):
|
|
def post(self, request):
|
|
file = request.FILES.get('file')
|
|
if not file:
|
|
return Response({'error': 'Файл не передан.'}, status=status.HTTP_400_BAD_REQUEST)
|
|
try:
|
|
rows, error = parse_upload_file(file)
|
|
if error:
|
|
return Response({'error': error}, status=status.HTTP_400_BAD_REQUEST)
|
|
owner = request.user if use_jwt_auth() and request.user.is_authenticated else None
|
|
return Response(import_contacts_from_rows(rows, owner=owner))
|
|
except Exception as e:
|
|
return Response(
|
|
{'error': f'Ошибка разбора файла: {e}'},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|