User list sorted by last activity.
[redakcja.git] / src / documents / 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.urls import reverse
9 from django.utils.translation import ugettext_lazy as _
10 from documents.helpers import cached_in_field
11 from documents.models import Project
12 from dvcs import models as dvcs_models
13
14
15 class Image(dvcs_models.Document):
16     """ An editable chunk of text. Every Book text is divided into chunks. """
17     REPO_PATH = settings.CATALOGUE_IMAGE_REPO_PATH
18
19     image = models.FileField(_('image'), upload_to='catalogue/images')
20     title = models.CharField(_('title'), max_length=255, blank=True)
21     slug = models.SlugField(_('slug'), unique=True)
22     public = models.BooleanField(_('public'), default=True, db_index=True)
23     project = models.ForeignKey(Project, models.SET_NULL, null=True, blank=True)
24
25     # cache
26     _new_publishable = models.BooleanField(editable=False, null=True)
27     _published = models.BooleanField(editable=False, null=True)
28     _changed = models.BooleanField(editable=False, null=True)
29
30     class Meta:
31         app_label = 'documents'
32         ordering = ['title']
33         verbose_name = _('image')
34         verbose_name_plural = _('images')
35         permissions = [('can_pubmark_image', 'Can mark images for publishing')]
36
37     # Representing
38     # ============
39
40     def __str__(self):
41         return self.title
42
43     def get_absolute_url(self):
44         return reverse("documents_image", args=[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 documents.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)