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