def create_zip_deck_file(deck): """Creates a zipped file containing the contents of the deck (XLS and media objects.""" # create the string buffer to hold the contents of the zip file s = StringIO() # create the zipfile object zfile = zipfile.ZipFile(s, "w") # write the deck XLS file to the zip deck_file_output = utils.create_deck_file(deck.id) temp_dirpath = tempfile.mkdtemp() temp_filepath = os.path.join(temp_dirpath, "deck.xls") deck_file_output.save(temp_filepath) zfile.write(temp_filepath, arcname=os.path.split(temp_filepath)[1]) shutil.rmtree(temp_dirpath) # must delete temp dir when we're done # lookup the unique field values in the deck of cards, # where the field values are the media object names card_list = queries.getDeckCardsList(deck.id) field_set = set() for c in card_list: for f in c['fields']: if f['type'] not in ('T', 'M'): field_set.add(f['value']) # add each media object ot the zip file for file_name in field_set: file_contents = MediaStoreService.readFileContents(file_name) if file_contents is not None: zfile.writestr(file_name, file_contents) zfile.close() return s.getvalue()