1 | ## $Id: document.py 12056 2014-11-25 11:44:47Z 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 | These are the document tickets. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from time import time |
---|
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 |
---|
30 | from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory |
---|
31 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
32 | from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal |
---|
33 | from waeup.ikoba.utils.logger import Logger |
---|
34 | from waeup.ikoba.documents.interfaces import ( |
---|
35 | IDocument, IDocument, IDocumentsUtils) |
---|
36 | from waeup.ikoba.documents.utils import generate_document_id |
---|
37 | |
---|
38 | class Document(grok.Container, Logger): |
---|
39 | """This is a document. |
---|
40 | """ |
---|
41 | grok.implements(IDocument) |
---|
42 | grok.provides(IDocument) |
---|
43 | grok.baseclass() |
---|
44 | |
---|
45 | logger_name = 'waeup.ikoba.${sitename}.documents' |
---|
46 | logger_filename = 'documents.log' |
---|
47 | logger_format_str = '"%(asctime)s","%(user)s",%(message)s' |
---|
48 | |
---|
49 | def logger_info(self, comment=None): |
---|
50 | """Get the logger's info method. |
---|
51 | """ |
---|
52 | self.logger.info('%s' % comment) |
---|
53 | return |
---|
54 | |
---|
55 | def __init__(self): |
---|
56 | super(Document, self).__init__() |
---|
57 | # The site doesn't exist in unit tests |
---|
58 | try: |
---|
59 | self.document_id = generate_document_id() |
---|
60 | except AttributeError: |
---|
61 | self.document_id = u'd123' |
---|
62 | return |
---|
63 | |
---|
64 | @property |
---|
65 | def history(self): |
---|
66 | history = IObjectHistory(self) |
---|
67 | return history |
---|
68 | |
---|
69 | @property |
---|
70 | def state(self): |
---|
71 | state = IWorkflowState(self).getState() |
---|
72 | return state |
---|
73 | |
---|
74 | @property |
---|
75 | def translated_state(self): |
---|
76 | try: |
---|
77 | TRANSLATED_STATES = getUtility(IDocumentsUtils).TRANSLATED_STATES |
---|
78 | return TRANSLATED_STATES[self.state] |
---|
79 | except KeyError: |
---|
80 | return |
---|
81 | |
---|
82 | @property |
---|
83 | def class_name(self): |
---|
84 | return self.__class__.__name__ |
---|
85 | |
---|
86 | @property |
---|
87 | def formatted_transition_date(self): |
---|
88 | try: |
---|
89 | return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S') |
---|
90 | except AttributeError: |
---|
91 | return |
---|
92 | |
---|
93 | |
---|
94 | Document = attrs_to_fields(Document) |
---|
95 | |
---|
96 | class DocumentFactory(grok.GlobalUtility): |
---|
97 | """A factory for documents. |
---|
98 | """ |
---|
99 | grok.implements(IFactory) |
---|
100 | grok.name(u'waeup.Document') |
---|
101 | title = u"Create a new document.", |
---|
102 | description = u"This factory instantiates new documents." |
---|
103 | |
---|
104 | def __call__(self, *args, **kw): |
---|
105 | return Document(*args, **kw) |
---|
106 | |
---|
107 | def getInterfaces(self): |
---|
108 | return implementedBy(Document) |
---|
109 | |
---|
110 | @grok.subscribe(IDocument, grok.IObjectAddedEvent) |
---|
111 | def handle_document_added(document, event): |
---|
112 | """If a document is added the transition create is fired. |
---|
113 | The latter produces a logging message. |
---|
114 | """ |
---|
115 | if IWorkflowState(document).getState() is None: |
---|
116 | IWorkflowInfo(document).fireTransition('create') |
---|
117 | return |
---|