﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26644	SuspiciousFileOperation when creating a File from a NamedTemporaryFile	Hugo Osvaldo Barrera	Hugo Osvaldo Barrera	"This code snippet worked fine on django 1.9. It should still work on django 1.10.

{{{
from tempfile import NamedTemporaryFile
from django.core.files.base import File
from . import pdf


with NamedTemporaryFile(suffix='.pdf') as file_:
            pdf.generate_receipt_pdf(self.receipt_id, file_)
            self.pdf_file = File(file_)
            self.save()
}}}

However, the following exception is raised on django1.10:

{{{
Traceback (most recent call last):
  File ""/home/hugo/workspace/Hugo/django-afip/testapp/testapp/testmain/tests.py"", line 305, in test_pdf_generation
    pdf.save_pdf()
  File ""/home/hugo/workspace/Hugo/django-afip/testapp/django_afip/models.py"", line 863, in save_pdf
    self.save()
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/db/models/base.py"", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/db/models/base.py"", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/db/models/base.py"", line 886, in _save_table
    for f in non_pks]
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/db/models/base.py"", line 886, in <listcomp>
    for f in non_pks]
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/db/models/fields/files.py"", line 287, in pre_save
    file.save(file.name, file, save=False)
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/db/models/fields/files.py"", line 90, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/core/files/storage.py"", line 53, in save
    name = self.get_available_name(name, max_length=max_length)
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/core/files/storage.py"", line 77, in get_available_name
    while self.exists(name) or (max_length and len(name) > max_length):
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/core/files/storage.py"", line 394, in exists
    return os.path.exists(self.path(name))
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/core/files/storage.py"", line 407, in path
    return safe_join(self.location, name)
  File ""/home/hugo/workspace/Hugo/django-afip/.tox/py35-django/lib/python3.5/site-packages/django/utils/_os.py"", line 78, in safe_join
    'component ({})'.format(final_path, base_path))
django.core.exceptions.SuspiciousFileOperation: The joined path (/tmp/tmpbwfln73d.pdf) is located outside of the base path component (/home/hugo/workspace/Hugo/django-afip/testapp/media)
}}}

I believe that there's something wrong with how `File` is now determining the resulting file's name: in previous versions of django, this file  *did not* end up in `/tmp`, but rather inside `media/`."	Bug	closed	Documentation	1.10	Normal	wontfix	File, SuspiciousFileOperation, NamedTemporaryFile, regression 1.10		Accepted	0	0	0	0	0	0
