Use requests library instead of raw urllib2

This commit is a foundation upon a more robust image fetching code
will be built in the future, and it already fixes at least one weird
issue I've seen.
This commit is contained in:
Krzysztof Klimonda
2013-03-19 15:22:20 +01:00
parent 99d7eba641
commit 98876bb0bd
3 changed files with 8 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import urllib2
import requests
from cStringIO import StringIO
from django.core.files.uploadedfile import InMemoryUploadedFile
@@ -15,8 +15,10 @@ class ImageManager(models.Manager):
def create_for_url(self, url):
file_name = url.split("/")[-1]
buf = StringIO()
buf.write(urllib2.urlopen(url).read())
obj = InMemoryUploadedFile(buf, 'image', file_name, None, buf.tell(), None)
response = requests.get(url)
buf.write(response.content)
obj = InMemoryUploadedFile(buf, 'image', file_name,
None, buf.tell(), None)
return Image.objects.create(image=obj)
@@ -37,4 +39,4 @@ class Pin(models.Model):
tags = TaggableManager()
def __unicode__(self):
return self.url
return self.url

View File

@@ -6,5 +6,6 @@ django-braces
mock
factory-boy
django_compressor
requests
http://github.com/kklimonda/django-images/tarball/master#egg=django-images
http://github.com/hcarvalhoalves/django-taggit/tarball/master#egg=django-taggit

View File

@@ -20,6 +20,7 @@ install_requires = [
'django-tastypie',
'django-braces',
'django_compressor',
'requests',
'django-images>=2013.1.99',
'django-taggit>=0.9.3.99'
]