|
|
|
|
@ -29,6 +29,9 @@ from django.core.files.storage import default_storage |
|
|
|
|
from django.core.files.base import ContentFile |
|
|
|
|
import os |
|
|
|
|
from django.http import HttpResponse |
|
|
|
|
import logging |
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
@api_view(['GET']) |
|
|
|
|
def user_by_token(request): |
|
|
|
|
@ -406,8 +409,12 @@ def xls_to_csv(request): |
|
|
|
|
file_path = os.path.join(directory, uploaded_file.name) |
|
|
|
|
file_name = default_storage.save(file_path, ContentFile(uploaded_file.read())) |
|
|
|
|
|
|
|
|
|
logger.info(f'file saved at {file_name}') |
|
|
|
|
full_path = default_storage.path(file_name) |
|
|
|
|
logger.info(f'full_path = {full_path}') |
|
|
|
|
|
|
|
|
|
# Check available sheets and look for 'inscriptions' |
|
|
|
|
xls = pd.ExcelFile(file_name) |
|
|
|
|
xls = pd.ExcelFile(full_path) |
|
|
|
|
sheet_names = xls.sheet_names |
|
|
|
|
|
|
|
|
|
# Determine which sheet to use |
|
|
|
|
@ -419,13 +426,14 @@ def xls_to_csv(request): |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
# Convert to csv and save |
|
|
|
|
data_xls = pd.read_excel(file_name, sheet_name=target_sheet, index_col=None) |
|
|
|
|
data_xls = pd.read_excel(full_path, sheet_name=target_sheet, index_col=None) |
|
|
|
|
csv_file_name = create_random_filename('players', 'csv') |
|
|
|
|
output_path = os.path.join(directory, csv_file_name) |
|
|
|
|
data_xls.to_csv(output_path, sep=';', index=False, encoding='utf-8') |
|
|
|
|
full_output_path = default_storage.path(output_path) |
|
|
|
|
data_xls.to_csv(full_output_path, sep=';', index=False, encoding='utf-8') |
|
|
|
|
|
|
|
|
|
# Send the processed file back |
|
|
|
|
with default_storage.open(output_path, 'rb') as file: |
|
|
|
|
with default_storage.open(full_output_path, 'rb') as file: |
|
|
|
|
response = HttpResponse(file.read(), content_type='application/octet-stream') |
|
|
|
|
response['Content-Disposition'] = f'attachment; filename="players.csv"' |
|
|
|
|
|
|
|
|
|
|