Example #1
0
    def testDownloadingImages(self):
        good_url = ur"http://media-cache-ec0.pinimg.com/236x/30/29/95/302995ee78f19d5ccb0d8061f1f4cadc.jpg"
        self.assertIsNotNone(download_image_file(good_url))

        image_too_large = ur"https://sites.google.com/site/dressbookproject/xxx"
        with self.assertRaises(ValidationError):
            download_image_file(image_too_large)

        not_image = ur"http://www.gazeta.pl/0,0.html"
        with self.assertRaises(ValidationError):
            download_image_file(not_image)
Example #2
0
    def to_python(self, data):
        if data[0]:
            return super(UrlOrUploadImageField, self).to_python(data[0])

        if not data[1]:
            return None

        try:
            self.url = data[1]

            #             opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(), urllib2.HTTPCookieProcessor())
            #             opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0')]
            #             inStream = opener.open(url)
            #
            #             content_type = inStream.info().getheader('Content-Type')
            #             if not content_type or not content_type.lower().startswith("image"):
            #                 logger.error(u'Url %s does not point to image' % url)
            #                 raise ValidationError(self.error_messages['url_not_image'])
            #
            #             file_name = url.rsplit('/', 1)[1]
            #             ext = file_name.rsplit('.', 1)[1].lower()
            #             if ext not in djsettings.ALLOW_FILE_TYPES:
            #                 ext = content_type.rsplit('/', 1)[1].lower()
            #                 if ext not in djsettings.ALLOW_FILE_TYPES:
            #                     raise ValidationError(self.error_messages['url_not_image'])
            #                 file_name = "file.%s" % ext
            #
            #             img_temp = StringIO()
            #             size = 0
            #             while True:
            #                 s = inStream.read(djsettings.ALLOW_MAX_FILE_SIZE / 8)
            #                 if not s:
            #                     break
            #                 img_temp.write(s)
            #                 size += len(s)
            #                 if size >= djsettings.ALLOW_MAX_FILE_SIZE:
            #                     raise ValidationError(self.error_messages['file_too_large'])
            #
            #             img_temp.seek(0)
            #             file_object = File(img_temp, file_name)
            #             file_object.size = size
            file_object = download_image_file(self.url)
            return super(UrlOrUploadImageField, self).to_python(file_object)
        except ValidationError:
            raise
        #         except urllib2.URLError as e:
        #             logger.error('Unable to download image from url %s error %s, code %d' % (url, str(e), e.code))
        #             raise ValidationError(str(self.error_messages['download_failed']))
        except:
            raise ValidationError(self.error_messages["invalid_image"])