source: main/waeup.ikoba/trunk/src/waeup/ikoba/documents/document.py @ 12200

Last change on this file since 12200 was 12200, checked in by Henrik Bettermann, 10 years ago

We need documents which can be accessed or downloaded from product pages. These documents can provide general information of registration processes or pdf forms to be filled offline and later uploaded by customers.

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1## $Id: document.py 12200 2014-12-12 15:34:50Z 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"""
19These are the document tickets.
20"""
21import grok
22from time import time
23from grok import index
24from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
25from zope.event import notify
26from zope.component import getUtility
27from zope.component.interfaces import IFactory
28from zope.interface import implementedBy
29from zope.i18n import translate
30from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory
31from waeup.ikoba.interfaces import MessageFactory as _
32from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal
33from waeup.ikoba.documents.interfaces import (
34    IDocument, IDocument, IDocumentsUtils,
35    IPDFDocument, IHTMLDocument)
36from waeup.ikoba.documents.utils import generate_document_id
37
38class Document(grok.Container):
39    """This is a document.
40    """
41    grok.implements(IDocument)
42    grok.provides(IDocument)
43    grok.baseclass()
44
45    user_id = None
46
47    def __init__(self):
48        super(Document, self).__init__()
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'
54        return
55
56    @property
57    def history(self):
58        history = IObjectHistory(self)
59        return history
60
61    @property
62    def state(self):
63        state = IWorkflowState(self).getState()
64        return state
65
66    @property
67    def translated_state(self):
68        try:
69            TRANSLATED_STATES = getUtility(
70                IDocumentsUtils).TRANSLATED_DOCUMENT_STATES
71            return TRANSLATED_STATES[self.state]
72        except KeyError:
73            return
74
75    @property
76    def class_name(self):
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
85
86    @property
87    def connected_files(self):
88        return
89
90    @property
91    def is_verifiable(self):
92        return True, None
93
94    def setMD5(self):
95        """Determine md5 checksum of all files and store checksums as
96        document attributes.
97        """
98        return
99       
100Document = attrs_to_fields(Document)
101
102
103class PDFDocument(Document):
104    """This is a  document for a single pdf upload file.
105    """
106    grok.implements(IPDFDocument)
107    grok.provides(IPDFDocument)
108
109PDFDocument = attrs_to_fields(PDFDocument)
110
111
112class HTMLDocument(Document):
113    """This is a  document to render html-coded text.
114    """
115    grok.implements(IHTMLDocument)
116    grok.provides(IHTMLDocument)
117
118HTMLDocument = attrs_to_fields(HTMLDocument)
119
120
121class 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
135
136class 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
151class 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
166@grok.subscribe(IDocument, grok.IObjectAddedEvent)
167def 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
Note: See TracBrowser for help on using the repository browser.