Skip to content

Commit 61bf42d

Browse files
added logic for user friendly download name
1 parent 937ddc8 commit 61bf42d

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

tasks/models.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,22 @@ def fdownload(fname):
9393
valid=validators.url(fname)
9494
if valid==True:
9595
download_url = fname
96-
try:
97-
resp = requests.head(download_url)
98-
filesize_bytes=int(resp.headers['Content-Length'])
99-
except:
100-
filesize_bytes=self.filesize_bytes
10196
absolute_download_url=download_url
10297
try :
10398
value = download_url.split('/')
10499
name=value[-1]
105-
fname=name
100+
split_name=name.split('_')
101+
download_name=f"{split_name[0]}_{split_name[-1]}" # getting human redable name ignoring unique id
102+
fname=download_name
106103
except:
107104
fname=f"""{self.run.job.name}_{self.name}.zip"""
105+
try:
106+
with open(os.path.join(settings.EXPORT_DOWNLOAD_ROOT, str(self.run.uid),f"{name}_size.txt")) as f :
107+
size=f.readline()
108+
filesize_bytes=int(size)
109+
except:
110+
filesize_bytes=0
111+
108112
else:
109113
try:
110114
filesize_bytes = os.path.getsize(os.path.join(settings.EXPORT_DOWNLOAD_ROOT, str(self.run.uid), fname).encode('utf-8'))

tasks/task_runners.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def run_task(self, job_uid=None, user=None, ondemand=True): # noqa
8181
LOG.debug('Saved task: {0}'.format(format_name))
8282

8383
if ondemand:
84-
# run_task_remote(run_uid)
85-
# db.close_old_connections()
86-
run_task_async_ondemand.send(run_uid)
84+
run_task_remote(run_uid)
85+
db.close_old_connections()
86+
# run_task_async_ondemand.send(run_uid)
8787
else:
88-
# run_task_remote(run_uid)
89-
# db.close_old_connections()
90-
run_task_async_scheduled.send(run_uid)
88+
run_task_remote(run_uid)
89+
db.close_old_connections()
90+
# run_task_async_scheduled.send(run_uid)
9191
return run
9292

9393
@dramatiq.actor(max_retries=0,queue_name='default',time_limit=1000*60*60*6)
@@ -283,6 +283,13 @@ def add_metadata(z,theme):
283283
if geojson :
284284
try:
285285
response_back=geojson.fetch('GeoJSON',is_hdx_export=True)
286+
try:
287+
for r in response_back:
288+
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
289+
with open(size_path, 'w') as f:
290+
f.write(str(r['zip_file_size_bytes'][0]))
291+
except:
292+
LOG.error("Can not write filesize to text")
286293
finish_task('geojson',response_back=response_back)
287294
except Exception as ex :
288295
raise ex
@@ -305,6 +312,13 @@ def add_metadata(z,theme):
305312
if shp:
306313
try:
307314
response_back=shp.fetch('shp',is_hdx_export=True)
315+
try:
316+
for r in response_back:
317+
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
318+
with open(size_path, 'w') as f:
319+
f.write(str(r['zip_file_size_bytes'][0]))
320+
except:
321+
LOG.error("Can not write filesize to text")
308322
finish_task('shp',response_back=response_back)
309323
except Exception as ex:
310324
raise ex
@@ -383,7 +397,15 @@ def add_metadata(z,theme):
383397
if geojson :
384398
try:
385399
response_back=geojson.fetch('GeoJSON')
400+
try:
401+
for r in response_back:
402+
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
403+
with open(size_path, 'w') as f:
404+
f.write(str(r['zip_file_size_bytes'][0]))
405+
except:
406+
LOG.error("Can not write filesize to text")
386407
finish_task('geojson',response_back=response_back)
408+
387409
except Exception as ex :
388410
raise ex
389411

@@ -396,6 +418,13 @@ def add_metadata(z,theme):
396418
if shp:
397419
try :
398420
response_back=shp.fetch('shp')
421+
try:
422+
for r in response_back:
423+
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
424+
with open(size_path, 'w') as f:
425+
f.write(str(r['zip_file_size_bytes'][0]))
426+
except:
427+
LOG.error("Can not write filesize to text")
399428
finish_task('shp',response_back=response_back)
400429
except Exception as ex :
401430
raise ex

0 commit comments

Comments
 (0)