Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading vote table from xlsx file - AttributeError: 'SpooledTemporaryFile' object has no attribute 'seekable' #95

Open
PeturOA opened this issue Nov 18, 2020 · 1 comment

Comments

@PeturOA
Copy link
Collaborator

PeturOA commented Nov 18, 2020

I was running some simulations recently, and discovered that the file upload had broken.
I do not remember when I last tried to upload a vote table xlsx file, so I do not know exactly when this became a problem,
but at least it was most definitely not a problem when I was working on the file upload feature last year.

The issue is when uploading votes from an xlsx file, and loading the filestream into a workbook using openpyxl.
The filestream is of type SpooledTemporaryFile, but openpyxl seems to expect it to implement BufferedIOBase or something like that (I'm no expert in these IO implementations)

Here's the relevant code:

@app.route('/api/votes/upload/', methods=['POST'])
def upload_votes():
    ...
    f = request.files['file']
    res = util.load_votes_from_stream(f.stream, f.filename)
    ...
def load_votes_from_stream(stream, filename):
    rd = []
    if filename.endswith(".csv"):
        ...
    elif filename.endswith(".xlsx"):
        book = openpyxl.load_workbook(stream)
        ...
    else:
        return None, None, None

I found this related werkzeug issue from two years ago.
pallets/werkzeug#1344

It seems to have been resolved, so I tried just updating werkzeug and flask, as well as openpyxl.
But that did not solve this, so I added a couple of lines to fix this, although I'm not satisfied with that solution:

if not hasattr(stream, "seekable") and hasattr(stream, "_file"):
    stream.seekable = stream._file.seekable

See PR: #94

@PeturOA PeturOA changed the title Uploading vote table from xlsx file: 'SpooledTemporaryFile' object has no attribute 'seekable' Uploading vote table from xlsx file - AttributeError: 'SpooledTemporaryFile' object has no attribute 'seekable' Nov 18, 2020
@smari
Copy link
Owner

smari commented Nov 18, 2020

Merged the temporary fix but leaving this issue open, as the XLSX import apparently needs a more permanent fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants