def handle_uploaded_deck_file(deck, uploaded_file): """Handles an uploaded deck file.""" cached_file_contents = uploaded_file.read() mappings = None try: [file_contents, mappings] = handle_zipped_deck_file(deck, uploaded_file) except zipfile.BadZipfile: file_contents = cached_file_contents if not utils.template_matches_file(deck.collection.card_template, file_contents): raise Exception, "The fields in the spreadsheet don't match those in the template." try: parsed_cards = utils.parse_deck_template_file(deck.collection.card_template, file_contents, mappings) except: raise Exception, "Uploaded file type not supported." add_cards_to_deck(deck, parsed_cards)
def handle_custom_file(uploaded_file, course_name, user, is_teacher=False): """Handles an uploaded custom deck file.""" cached_file_contents = uploaded_file.read() mappings = None try: [file_contents, zfile, file_names, path_to_excel] = extract_from_zip(uploaded_file) is_zip = True except zipfile.BadZipfile: file_contents = cached_file_contents is_zip = False if not utils.correct_custom_format(file_contents): raise Exception, "Incorrect format of the spreadsheet." card_template_fields = utils.get_card_template(file_contents) card_template = CardTemplate(title=course_name, owner=user) card_template.save() for template_field in card_template_fields: label = template_field['label'] side = template_field['side'] sort_order = template_field['sort_order'] if side == 'Front': display = True else: display = False type = template_field['type'][0] field = Field(label=label, field_type=type, show_label=True, display=display, sort_order=sort_order) field.save() card_template_field = CardTemplates_Fields(card_template=card_template, field=field) card_template_field.save() collection = Collection(title=course_name, card_template=card_template, published=not is_teacher) collection.save() user_collection = Users_Collections(user=user, date_joined=date.today(), collection=collection, role=Users_Collections.ADMINISTRATOR) user_collection.save() deck = create_deck(collection_id=collection.id, deck_title='Untitled Deck') if is_zip: [file_contents, mappings] = get_mappings_from_zip(deck, file_contents, file_names, zfile, path_to_excel, custom=True) try: parsed_cards = utils.parse_deck_template_file(deck.collection.card_template, file_contents, mappings, custom=True) except: raise Exception, "Uploaded file type not supported." add_cards_to_deck(deck, parsed_cards) return deck
def handle_uploaded_deck_file(deck, uploaded_file): """Handles an uploaded deck file.""" cached_file_contents = uploaded_file.read() mappings = None try: [file_contents, mappings] = handle_zipped_deck_file(deck, uploaded_file) except zipfile.BadZipfile: file_contents = cached_file_contents if not utils.template_matches_file(deck.collection.card_template, file_contents): raise Exception, "The fields in the spreadsheet don't match those in the template." try: parsed_cards = utils.parse_deck_template_file( deck.collection.card_template, file_contents, mappings) except: raise Exception, "Uploaded file type not supported." add_cards_to_deck(deck, parsed_cards)
def handle_custom_file(uploaded_file, course_name, user, is_teacher=False): """Handles an uploaded custom deck file.""" cached_file_contents = uploaded_file.read() mappings = None try: [file_contents, zfile, file_names, path_to_excel] = extract_from_zip(uploaded_file) is_zip = True except zipfile.BadZipfile: file_contents = cached_file_contents is_zip = False if not utils.correct_custom_format(file_contents): raise Exception, "Incorrect format of the spreadsheet." card_template_fields = utils.get_card_template(file_contents) card_template = CardTemplate(title=course_name, owner=user) card_template.save() for template_field in card_template_fields: label = template_field['label'] side = template_field['side'] sort_order = template_field['sort_order'] if side == 'Front': display = True else: display = False type = template_field['type'][0] field = Field(label=label, field_type=type, show_label=True, display=display, sort_order=sort_order) field.save() card_template_field = CardTemplates_Fields(card_template=card_template, field=field) card_template_field.save() collection = Collection(title=course_name, card_template=card_template, published=not is_teacher) collection.save() user_collection = Users_Collections(user=user, date_joined=date.today(), collection=collection, role=Users_Collections.ADMINISTRATOR) user_collection.save() deck = create_deck(collection_id=collection.id, deck_title='Untitled Deck') if is_zip: [file_contents, mappings] = get_mappings_from_zip(deck, file_contents, file_names, zfile, path_to_excel, custom=True) try: parsed_cards = utils.parse_deck_template_file( deck.collection.card_template, file_contents, mappings, custom=True) except: raise Exception, "Uploaded file type not supported." add_cards_to_deck(deck, parsed_cards) return deck