subject.curriculum.new in select list
[redakcja.git] / apps / cover / models.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
6 from django.core.files.base import ContentFile
7 from django.db import models
8 from django.db.models.signals import post_save
9 from django.dispatch import receiver
10 from django.utils.translation import ugettext_lazy as _
11 from django.contrib.sites.models import Site
12 from cover.utils import URLOpener
13
14
15 class Image(models.Model):
16     title = models.CharField(max_length=255, verbose_name=_('title'))
17     author = models.CharField(max_length=255, verbose_name=_('author'))
18     license_name = models.CharField(max_length=255, verbose_name=_('license name'))
19     license_url = models.URLField(max_length=255, blank=True, verbose_name=_('license URL'))
20     source_url = models.URLField(verbose_name=_('source URL'), null=True)
21     download_url = models.URLField(unique=True, verbose_name=_('image download URL'), null=True)
22     file = models.ImageField(upload_to='cover/image', editable=True, verbose_name=_('file'))
23
24     class Meta:
25         verbose_name = _('cover image')
26         verbose_name_plural = _('cover images')
27
28     def __unicode__(self):
29         return u"%s - %s" % (self.author, self.title)
30
31     @models.permalink
32     def get_absolute_url(self):
33         return 'cover_image', [self.id]
34
35     def get_full_url(self):
36         return "http://%s%s" % (Site.objects.get_current().domain, self.get_absolute_url())
37
38
39 @receiver(post_save, sender=Image)
40 def download_image(sender, instance, **kwargs):
41     if instance.pk and not instance.file:
42         t = URLOpener().open(instance.download_url).read()
43         instance.file.save("%d.jpg" % instance.pk, ContentFile(t))