1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from urllib2 import urlopen
8 from urlparse import urljoin
9 from django.conf import settings
10 from django.core.files.base import ContentFile
11 from django.db import models
12 from django.db.models.signals import post_save
13 from django.dispatch import receiver
14 from django.utils.translation import ugettext_lazy as _
17 class Image(models.Model):
18 title = models.CharField(max_length=255)
19 author = models.CharField(max_length=255)
20 license_name = models.CharField(max_length=255)
21 license_url = models.URLField(max_length=255, blank=True)
22 source_url = models.URLField()
23 download_url = models.URLField(unique=True)
24 file = models.ImageField(upload_to='cover/image', editable=False)
27 verbose_name = _('cover image')
28 verbose_name_plural = _('cover images')
30 def __unicode__(self):
31 return u"%s - %s" % (self.author, self.title)
34 def get_absolute_url(self):
35 return ('cover_image', [self.id])
38 @receiver(post_save, sender=Image)
39 def download_image(sender, instance, **kwargs):
40 if instance.pk and not instance.file:
41 t = urlopen(instance.download_url).read()
42 instance.file.save("%d.jpg" % instance.pk, ContentFile(t))