Skip to content

Commit

Permalink
added logic for user friendly download name
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Jul 29, 2022
1 parent 937ddc8 commit 61bf42d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
16 changes: 10 additions & 6 deletions tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ def fdownload(fname):
valid=validators.url(fname)
if valid==True:
download_url = fname
try:
resp = requests.head(download_url)
filesize_bytes=int(resp.headers['Content-Length'])
except:
filesize_bytes=self.filesize_bytes
absolute_download_url=download_url
try :
value = download_url.split('/')
name=value[-1]
fname=name
split_name=name.split('_')
download_name=f"{split_name[0]}_{split_name[-1]}" # getting human redable name ignoring unique id
fname=download_name
except:
fname=f"""{self.run.job.name}_{self.name}.zip"""
try:
with open(os.path.join(settings.EXPORT_DOWNLOAD_ROOT, str(self.run.uid),f"{name}_size.txt")) as f :
size=f.readline()
filesize_bytes=int(size)
except:
filesize_bytes=0

else:
try:
filesize_bytes = os.path.getsize(os.path.join(settings.EXPORT_DOWNLOAD_ROOT, str(self.run.uid), fname).encode('utf-8'))
Expand Down
41 changes: 35 additions & 6 deletions tasks/task_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ def run_task(self, job_uid=None, user=None, ondemand=True): # noqa
LOG.debug('Saved task: {0}'.format(format_name))

if ondemand:
# run_task_remote(run_uid)
# db.close_old_connections()
run_task_async_ondemand.send(run_uid)
run_task_remote(run_uid)
db.close_old_connections()
# run_task_async_ondemand.send(run_uid)
else:
# run_task_remote(run_uid)
# db.close_old_connections()
run_task_async_scheduled.send(run_uid)
run_task_remote(run_uid)
db.close_old_connections()
# run_task_async_scheduled.send(run_uid)
return run

@dramatiq.actor(max_retries=0,queue_name='default',time_limit=1000*60*60*6)
Expand Down Expand Up @@ -283,6 +283,13 @@ def add_metadata(z,theme):
if geojson :
try:
response_back=geojson.fetch('GeoJSON',is_hdx_export=True)
try:
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes'][0]))
except:
LOG.error("Can not write filesize to text")
finish_task('geojson',response_back=response_back)
except Exception as ex :
raise ex
Expand All @@ -305,6 +312,13 @@ def add_metadata(z,theme):
if shp:
try:
response_back=shp.fetch('shp',is_hdx_export=True)
try:
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes'][0]))
except:
LOG.error("Can not write filesize to text")
finish_task('shp',response_back=response_back)
except Exception as ex:
raise ex
Expand Down Expand Up @@ -383,7 +397,15 @@ def add_metadata(z,theme):
if geojson :
try:
response_back=geojson.fetch('GeoJSON')
try:
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes'][0]))
except:
LOG.error("Can not write filesize to text")
finish_task('geojson',response_back=response_back)

except Exception as ex :
raise ex

Expand All @@ -396,6 +418,13 @@ def add_metadata(z,theme):
if shp:
try :
response_back=shp.fetch('shp')
try:
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes'][0]))
except:
LOG.error("Can not write filesize to text")
finish_task('shp',response_back=response_back)
except Exception as ex :
raise ex
Expand Down

1 comment on commit 61bf42d

@kshitijrajsharma
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.