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()
def create_deck_file(deck_id): """Creates a spreadsheet containing a deck of cards.""" deck = Deck.objects.get(id=deck_id) output = StringIO.StringIO() workbook = xlwt.Workbook(encoding='utf8') worksheet = workbook.add_sheet('sheet1') card_list = queries.getDeckCardsList(deck_id) row = 0 for card in card_list: if row == 0: for idx, field in enumerate(card['fields']): worksheet.write(row, idx, label=field['label']) row = row + 1 for idx, field in enumerate(card['fields']): field_value = field['value'] field_type = field['type'] if field_type in ('I', 'A'): field_value = os.path.split(field_value)[ 1] # strips the media folder path worksheet.write(row, idx, label=field_value) workbook.save(output) file_output = workbook #file_output = output.getvalue() output.close() return file_output
def create_deck_file(deck_id): """Creates a spreadsheet containing a deck of cards.""" deck = Deck.objects.get(id=deck_id) output = StringIO.StringIO() workbook = xlwt.Workbook(encoding='utf8') worksheet = workbook.add_sheet('sheet1') card_list = queries.getDeckCardsList(deck_id) row = 0 for card in card_list: if row == 0: for idx, field in enumerate(card['fields']): worksheet.write(row, idx, label=field['label']) row = row + 1 for idx, field in enumerate(card['fields']): field_value = field['value'] field_type = field['type'] if field_type in ('I','A'): field_value = os.path.split(field_value)[1] # strips the media folder path worksheet.write(row, idx, label=field_value) workbook.save(output) file_output = workbook #file_output = output.getvalue() output.close() return file_output