Skip to content

Commit

Permalink
update storages module; allow defaults to do their magic for auth; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nucleogenesis committed Feb 27, 2025
1 parent 2e739fa commit 07db9d6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
9 changes: 3 additions & 6 deletions kolibri/core/logger/management/commands/exportlogs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import ntpath
import os

from dateutil import parser
from django.conf import settings
Expand Down Expand Up @@ -128,8 +127,6 @@ def handle_async(self, *args, **options):
else:
filename = options["output_file"]

filepath = os.path.join(os.getcwd(), filename)

queryset = log_info["queryset"]

total_rows = queryset.count()
Expand All @@ -139,7 +136,7 @@ def handle_async(self, *args, **options):
for row in csv_file_generator(
facility,
log_type,
filepath,
filename,
start_date=start_date,
end_date=end_date,
overwrite=options["overwrite"],
Expand All @@ -150,14 +147,14 @@ def handle_async(self, *args, **options):

if job:
job.extra_metadata["overall_error"] = self.overall_error
self.job.extra_metadata["filename"] = ntpath.basename(filepath)
self.job.extra_metadata["filename"] = ntpath.basename(filename)
job.save_meta()
else:
if self.overall_error:
raise CommandError(self.overall_error)
else:
logger.info(
"Created csv file {} with {} lines".format(filepath, total_rows)
"Created csv file {} with {} lines".format(filename, total_rows)
)

translation.deactivate()
Expand Down
10 changes: 3 additions & 7 deletions kolibri/core/logger/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from django.core.files.storage import default_storage
from django.core.management import call_command
from rest_framework import serializers
Expand All @@ -19,10 +17,8 @@

def get_filepath(log_type, facility_id, start_date, end_date):
facility = Facility.objects.get(id=facility_id)
filepath = default_storage.path(
CSV_EXPORT_FILENAMES[log_type].format(
facility.name, facility.id[:4], start_date[:10], end_date[:10]
),
filepath = CSV_EXPORT_FILENAMES[log_type].format(
facility.name, facility.id[:4], start_date[:10], end_date[:10]
)
return filepath

Expand All @@ -42,7 +38,7 @@ def get_valid_logs_csv_filenames():
log_request.selected_start_date.strftime("%Y-%m-%d"),
log_request.selected_end_date.strftime("%Y-%m-%d"),
)
valid_filenames_set.add(os.path.basename(full_path))
valid_filenames_set.add(full_path)
return valid_filenames_set


Expand Down
13 changes: 13 additions & 0 deletions kolibri/core/utils/csv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import io
import logging
import re
from contextlib import contextmanager
from numbers import Number

from django.core.files.storage import default_storage

logger = logging.getLogger(__name__)


@contextmanager
def open_csv_for_writing(filename):
Expand All @@ -17,6 +20,11 @@ def open_csv_for_writing(filename):
yield encoded_fh
encoded_fh.flush()
default_storage.save(filename, f)
logger.info("CSV file {} updated".format(filename))
try:
logger.info("File path: {}".format(default_storage.path(filename)))
except NotImplementedError:
logger.info("File url: {}".format(default_storage.url(filename)))
else:
# If the file does not exist, we need to create it and return it wrapped in a TextIOWrapper
with io.BytesIO() as f:
Expand All @@ -30,6 +38,11 @@ def open_csv_for_writing(filename):
yield encoded_fh
encoded_fh.flush()
default_storage.save(filename, f)
logger.info("CSV file {} saved".format(filename))
try:
logger.info("File path: {}".format(default_storage.path(filename)))
except NotImplementedError:
logger.info("File url: {}".format(default_storage.url(filename)))


@contextmanager
Expand Down
4 changes: 0 additions & 4 deletions kolibri/deployment/default/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@

if not os.environ.get("DEFAULT_FILE_STORAGE"):
if conf.OPTIONS["FileStorage"]["STORAGE_BACKEND"] == "gcs":
# Initialize GS_CREDENTIALS as a proper google.auth.credentials.Credentials object
import google.auth

GS_CREDENTIALS, _ = google.auth.default()
# Options per https://django-storages.readthedocs.io/en/latest/backends/gcloud.html
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
GS_BUCKET_NAME = conf.OPTIONS["FileStorage"]["GS_BUCKET_NAME"]
Expand Down
4 changes: 3 additions & 1 deletion kolibri/plugins/facility/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from kolibri.core.logger.models import ContentSessionLog
from kolibri.core.logger.models import GenerateCSVLogRequest


CSV_EXPORT_FILENAMES = {}
CSV_EXPORT_FILENAMES.update(LOGGER_CSV_EXPORT_FILENAMES)
CSV_EXPORT_FILENAMES.update(USER_CSV_EXPORT_FILENAMES)
Expand Down Expand Up @@ -103,6 +102,9 @@ def exported_csv_info(request, facility_id):
if log_request is not None:
start = log_request.selected_start_date.isoformat()
end = log_request.selected_end_date.isoformat()
filename = CSV_EXPORT_FILENAMES[log_type].format(
facility.name, facility.id[:4], start[:10], end[:10]
)
else:
start = ""
end = ""
Expand Down
3 changes: 2 additions & 1 deletion requirements/storages.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Additional reqs for running kolibri with GCS file storage backend
django-storages[google]==1.14.2
django-storages[google]==1.14.5
google-auth==2.38.0

0 comments on commit 07db9d6

Please sign in to comment.