[12004] | 1 | ## $Id: document.py 12200 2014-12-12 15:34:50Z henrik $ |
---|
[11982] | 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 | These are the document tickets. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
[11990] | 22 | from time import time |
---|
[11982] | 23 | from grok import index |
---|
| 24 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
| 25 | from zope.event import notify |
---|
| 26 | from zope.component import getUtility |
---|
| 27 | from zope.component.interfaces import IFactory |
---|
| 28 | from zope.interface import implementedBy |
---|
| 29 | from zope.i18n import translate |
---|
[11983] | 30 | from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory |
---|
[11982] | 31 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 32 | from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal |
---|
[12032] | 33 | from waeup.ikoba.documents.interfaces import ( |
---|
[12200] | 34 | IDocument, IDocument, IDocumentsUtils, |
---|
| 35 | IPDFDocument, IHTMLDocument) |
---|
[12005] | 36 | from waeup.ikoba.documents.utils import generate_document_id |
---|
[11982] | 37 | |
---|
[12063] | 38 | class Document(grok.Container): |
---|
[11982] | 39 | """This is a document. |
---|
| 40 | """ |
---|
| 41 | grok.implements(IDocument) |
---|
| 42 | grok.provides(IDocument) |
---|
| 43 | grok.baseclass() |
---|
| 44 | |
---|
[12126] | 45 | user_id = None |
---|
| 46 | |
---|
[11982] | 47 | def __init__(self): |
---|
| 48 | super(Document, self).__init__() |
---|
[12005] | 49 | # The site doesn't exist in unit tests |
---|
| 50 | try: |
---|
| 51 | self.document_id = generate_document_id() |
---|
| 52 | except AttributeError: |
---|
| 53 | self.document_id = u'd123' |
---|
[11982] | 54 | return |
---|
| 55 | |
---|
[11983] | 56 | @property |
---|
| 57 | def history(self): |
---|
| 58 | history = IObjectHistory(self) |
---|
| 59 | return history |
---|
| 60 | |
---|
[12017] | 61 | @property |
---|
| 62 | def state(self): |
---|
| 63 | state = IWorkflowState(self).getState() |
---|
| 64 | return state |
---|
| 65 | |
---|
[12032] | 66 | @property |
---|
| 67 | def translated_state(self): |
---|
| 68 | try: |
---|
[12087] | 69 | TRANSLATED_STATES = getUtility( |
---|
| 70 | IDocumentsUtils).TRANSLATED_DOCUMENT_STATES |
---|
[12053] | 71 | return TRANSLATED_STATES[self.state] |
---|
[12032] | 72 | except KeyError: |
---|
| 73 | return |
---|
| 74 | |
---|
[12053] | 75 | @property |
---|
[12056] | 76 | def class_name(self): |
---|
[12053] | 77 | return self.__class__.__name__ |
---|
| 78 | |
---|
| 79 | @property |
---|
| 80 | def formatted_transition_date(self): |
---|
| 81 | try: |
---|
| 82 | return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S') |
---|
| 83 | except AttributeError: |
---|
| 84 | return |
---|
[12160] | 85 | |
---|
| 86 | @property |
---|
| 87 | def connected_files(self): |
---|
| 88 | return |
---|
| 89 | |
---|
[12168] | 90 | @property |
---|
| 91 | def is_verifiable(self): |
---|
| 92 | return True, None |
---|
| 93 | |
---|
[12161] | 94 | def setMD5(self): |
---|
| 95 | """Determine md5 checksum of all files and store checksums as |
---|
| 96 | document attributes. |
---|
[12160] | 97 | """ |
---|
| 98 | return |
---|
[12053] | 99 | |
---|
[11982] | 100 | Document = attrs_to_fields(Document) |
---|
| 101 | |
---|
[12161] | 102 | |
---|
[12200] | 103 | class PDFDocument(Document): |
---|
| 104 | """This is a document for a single pdf upload file. |
---|
| 105 | """ |
---|
| 106 | grok.implements(IPDFDocument) |
---|
| 107 | grok.provides(IPDFDocument) |
---|
| 108 | |
---|
| 109 | PDFDocument = attrs_to_fields(PDFDocument) |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | class HTMLDocument(Document): |
---|
| 113 | """This is a document to render html-coded text. |
---|
| 114 | """ |
---|
| 115 | grok.implements(IHTMLDocument) |
---|
| 116 | grok.provides(IHTMLDocument) |
---|
| 117 | |
---|
| 118 | HTMLDocument = attrs_to_fields(HTMLDocument) |
---|
| 119 | |
---|
| 120 | |
---|
[11982] | 121 | class DocumentFactory(grok.GlobalUtility): |
---|
| 122 | """A factory for documents. |
---|
| 123 | """ |
---|
| 124 | grok.implements(IFactory) |
---|
| 125 | grok.name(u'waeup.Document') |
---|
| 126 | title = u"Create a new document.", |
---|
| 127 | description = u"This factory instantiates new documents." |
---|
| 128 | |
---|
| 129 | def __call__(self, *args, **kw): |
---|
| 130 | return Document(*args, **kw) |
---|
| 131 | |
---|
| 132 | def getInterfaces(self): |
---|
| 133 | return implementedBy(Document) |
---|
| 134 | |
---|
[12200] | 135 | |
---|
| 136 | class PDFDocumentFactory(grok.GlobalUtility): |
---|
| 137 | """A factory for documents. |
---|
| 138 | """ |
---|
| 139 | grok.implements(IFactory) |
---|
| 140 | grok.name(u'waeup.PDFDocument') |
---|
| 141 | title = u"Create a new PDF document.", |
---|
| 142 | description = u"This factory instantiates new PDF documents." |
---|
| 143 | |
---|
| 144 | def __call__(self, *args, **kw): |
---|
| 145 | return PDFDocument(*args, **kw) |
---|
| 146 | |
---|
| 147 | def getInterfaces(self): |
---|
| 148 | return implementedBy(PDFDocument) |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | class HTMLDocumentFactory(grok.GlobalUtility): |
---|
| 152 | """A factory for HTML documents. |
---|
| 153 | """ |
---|
| 154 | grok.implements(IFactory) |
---|
| 155 | grok.name(u'waeup.HTMLDocument') |
---|
| 156 | title = u"Create a new HTML document.", |
---|
| 157 | description = u"This factory instantiates new HTML documents." |
---|
| 158 | |
---|
| 159 | def __call__(self, *args, **kw): |
---|
| 160 | return HTMLDocument(*args, **kw) |
---|
| 161 | |
---|
| 162 | def getInterfaces(self): |
---|
| 163 | return implementedBy(HTMLDocument) |
---|
| 164 | |
---|
| 165 | |
---|
[11982] | 166 | @grok.subscribe(IDocument, grok.IObjectAddedEvent) |
---|
| 167 | def handle_document_added(document, event): |
---|
| 168 | """If a document is added the transition create is fired. |
---|
| 169 | The latter produces a logging message. |
---|
| 170 | """ |
---|
| 171 | if IWorkflowState(document).getState() is None: |
---|
| 172 | IWorkflowInfo(document).fireTransition('create') |
---|
| 173 | return |
---|