1 | ## $Id: fileviewlets.py 14871 2017-10-13 08:48:46Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | |
---|
19 | import grok |
---|
20 | from zope.component import getUtility |
---|
21 | from waeup.kofa.interfaces import IExtFileStore |
---|
22 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
23 | from waeup.kofa.browser.layout import UtilityView |
---|
24 | from waeup.kofa.browser.fileviewlets import ( |
---|
25 | FileDisplay, FileUpload, Image) |
---|
26 | from waeup.kofa.utils.helpers import file_size |
---|
27 | |
---|
28 | from waeup.kofa.documents.document import PDFDocument |
---|
29 | from waeup.kofa.documents.browser import ( |
---|
30 | DocumentManageFormPage, DocumentDisplayFormPage) |
---|
31 | |
---|
32 | from waeup.kofa.documents.workflow import PUBLISHED |
---|
33 | |
---|
34 | |
---|
35 | # File viewlets for documents |
---|
36 | |
---|
37 | class PDFScanManageUpload(FileUpload): |
---|
38 | """Scan upload viewlet for officers. |
---|
39 | """ |
---|
40 | grok.view(DocumentManageFormPage) |
---|
41 | grok.context(PDFDocument) |
---|
42 | grok.require('waeup.manageDocuments') |
---|
43 | label = _(u'PDF File') |
---|
44 | title = _(u'PDF File') |
---|
45 | mus = 1.5 * 1024*1024 |
---|
46 | download_name = u'file.pdf' |
---|
47 | tab_redirect = '#tab2' |
---|
48 | |
---|
49 | @property |
---|
50 | def download_filename(self): |
---|
51 | file = getUtility(IExtFileStore).getFileByContext( |
---|
52 | self.context, attr=self.download_name) |
---|
53 | fs = file_size(file)/1024.0 |
---|
54 | return u"%s.pdf (%.1f kB)" % (self.context.document_id, fs) |
---|
55 | |
---|
56 | |
---|
57 | class PDFScanDisplay(FileDisplay): |
---|
58 | """Scan display viewlet. |
---|
59 | """ |
---|
60 | grok.order(1) |
---|
61 | grok.context(PDFDocument) |
---|
62 | grok.require('waeup.viewDocuments') |
---|
63 | grok.view(DocumentDisplayFormPage) |
---|
64 | label = _(u'PDF Scan') |
---|
65 | title = _(u'PDF Scan') |
---|
66 | download_name = u'file.pdf' |
---|
67 | |
---|
68 | @property |
---|
69 | def download_filename(self): |
---|
70 | file = getUtility(IExtFileStore).getFileByContext( |
---|
71 | self.context, attr=self.download_name) |
---|
72 | fs = file_size(file)/1024.0 |
---|
73 | return u"%s.pdf (%.1f kB)" % (self.context.document_id, fs) |
---|
74 | |
---|
75 | |
---|
76 | class PDFScanImage(UtilityView, Image): |
---|
77 | """Scan document. |
---|
78 | """ |
---|
79 | grok.name('file.pdf') |
---|
80 | grok.context(PDFDocument) |
---|
81 | grok.require('waeup.Public') |
---|
82 | download_name = u'file.pdf' |
---|
83 | |
---|
84 | @property |
---|
85 | def download_filename(self): |
---|
86 | return self.context.document_id |
---|
87 | |
---|
88 | def update(self): |
---|
89 | if self.context.state != PUBLISHED: |
---|
90 | self.flash(_('The document requested has not yet been published.'), |
---|
91 | type="warning") |
---|
92 | self.redirect(self.application_url()) |
---|
93 | return |
---|