mirror of
https://github.com/Tiendil/pynames.git
synced 2025-09-22 04:40:47 +02:00
fixed utils.is_file to correctly process django.core.files.uploadedfile.UploadedFile
This commit is contained in:
parent
4f316c0ddc
commit
23d3e74a6e
2 changed files with 34 additions and 1 deletions
29
pynames/tests/test_utils.py
Normal file
29
pynames/tests/test_utils.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
from pynames.utils import is_file
|
||||||
|
|
||||||
|
try:
|
||||||
|
from django.core.files import File
|
||||||
|
from django.core.files.base import ContentFile
|
||||||
|
from django.core.files.uploadedfile import UploadedFile
|
||||||
|
except:
|
||||||
|
UploadedFile = None
|
||||||
|
File = None
|
||||||
|
ContentFile = None
|
||||||
|
|
||||||
|
|
||||||
|
class TestName(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_is_file(self):
|
||||||
|
some_file = tempfile.NamedTemporaryFile()
|
||||||
|
self.assertTrue(is_file(some_file))
|
||||||
|
some_file.close()
|
||||||
|
|
||||||
|
def test_is_file_on_django_files(self):
|
||||||
|
if File and UploadedFile and ContentFile:
|
||||||
|
self.assertTrue(is_file(UploadedFile('mock')))
|
||||||
|
self.assertTrue(is_file(File('mock')))
|
||||||
|
self.assertTrue(is_file(ContentFile('mock')))
|
|
@ -49,4 +49,8 @@ def is_file(obj):
|
||||||
Suitable to check both builtin ``file`` and ``django.core.file.File`` instances.
|
Suitable to check both builtin ``file`` and ``django.core.file.File`` instances.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return all([callable(getattr(obj, method_name, None)) for method_name in ('next', '__enter__', '__exit__')])
|
return all(
|
||||||
|
[callable(getattr(obj, method_name, None)) for method_name in ('__enter__', '__exit__')]
|
||||||
|
+
|
||||||
|
[any([callable(getattr(obj, method_name, None)) for method_name in ('next', '__iter__')])]
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue