1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.conf import settings
5 from django.contrib.sites.models import Site
6 from django.db import models
7 from django.template.loader import render_to_string
8 from django.utils.translation import ugettext_lazy as _
9 from catalogue.helpers import cached_in_field
10 from catalogue.models import Project
11 from dvcs import models as dvcs_models
14 class Image(dvcs_models.Document):
15 """ An editable chunk of text. Every Book text is divided into chunks. """
16 REPO_PATH = settings.CATALOGUE_IMAGE_REPO_PATH
18 image = models.FileField(_('image'), upload_to='catalogue/images')
19 title = models.CharField(_('title'), max_length=255, blank=True)
20 slug = models.SlugField(_('slug'), unique=True)
21 public = models.BooleanField(_('public'), default=True, db_index=True)
22 project = models.ForeignKey(Project, null=True, blank=True)
25 _new_publishable = models.NullBooleanField(editable=False)
26 _published = models.NullBooleanField(editable=False)
27 _changed = models.NullBooleanField(editable=False)
30 app_label = 'catalogue'
32 verbose_name = _('image')
33 verbose_name_plural = _('images')
34 permissions = [('can_pubmark_image', 'Can mark images for publishing')]
43 def get_absolute_url(self):
44 return ("catalogue_image", [self.slug])
46 def correct_about(self):
47 return ["http://%s%s" % (
48 Site.objects.get_current().domain,
49 self.get_absolute_url()
52 'obrazy.redakcja.wolnelektury.pl',
53 self.get_absolute_url()
59 def last_published(self):
61 return self.publish_log.all()[0].timestamp
65 def assert_publishable(self):
66 from librarian.picture import WLPicture
67 from librarian import NoDublinCore, ParseError, ValidationError
69 class SelfImageStore(object):
70 def path(self_, slug, mime_type):
71 """Returns own file object. Ignores slug ad mime_type."""
72 return open(self.image.path)
74 publishable = self.publishable()
75 assert publishable, _("There is no publishable revision")
76 picture_xml = publishable.materialize()
79 picture = WLPicture.from_bytes(
80 picture_xml.encode('utf-8'),
81 image_store=SelfImageStore)
82 except ParseError as e:
83 raise AssertionError(_('Invalid XML') + ': ' + str(e))
85 raise AssertionError(_('No Dublin Core found.'))
86 except ValidationError as e:
87 raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
89 valid_about = self.correct_about()
90 assert picture.picture_info.about in valid_about, \
91 _("rdf:about is not") + " " + valid_about[0]
93 def publishable_error(self):
95 return self.assert_publishable()
96 except AssertionError as e:
101 def accessible(self, request):
102 return self.public or request.user.is_authenticated
104 def is_new_publishable(self):
105 change = self.publishable()
108 return not change.publish_log.exists()
109 new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
111 def is_published(self):
112 return self.publish_log.exists()
113 published = cached_in_field('_published')(is_published)
115 def is_changed(self):
116 if self.head is None:
118 return not self.head.publishable
119 changed = cached_in_field('_changed')(is_changed)
123 "_changed": self.is_changed(),
124 "_new_publishable": self.is_new_publishable(),
125 "_published": self.is_published(),
127 Image.objects.filter(pk=self.pk).update(**update)
132 def publish(self, user):
133 """Publishes the picture on behalf of a (local) user."""
134 from base64 import b64encode
136 from catalogue.signals import post_publish
138 self.assert_publishable()
139 change = self.publishable()
140 picture_xml = change.materialize()
141 picture_data = open(self.image.path).read()
142 apiclient.api_call(user, "pictures/", {
143 "picture_xml": picture_xml,
144 "picture_image_data": b64encode(picture_data),
147 log = self.publish_log.create(user=user, change=change)
148 post_publish.send(sender=log)