Librarian in regular requirements.
[redakcja.git] / apps / catalogue / models / image.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.conf import settings
7 from django.contrib.sites.models import Site
8 from django.db import models
9 from django.template.loader import render_to_string
10 from django.utils.translation import ugettext_lazy as _
11 from catalogue.helpers import cached_in_field
12 from catalogue.models import Project
13 from catalogue.tasks import refresh_instance
14 from dvcs import models as dvcs_models
15
16
17 class Image(dvcs_models.Document):
18     """ An editable chunk of text. Every Book text is divided into chunks. """
19     REPO_PATH = settings.CATALOGUE_IMAGE_REPO_PATH
20
21     image = models.FileField(_('image'), upload_to='catalogue/images')
22     title = models.CharField(_('title'), max_length=255, blank=True)
23     slug = models.SlugField(_('slug'), unique=True)
24     public = models.BooleanField(_('public'), default=True, db_index=True)
25     project = models.ForeignKey(Project, null=True, blank=True)
26
27     # cache
28     _short_html = models.TextField(null=True, blank=True, editable=False)
29     _new_publishable = models.NullBooleanField(editable=False)
30     _published = models.NullBooleanField(editable=False)
31     _changed = models.NullBooleanField(editable=False)
32
33     class Meta:
34         app_label = 'catalogue'
35         ordering = ['title']
36         verbose_name = _('image')
37         verbose_name_plural = _('images')
38         permissions = [('can_pubmark_image', 'Can mark images for publishing')]
39
40     # Representing
41     # ============
42
43     def __unicode__(self):
44         return self.title
45
46     @models.permalink
47     def get_absolute_url(self):
48         return ("catalogue_image", [self.slug])
49
50     def correct_about(self):
51         return ["http://%s%s" % (
52             Site.objects.get_current().domain,
53             self.get_absolute_url()
54             ),
55             "http://%s%s" % (
56                 'obrazy.redakcja.wolnelektury.pl',
57                 self.get_absolute_url()
58             )]
59
60     # State & cache
61     # =============
62
63     def last_published(self):
64         try:
65             return self.publish_log.all()[0].timestamp
66         except IndexError:
67             return None
68
69     def assert_publishable(self):
70         from librarian.picture import WLPicture
71         from librarian import NoDublinCore, ParseError, ValidationError
72
73         class SelfImageStore(object):
74             def path(self_, slug, mime_type):
75                 """Returns own file object. Ignores slug ad mime_type."""
76                 return open(self.image.path)
77
78         publishable = self.publishable()
79         assert publishable, _("There is no publishable revision")
80         picture_xml = publishable.materialize()
81
82         try:
83             picture = WLPicture.from_bytes(
84                     picture_xml.encode('utf-8'),
85                     image_store=SelfImageStore)
86         except ParseError, e:
87             raise AssertionError(_('Invalid XML') + ': ' + str(e))
88         except NoDublinCore:
89             raise AssertionError(_('No Dublin Core found.'))
90         except ValidationError, e:
91             raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
92
93         valid_about = self.correct_about()
94         assert picture.picture_info.about in valid_about, \
95                 _("rdf:about is not") + " " + valid_about[0]
96
97     def publishable_error(self):
98         try:
99             return self.assert_publishable()
100         except AssertionError, e:
101             return e
102         else:
103             return None
104
105     def accessible(self, request):
106         return self.public or request.user.is_authenticated()
107
108     def is_new_publishable(self):
109         change = self.publishable()
110         if not change:
111             return False
112         return not change.publish_log.exists()
113     new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
114
115     def is_published(self):
116         return self.publish_log.exists()
117     published = cached_in_field('_published')(is_published)
118
119     def is_changed(self):
120         if self.head is None:
121             return False
122         return not self.head.publishable
123     changed = cached_in_field('_changed')(is_changed)
124
125     @cached_in_field('_short_html')
126     def short_html(self):
127         return render_to_string(
128                     'catalogue/image_short.html', {'image': self})
129
130     def refresh(self):
131         """This should be done offline."""
132         self.short_html
133         self.single
134         self.new_publishable
135         self.published
136
137     def touch(self):
138         update = {
139             "_changed": self.is_changed(),
140             "_short_html": None,
141             "_new_publishable": self.is_new_publishable(),
142             "_published": self.is_published(),
143         }
144         Image.objects.filter(pk=self.pk).update(**update)
145         refresh_instance(self)
146
147     def refresh(self):
148         """This should be done offline."""
149         self.changed
150         self.short_html
151
152
153     # Publishing
154     # ==========
155
156     def publish(self, user):
157         """Publishes the picture on behalf of a (local) user."""
158         from base64 import b64encode
159         import apiclient
160         from catalogue.signals import post_publish
161
162         self.assert_publishable()
163         change = self.publishable()
164         picture_xml = change.materialize()
165         picture_data = open(self.image.path).read()
166         apiclient.api_call(user, "pictures/", {
167                 "picture_xml": picture_xml,
168                 "picture_image_data": b64encode(picture_data),
169             })
170         # record the publish
171         log = self.publish_log.create(user=user, change=change)
172         post_publish.send(sender=log)