Skip to content

Commit 21b32e7

Browse files
committed
Port /iesg/agenda/telechat-YYYY-MM-DD-docs.tgz, use tarfile.addfile
and StringIO to avoid the temporary file - Legacy-Id: 6407
1 parent a81af95 commit 21b32e7

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

ietf/iesg/views.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
import codecs, re, os, glob
3636
import datetime
37-
import tarfile
37+
import tarfile, StringIO, time
3838

3939
from django.views.generic.simple import direct_to_template
4040
from django.core.urlresolvers import reverse as urlreverse
@@ -352,36 +352,40 @@ def agenda_documents(request):
352352
telechats.append({'date':date, 'docs':res})
353353
return direct_to_template(request, 'iesg/agenda_documents.html', { 'telechats':telechats })
354354

355-
def telechat_docs_tarfile(request,year,month,day):
356-
from tempfile import mkstemp
357-
date=datetime.date(int(year),int(month),int(day))
358-
if settings.USE_DB_REDESIGN_PROXY_CLASSES:
359-
from ietf.doc.models import TelechatDocEvent
360-
docs = []
361-
for d in IDInternal.objects.filter(docevent__telechatdocevent__telechat_date=date).distinct():
362-
if d.latest_event(TelechatDocEvent, type="scheduled_for_telechat").telechat_date == date:
363-
docs.append(d)
364-
else:
365-
docs= IDInternal.objects.filter(telechat_date=date, primary_flag=1, agenda=1)
355+
def telechat_docs_tarfile(request, date):
356+
date = get_agenda_date(date)
357+
358+
docs = []
359+
for d in Document.objects.filter(docevent__telechatdocevent__telechat_date=date).distinct():
360+
if d.latest_event(TelechatDocEvent, type="scheduled_for_telechat").telechat_date == date:
361+
docs.append(d)
362+
366363
response = HttpResponse(mimetype='application/octet-stream')
367-
response['Content-Disposition'] = 'attachment; filename=telechat-%s-%s-%s-docs.tgz'%(year, month, day)
368-
tarstream = tarfile.open('','w:gz',response)
369-
mfh, mfn = mkstemp()
370-
manifest = open(mfn, "w")
364+
response['Content-Disposition'] = 'attachment; filename=telechat-%s-docs.tgz' % date.isoformat()
365+
366+
tarstream = tarfile.open('', 'w:gz', response)
367+
368+
manifest = StringIO.StringIO()
369+
371370
for doc in docs:
372-
doc_path = os.path.join(settings.INTERNET_DRAFT_PATH, doc.draft.filename+"-"+doc.draft.revision_display()+".txt")
371+
doc_path = os.path.join(settings.INTERNET_DRAFT_PATH, doc.name + "-" + doc.rev + ".txt")
373372
if os.path.exists(doc_path):
374373
try:
375-
tarstream.add(doc_path, str(doc.draft.filename+"-"+doc.draft.revision_display()+".txt"))
376-
manifest.write("Included: "+doc_path+"\n")
377-
except Exception, e:
378-
manifest.write(("Failed (%s): "%e)+doc_path+"\n")
374+
tarstream.add(doc_path, str(doc.name + "-" + doc.rev + ".txt"))
375+
manifest.write("Included: %s\n" % doc_path)
376+
except Exception as e:
377+
manifest.write("Failed (%s): %s\n" % (e, doc_path))
379378
else:
380-
manifest.write("Not found: "+doc_path+"\n")
381-
manifest.close()
382-
tarstream.add(mfn, "manifest.txt")
379+
manifest.write("Not found: %s\n" % doc_path)
380+
381+
manifest.seek(0)
382+
t = tarfile.TarInfo(name="manifest.txt")
383+
t.size = len(manifest.buf)
384+
t.mtime = time.time()
385+
tarstream.addfile(t, manifest)
386+
383387
tarstream.close()
384-
os.unlink(mfn)
388+
385389
return response
386390

387391
def discusses(request):

0 commit comments

Comments
 (0)