django-commons / drf-excel

An XLSX spreadsheet renderer for Django REST Framework.
BSD 3-Clause "New" or "Revised" License
217 stars 40 forks source link

fix(windows): replace NamedTemporaryFile with TemporaryFile #74

Closed rdmolony closed 1 year ago

rdmolony commented 1 year ago

Hi there,

Thanks for creating & maintaining drf-excel 😃

I'm running drf on Windows Server 2019 (Datacenter) for legacy/historical reasons. This version blocks NamedTemporaryFile from saving to temp dir.

https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file

TemporaryFile seems to work fine across platforms in its place as it is not blocked by the dreaded permissions

FlipperPA commented 1 year ago

Huh, it is odd that NamedTemporaryFile got used without using an explicit name. Let me take a look a bit more, but in concept I'll get this merged in a day or two.

AlexKovyazin commented 1 year ago

Got this problem too. https://stackoverflow.com/questions/66744497/python-tempfile-namedtemporaryfile-cant-use-generated-tempfile

The following adjustments also solved the problem:

def _save_virtual_workbook(self, wb):
    try:
        tmp = NamedTemporaryFile(delete=False)
        save_workbook(wb, tmp.name)
        tmp.seek(0)
        virtual_workbook = tmp.read()
    finally:
        tmp.close()
        os.unlink(tmp.name)

    return virtual_workbook
FlipperPA commented 1 year ago

Sorry for the delay, just back from vacation. I'll get a version published to PyPI today. @rdmolony @AlexKovyazin