63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
from django.http import HttpResponse
|
|
from django.urls import path
|
|
from rest_framework.decorators import api_view, permission_classes, authentication_classes
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
|
|
from authentication.signature_auth import SignatureAuthentication
|
|
|
|
|
|
def user_data():
|
|
import io
|
|
import zipfile
|
|
|
|
zip_buffer = io.BytesIO()
|
|
|
|
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
|
|
for file_name, data in [('1.txt', io.BytesIO(b'111')),
|
|
('2.txt', io.BytesIO(b'222'))]:
|
|
zip_file.writestr(file_name, data.getvalue())
|
|
|
|
return zip_buffer.getvalue()
|
|
|
|
|
|
def parse_user_data(data):
|
|
import io
|
|
import zipfile
|
|
|
|
with zipfile.ZipFile(io.BytesIO(data), "r") as zip_file:
|
|
for file_name in zip_file.namelist():
|
|
yield file_name, zip_file.read(file_name)
|
|
|
|
|
|
@api_view(['POST'])
|
|
@permission_classes([IsAuthenticated])
|
|
@authentication_classes([SignatureAuthentication])
|
|
def import_data(request, format=None):
|
|
zip = request.data.get('zip')
|
|
if not zip:
|
|
return Response(status=400)
|
|
for file_name, data in parse_user_data(zip):
|
|
print(file_name, data)
|
|
return Response(status=200)
|
|
|
|
|
|
@api_view(['GET'])
|
|
@permission_classes([IsAuthenticated])
|
|
@authentication_classes([SignatureAuthentication])
|
|
def export_data(request, format=None):
|
|
return HttpResponse(user_data(), content_type='application/zip', status=200)
|
|
|
|
|
|
@api_view(['DELETE'])
|
|
@permission_classes([IsAuthenticated])
|
|
@authentication_classes([SignatureAuthentication])
|
|
def delete_account(request, format=None):
|
|
pass
|
|
|
|
|
|
urlpatterns = [
|
|
path('export/', export_data, name='export_data'),
|
|
path('import/', import_data, name='import_data'),
|
|
path('account_data/', delete_account, name='delete_account'),
|
|
]
|