Skip to content

Commit

Permalink
Enregistrement de l'export sur le repo s3 (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolok authored Dec 10, 2024
1 parent 75bbc91 commit 2189e5e
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions qfdmo/management/commands/export_displayedacteur.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import tempfile
from datetime import datetime
from pathlib import Path

import openpyxl
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.core.management.base import BaseCommand

from qfdmo.admin import OpenSourceDisplayedActeurResource

TARGET_FILE = "export_lavo_acteurs.xlsx"
CHUNK = 1000


Expand All @@ -15,33 +17,40 @@ class Command(BaseCommand):

def handle(self, *args, **options):
self.stdout.write(f"Exporting Ressources, starting at {datetime.now()}")
target_file = datetime.now().strftime(
"exports/export_acteur_%Y%m%d_%H%M%S.xlsx"
)

with tempfile.NamedTemporaryFile(mode="w+b", suffix=".xlsx") as tmp_file:
try:
workbook = openpyxl.load_workbook(Path(tmp_file.name).name)
sheet = workbook.active
except FileNotFoundError:
workbook = openpyxl.Workbook()
sheet = workbook.active

offset = 0
dataset = OpenSourceDisplayedActeurResource(
limit=CHUNK, offset=offset
).export()
sheet.append(dataset.headers)

if Path(TARGET_FILE).exists():
Path(TARGET_FILE).unlink()

try:
workbook = openpyxl.load_workbook(TARGET_FILE)
sheet = workbook.active
except FileNotFoundError:
workbook = openpyxl.Workbook()
sheet = workbook.active

offset = 0
dataset = OpenSourceDisplayedActeurResource(limit=CHUNK, offset=offset).export()
sheet.append(dataset.headers)
while dataset.dict:
self.stdout.write(f"Exporting {offset} to {offset + CHUNK}")
dataset.headers = None

while dataset.dict:
self.stdout.write(f"Exporting {offset} to {offset + CHUNK}")
dataset.headers = None
for row in dataset.dict:
sheet.append(row)

for row in dataset.dict:
sheet.append(row)
offset += CHUNK
dataset = OpenSourceDisplayedActeurResource(
limit=CHUNK, offset=offset
).export()

offset += CHUNK
dataset = OpenSourceDisplayedActeurResource(
limit=CHUNK, offset=offset
).export()
self.stdout.write(f"Writing to {target_file}")

workbook.save(TARGET_FILE)
workbook.save(tmp_file.name)
tmp_file.seek(0)
default_storage.save(target_file, ContentFile(tmp_file.read()))

self.stdout.write(f"Ended at {datetime.now()}")

0 comments on commit 2189e5e

Please sign in to comment.