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

Last change on this file since 12201 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
RevLine 
[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"""
19These are the document tickets.
20"""
21import grok
[11990]22from time import time
[11982]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
[11983]30from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory
[11982]31from waeup.ikoba.interfaces import MessageFactory as _
32from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal
[12032]33from waeup.ikoba.documents.interfaces import (
[12200]34    IDocument, IDocument, IDocumentsUtils,
35    IPDFDocument, IHTMLDocument)
[12005]36from waeup.ikoba.documents.utils import generate_document_id
[11982]37
[12063]38class 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]100Document = attrs_to_fields(Document)
101
[12161]102
[12200]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
[11982]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
[12200]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
[11982]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.