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

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

Implement document_id generator. Ensure that document_ids remain unique during import.

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1## $Id: document.py 12005 2014-11-20 05:40:52Z 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.utils.logger import Logger
34from waeup.ikoba.documents.interfaces import IDocument, IDocument
35from waeup.ikoba.documents.utils import generate_document_id
36
37class Document(grok.Container, Logger):
38    """This is a document.
39    """
40    grok.implements(IDocument)
41    grok.provides(IDocument)
42    grok.baseclass()
43
44    logger_name = 'waeup.ikoba.${sitename}.documents'
45    logger_filename = 'documents.log'
46    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
47
48    def logger_info(self, comment=None):
49        """Get the logger's info method.
50        """
51        self.logger.info('%s' % comment)
52        return
53
54    #def __init__(self):
55    #    super(Document, self).__init__()
56    #    timestamp = ("%d" % int(time()*10000))[1:]
57    #    self.document_id = "d%s" % timestamp
58     #   return
59
60    def __init__(self):
61        super(Document, self).__init__()
62        # The site doesn't exist in unit tests
63        try:
64            self.document_id = generate_document_id()
65        except AttributeError:
66            self.document_id = u'd123'
67        return
68
69    @property
70    def history(self):
71        history = IObjectHistory(self)
72        return history
73
74Document = attrs_to_fields(Document)
75
76class DocumentFactory(grok.GlobalUtility):
77    """A factory for documents.
78    """
79    grok.implements(IFactory)
80    grok.name(u'waeup.Document')
81    title = u"Create a new document.",
82    description = u"This factory instantiates new documents."
83
84    def __call__(self, *args, **kw):
85        return Document(*args, **kw)
86
87    def getInterfaces(self):
88        return implementedBy(Document)
89
90@grok.subscribe(IDocument, grok.IObjectAddedEvent)
91def handle_document_added(document, event):
92    """If a document is added the transition create is fired.
93    The latter produces a logging message.
94    """
95    if IWorkflowState(document).getState() is None:
96        IWorkflowInfo(document).fireTransition('create')
97    return
Note: See TracBrowser for help on using the repository browser.