def __init__(self, path, upload_to=None, preview_w=None, preview_h=None): self.upload_to = upload_to self.preview_width = preview_w self.preview_height = preview_h self.metadata = {} if not path: self.name = None return if '%' in path: path = urlunquote_plus(path) if path.startswith(settings.MEDIA_URL): # Strips leading MEDIA_URL, if starts with self._path = get_relative_media_url(path, clean_slashes=False) elif re.search(r'^(?:http(?:s)?:)?//', path): # url on other server? download it. self._path = self.download_image_url(path) else: abs_path = get_media_path(path) if os.path.exists(abs_path): self._path = get_relative_media_url(abs_path) if not self._path or not os.path.exists(os.path.join(settings.MEDIA_ROOT, self._path)): self.name = None return super(ImageFile, self).__init__(self._path) if self: self.preview_image = self.get_for_size('preview')
def test_get_media_path(self): from generic_plus.utils import get_media_path img_name = '/test/some-test-image.jpg' from_url = settings.MEDIA_URL + img_name to_url = settings.MEDIA_ROOT + img_name self.assertEqual(get_media_path(from_url), to_url)
def test_post_request(self): img_file = open(os.path.join(self.TEST_IMG_DIR, 'img.jpg'), 'rb') data = { u'image': img_file, u'upload_to': [u'test'], u'image_element_id': u'mt_image', u'md5': u'', u'preview_height': u'500', u'preview_width': u'800', u'sizes': u''' [{ "auto": [{ "max_w": null, "retina": 0, "min_h": 1, "name": "lead", "w": 570, "h": null, "min_w": 570, "__type__": "Size", "max_h": null, "label": "Lead" }, { "max_w": null, "retina": 0, "min_h": 110, "name": "featured_small", "w": 170, "h": 110, "min_w": 170, "__type__": "Size", "max_h": null, "label": "Featured Small" }, { "max_w": null, "retina": 0, "min_h": 250, "name": "featured_large", "w": 386, "h": 250, "min_w": 386, "__type__": "Size", "max_h": null, "label": "Featured Large" }], "retina": 0, "name": "lead_large", "h": null, "min_w": 615, "__type__": "Size", "max_h": null, "label": "Lead Large", "max_w": null, "min_h": 250, "w": 615 }]''', } request = self.factory.post(reverse('cropduster-upload'), data) request.user = self.user response = views.upload(request) data = json.loads(response.content) uploaded_img_path = get_media_path(data['url']) self.assertEqual(response.status_code, 200) self.assertTrue(os.path.exists(uploaded_img_path))