Skip to content

Commit

Permalink
fix: modify save method for ProductRaster
Browse files Browse the repository at this point in the history
  • Loading branch information
jkgeo committed Dec 20, 2024
1 parent e1b001f commit 19a034e
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions glam_api/glam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)

from config.utils import generate_unique_slug
from glam.utils import extract_datetime_from_filename

if not settings.USE_S3:
raster_storage = FileSystemStorage()
Expand Down Expand Up @@ -516,25 +517,18 @@ class ProductRaster(models.Model):
def __str__(self):
return self.slug

def upload_file(self, new=False):
def upload_file(self):
"""
Function to upload file to raster storage. Triggered on object save.
If dataset is new, create file_object using local_path.
Method necessary for file upload to s3 using django-storages
"""
print("uhoh")
if new:
with open(self.local_path, "rb") as f:
self.file_object = File(f, name=os.path.basename(f.name))
self.save()
with open(self.local_path, "rb") as f:
self.file_object = File(f, name=os.path.basename(f.name))
self.save()

def save(self, *args, **kwargs):

if "new" in kwargs:
new = kwargs["new"]
else:
new = self.pk is None

if not self.name:
# generate name
base_file = os.path.basename(self.local_path)
Expand All @@ -548,15 +542,7 @@ def save(self, *args, **kwargs):
if not self.date:
# get date from file name
base_file = os.path.basename(self.local_path)
parts = base_file.split(".")
try:
ds_date = datetime.datetime.strptime(
f"{parts[-3]}.{parts[-2]}", "%Y.%j"
).strftime("%Y-%m-%d")
except:
ds_date = datetime.datetime.strptime(parts[-2], "%Y-%m-%d").strftime(
"%Y-%m-%d"
)
ds_date = extract_datetime_from_filename(base_file)
self.date = ds_date

# to do
Expand All @@ -565,8 +551,6 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

self.upload_file(new)

class Meta:
verbose_name = "product dataset"

Expand Down

0 comments on commit 19a034e

Please sign in to comment.