Skip to content

Commit

Permalink
fix(Django): fix daily DB backup task (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 23, 2024
1 parent d5e8cfd commit 35cbb81
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion open_prices/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def dump_db_task():
"""
Dump the database as JSONL files to the data directory
"""
output_dir = Path(os.path.join(settings.BASE_DIR, "static/data"))
output_dir = Path(os.path.join(settings.BASE_DIR, "static", "data"))
output_dir.mkdir(parents=True, exist_ok=True)

for table_name, model_class, schema_class in (
("prices", Price, PriceSerializer),
Expand Down
6 changes: 4 additions & 2 deletions open_prices/common/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import gzip
import json
import os

import tqdm
from django.core.serializers.json import DjangoJSONEncoder


def is_float(string):
Expand All @@ -28,10 +30,10 @@ def add_validation_error(dict, key, value):


def export_model_to_jsonl_gz(table_name, model_class, schema_class, output_dir):
output_path = output_dir / f"{table_name}.jsonl.gz"
output_path = os.path.join(output_dir, f"{table_name}.jsonl.gz")
with gzip.open(output_path, "wt") as f:
for item in tqdm.tqdm(model_class.objects.all(), desc=table_name):
f.write(json.dumps(schema_class(item).data))
f.write(json.dumps(schema_class(item).data, cls=DjangoJSONEncoder))
f.write("\n")


Expand Down

0 comments on commit 35cbb81

Please sign in to comment.