From 98876bb0bd445bd8ea4988008fe01b614853c747 Mon Sep 17 00:00:00 2001 From: Krzysztof Klimonda Date: Tue, 19 Mar 2013 15:22:20 +0100 Subject: [PATCH] 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. --- pinry/core/models.py | 10 ++++++---- requirements.txt | 1 + setup.py | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pinry/core/models.py b/pinry/core/models.py index b648c3a..9b6f85f 100644 --- a/pinry/core/models.py +++ b/pinry/core/models.py @@ -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 \ No newline at end of file + return self.url diff --git a/requirements.txt b/requirements.txt index bf70bb7..5d7d04b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py index 48a7d30..26f6b0b 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ install_requires = [ 'django-tastypie', 'django-braces', 'django_compressor', + 'requests', 'django-images>=2013.1.99', 'django-taggit>=0.9.3.99' ]