Created
March 1, 2016 11:48
-
-
Save acquayefrank/52c3ba9192117736e00f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image(models.Model): | |
original = models.ImageField( | |
upload_to='products', | |
max_length=250, | |
blank=True, | |
null=True | |
) | |
thumbnail = ImageSpecField( | |
source='original', | |
processors=[ResizeToFill(100, 50)], | |
format='JPEG', | |
options={'quality': 60} | |
) | |
image_url = models.URLField(blank=True, null=True, unique=True, max_length=250) | |
product = models.ForeignKey(Product, related_name='images') | |
created_at = models.DateTimeField( | |
auto_now_add=True, | |
blank=True, null=True) | |
updated_at = models.DateTimeField( | |
auto_now=True, | |
blank=True, null=True) | |
def get_remote_image(self): | |
if self.image_url: | |
filename = urlparse(self.image_url).path.split('/')[-1] | |
full_path = os.path.abspath('storeuno') | |
try: | |
result = urlretrieve(self.image_url, os.path.join(full_path + '/media/products', filename)) | |
self.original = '/media/products/' + result[0].rsplit('/', 1)[-1] | |
self.save() | |
except urllib.error.HTTPError: | |
pass | |
def __str__(self): | |
return self.image_url[:10] | |
class Meta: | |
verbose_name = u'Image' | |
verbose_name_plural = u'Images' | |
ordering = ["updated_at"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Call the get_remote_image on the object after creation