python django dynamic zipping and download
Question: I have a checkbox form. I am trying to make it so if a user checks the "video"
box and submits the form then the video files gets zipped and django generates
a dynamic download that pops up on the user screen. I want the same to happen
for the audio. BUT if the user selects both checkboxes and submits, the the
audio and video files are combined in a zip and a download pops up for the
user. I dont want any of this stored on the disk so I am using StringIO. No
download popup comes up

html

    
    
    <form>
    <input type="checkbox" name="list" value="audio"/> Audio<br />
    <input type="checkbox" name="list" value="video"/> Video<br />
    <input type="submit" value="submit" /> 
    </form>
    

python

    
    
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    import cgitb; cgitb.enable()
    import cgi
    from zipfile import ZipFile
    from StringIO import StringIO
    from django.http import HttpResponse
    from django.core.servers.basehttp import FileWrapper
    files = 0
    def zip (content):
        buffer= StringIO.StringIO()
        z= zipfile.ZipFile( buffer, "w" )
        z.writestr(content)
        z.close()
        # generate the file
        response = HttpResponse(FileWrapper(z), content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename=z.zip'
        return response
    form = cgi.FieldStorage()
    mylist = form.getlist('list')
    print 'Content-Type: text/plain\n'
    for item in mylist:
      if item == 'video':
        files = '../download/video.html'
        zip(files)
      elif item == 'audio':
        files = '../download/audio.html'
        zip(files)
      elif item == 'audio' and 'video':
        files = '../download/audio.html'+'../download/video.html'
        zip(files)
      else:
        print 'nothing selected'
    


Answer: For sure the for loop cant work, the third code within the last elif will
never be executed since it is the same as the second `item == 'audio' and
'video'` is the same as `item == 'audio and True'` which is the same as `item
== 'audio'`

I would change the loop code to look a bit more simple

    
    
    files = []
    for item in mylist:
      if item == 'video':
        files.append('../download/video.html')
      elif item == 'audio':
        files.append('../download/audio.html')
    zip(files)
    

and then refacor the zip to handle lists of files

    
    
    def zip (filenames):
        buffer= StringIO.StringIO()
        z= zipfile.ZipFile( buffer, "w" )
        [z.writestr(f) for f in filenames]
        z.close()
        # generate the file
        response = HttpResponse(FileWrapper(z), content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename=z.zip'
        return response
    

You also have to make sure that the view functions returns what the zip
function returns.

