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