[12004] | 1 | ## $Id: document.py 12087 2014-11-28 16:17:38Z 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 ( |
---|
| 34 | IDocument, IDocument, IDocumentsUtils) |
---|
[12005] | 35 | from waeup.ikoba.documents.utils import generate_document_id |
---|
[11982] | 36 | |
---|
[12063] | 37 | class Document(grok.Container): |
---|
[11982] | 38 | """This is a document. |
---|
| 39 | """ |
---|
| 40 | grok.implements(IDocument) |
---|
| 41 | grok.provides(IDocument) |
---|
| 42 | grok.baseclass() |
---|
| 43 | |
---|
| 44 | def __init__(self): |
---|
| 45 | super(Document, self).__init__() |
---|
[12005] | 46 | # The site doesn't exist in unit tests |
---|
| 47 | try: |
---|
| 48 | self.document_id = generate_document_id() |
---|
| 49 | except AttributeError: |
---|
| 50 | self.document_id = u'd123' |
---|
[11982] | 51 | return |
---|
| 52 | |
---|
[11983] | 53 | @property |
---|
| 54 | def history(self): |
---|
| 55 | history = IObjectHistory(self) |
---|
| 56 | return history |
---|
| 57 | |
---|
[12017] | 58 | @property |
---|
| 59 | def state(self): |
---|
| 60 | state = IWorkflowState(self).getState() |
---|
| 61 | return state |
---|
| 62 | |
---|
[12032] | 63 | @property |
---|
| 64 | def translated_state(self): |
---|
| 65 | try: |
---|
[12087] | 66 | TRANSLATED_STATES = getUtility( |
---|
| 67 | IDocumentsUtils).TRANSLATED_DOCUMENT_STATES |
---|
[12053] | 68 | return TRANSLATED_STATES[self.state] |
---|
[12032] | 69 | except KeyError: |
---|
| 70 | return |
---|
| 71 | |
---|
[12053] | 72 | @property |
---|
[12056] | 73 | def class_name(self): |
---|
[12053] | 74 | return self.__class__.__name__ |
---|
| 75 | |
---|
| 76 | @property |
---|
| 77 | def formatted_transition_date(self): |
---|
| 78 | try: |
---|
| 79 | return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S') |
---|
| 80 | except AttributeError: |
---|
| 81 | return |
---|
| 82 | |
---|
| 83 | |
---|
[11982] | 84 | Document = attrs_to_fields(Document) |
---|
| 85 | |
---|
| 86 | class DocumentFactory(grok.GlobalUtility): |
---|
| 87 | """A factory for documents. |
---|
| 88 | """ |
---|
| 89 | grok.implements(IFactory) |
---|
| 90 | grok.name(u'waeup.Document') |
---|
| 91 | title = u"Create a new document.", |
---|
| 92 | description = u"This factory instantiates new documents." |
---|
| 93 | |
---|
| 94 | def __call__(self, *args, **kw): |
---|
| 95 | return Document(*args, **kw) |
---|
| 96 | |
---|
| 97 | def getInterfaces(self): |
---|
| 98 | return implementedBy(Document) |
---|
| 99 | |
---|
| 100 | @grok.subscribe(IDocument, grok.IObjectAddedEvent) |
---|
| 101 | def handle_document_added(document, event): |
---|
| 102 | """If a document is added the transition create is fired. |
---|
| 103 | The latter produces a logging message. |
---|
| 104 | """ |
---|
| 105 | if IWorkflowState(document).getState() is None: |
---|
| 106 | IWorkflowInfo(document).fireTransition('create') |
---|
| 107 | return |
---|