mirror of
https://github.com/pinry/pinry.git
synced 2026-01-19 21:52:06 +01:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user