dbe1c0475f1b45e482b0b0f819c799645724b323
[redakcja.git] / src / catalogue / models / image.py
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.
3 #
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
12
13
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
17
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)
23
24     # cache
25     _new_publishable = models.NullBooleanField(editable=False)
26     _published = models.NullBooleanField(editable=False)
27     _changed = models.NullBooleanField(editable=False)
28
29     class Meta:
30         app_label = 'catalogue'
31         ordering = ['title']
32         verbose_name = _('image')
33         verbose_name_plural = _('images')
34         permissions = [('can_pubmark_image', 'Can mark images for publishing')]
35
36     # Representing
37     # ============
38
39     def __str__(self):
40         return self.title
41
42     @models.permalink
43     def get_absolute_url(self):
44         return ("catalogue_image", [self.slug])
45
46     def correct_about(self):
47         return ["http://%s%s" % (
48             Site.objects.get_current().domain,
49             self.get_absolute_url()
50             ),
51             "http://%s%s" % (
52                 'obrazy.redakcja.wolnelektury.pl',
53                 self.get_absolute_url()
54             )]
55
56     # State & cache
57     # =============
58
59     def last_published(self):
60         try:
61             return self.publish_log.all()[0].timestamp
62         except IndexError:
63             return None
64
65     def assert_publishable(self):
66         from librarian.picture import WLPicture
67         from librarian import NoDublinCore, ParseError, ValidationError
68
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)
73
74         publishable = self.publishable()
75         assert publishable, _("There is no publishable revision")
76         picture_xml = publishable.materialize()
77
78         try:
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))
84         except NoDublinCore:
85             raise AssertionError(_('No Dublin Core found.'))
86         except ValidationError as e:
87             raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
88
89         valid_about = self.correct_about()
90         assert picture.picture_info.about in valid_about, \
91                 _("rdf:about is not") + " " + valid_about[0]
92
93     def publishable_error(self):
94         try:
95             return self.assert_publishable()
96         except AssertionError as e:
97             return e
98         else:
99             return None
100
101     def accessible(self, request):
102         return self.public or request.user.is_authenticated
103
104     def is_new_publishable(self):
105         change = self.publishable()
106         if not change:
107             return False
108         return not change.publish_log.exists()
109     new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
110
111     def is_published(self):
112         return self.publish_log.exists()
113     published = cached_in_field('_published')(is_published)
114
115     def is_changed(self):
116         if self.head is None:
117             return False
118         return not self.head.publishable
119     changed = cached_in_field('_changed')(is_changed)
120
121     def touch(self):
122         update = {
123             "_changed": self.is_changed(),
124             "_new_publishable": self.is_new_publishable(),
125             "_published": self.is_published(),
126         }
127         Image.objects.filter(pk=self.pk).update(**update)
128
129     # Publishing
130     # ==========
131
132     def publish(self, user):
133         """Publishes the picture on behalf of a (local) user."""
134         from base64 import b64encode
135         import apiclient
136         from catalogue.signals import post_publish
137
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),
145             })
146         # record the publish
147         log = self.publish_log.create(user=user, change=change)
148         post_publish.send(sender=log)