예제 #1
0
def get_mappings_from_zip(deck,
                          file_contents,
                          file_names,
                          zfile,
                          path_to_excel,
                          custom=False):
    """
    Checks if all the files in the excel file are in the zipped folder.
    Saves the files and returns the mappings between the file names and their paths
    """
    mappings = {'Image': {}, 'Audio': {}}

    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."

    files = []
    files_not_found = []
    files_to_upload = utils.get_file_names(deck.collection.card_template,
                                           file_contents,
                                           custom=custom)
    for f in files_to_upload:
        file_map = {"absolute": os.path.join(path_to_excel, f), "relative": f}
        if file_map['absolute'] in file_names:
            files.append(file_map)
        else:
            files_not_found.append(file_map['relative'])

    if len(files_not_found):
        raise Exception, "File(s) not found in the zipped folder: %s" % str(
            files_not_found)[1:-1]

    temp_dir_path = tempfile.mkdtemp()
    for file in files:
        zfile.extract(file['absolute'], temp_dir_path)
        temp_file_path = os.path.join(temp_dir_path, file['absolute'])

        is_valid_image, errstr = valid_image_file_type(temp_file_path)
        if is_valid_image:
            store_file_name = handle_uploaded_media_file(temp_file_path, 'I')
            mappings['Image'][file['relative']] = store_file_name
        else:
            is_valid_audio, errstr = valid_audio_file_type(temp_file_path)
            if is_valid_audio:
                store_file_name = handle_uploaded_media_file(
                    temp_file_path, 'A')
                mappings['Audio'][file['relative']] = store_file_name

    if len(files):
        shutil.rmtree(temp_dir_path)

    return [file_contents, mappings]
예제 #2
0
def get_mappings_from_zip(deck, file_contents, file_names, zfile, path_to_excel, custom=False):
    """
    Checks if all the files in the excel file are in the zipped folder.
    Saves the files and returns the mappings between the file names and their paths
    """
    mappings = {'Image':{}, 'Audio':{}}

    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."

    files = []
    files_not_found = []
    files_to_upload = utils.get_file_names(deck.collection.card_template, file_contents, custom=custom)
    for f in files_to_upload:
        file_map = {
            "absolute": os.path.join(path_to_excel, f),
            "relative": f
        }
        if file_map['absolute'] in file_names:
            files.append(file_map)
        else:
            files_not_found.append(file_map['relative'])

    if len(files_not_found):
        raise Exception, "File(s) not found in the zipped folder: %s" %str(files_not_found)[1:-1]

    temp_dir_path = tempfile.mkdtemp()
    for file in files:
        zfile.extract(file['absolute'], temp_dir_path)
        temp_file_path = os.path.join(temp_dir_path, file['absolute'])

        is_valid_image, errstr = valid_image_file_type(temp_file_path)
        if is_valid_image:
            store_file_name = handle_uploaded_media_file(temp_file_path, 'I')
            mappings['Image'][file['relative']] = store_file_name
        else:
            is_valid_audio, errstr = valid_audio_file_type(temp_file_path)
            if is_valid_audio:
                store_file_name = handle_uploaded_media_file(temp_file_path, 'A')
                mappings['Audio'][file['relative']] = store_file_name

    if len(files):
        shutil.rmtree(temp_dir_path)

    return [file_contents, mappings]
예제 #3
0
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)
예제 #4
0
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)