AcknowledgementTool.py 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
##############################################################################
#
# Copyright (c) 2009 Nexedi SARL and Contributors. All Rights Reserved.
#                    Ben Mayhew <maybewhen@gmx.net>
#                    Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##############################################################################
from AccessControl import ClassSecurityInfo
30
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
31 32 33 34 35 36 37 38 39 40 41 42
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions
from Products.ERP5 import _dtmldir
from Products.ERP5.Document.Acknowledgement import Acknowledgement
from zLOG import LOG
from DateTime import DateTime
from Products.ZSQLCatalog.SQLCatalog import Query, NegatedQuery


class AcknowledgementTool(BaseTool):
  """
    Provide an entry point to track reception of events
43

44 45 46 47
    someone who can not view the ticket or the event
    must be able to acknowledge reception of email
    or of site message sent by CRM.

48 49
    This tools take into account that for some kind of document,
    acknowledgements are not created in advance. For Site Message,
50 51 52 53
    acknowledgements will be created every time the user confirm that he has
    read the information.

    In the case of internal emails, acknowledgements are created in advance.
54

55 56 57 58 59
    Use Case: who read the emails I sent ?
    Use Case: who said OK to Site Message ?
  """
  id = 'portal_acknowledgements'
  meta_type = 'ERP5 Acknowledgement Tool'
60
  portal_type = 'Acknowledgement Tool'
61 62 63 64 65
  allowed_types = ('ERP5 Acknowledgement',)
  # Declarative Security
  security = ClassSecurityInfo()


66
  security.declarePublic('countUnread')
67 68 69 70 71 72
  def countUnread(self, *args, **kw):
    """
      counts number of acknowledgements pending
    """
    return len(self.getUnreadAcknowledgementList(*args, **kw))

73 74 75 76 77 78 79
  def _getAcknowledgementTypeList(self):
    """
    Return list of acknowledgement types, should use portal types group
    when we see need of having several portal types for acknowledgements
    """
    return ('Acknowledgement',)

80
  security.declarePublic('getUnreadAcknowledgementList')
81
  def getUnreadAcknowledgementList(self, portal_type=None, user_name=None,
82 83 84 85 86 87 88 89 90 91
                                   url_list=None):
    """
      returns acknowledgements pending
      in the form of
      - TempAcknowledgement (for Site Message)
      - Acknowledgement (internal email)
    """
    portal = self.getPortalObject()
    return_list = []
    if url_list is None:
92
      url_list = self.getUnreadDocumentUrlList(portal_type=portal_type,
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                                               user_name=user_name)
    for url in url_list:
      document = portal.restrictedTraverse(url)
      if not document.isAcknowledged(user_name=user_name):
        # If the document to acknowledge is a ticket, we should return
        # a temp acknowledgement
        if document.getPortalType() in portal.getPortalEventTypeList():
          module = portal.getDefaultModule('Acknowledgement')
          temp_acknowledgement = module.newContent(
                                       portal_type='Acknowledgement',
                                       temp_object=1,
                                       document_proxy=document.getRelativeUrl(),
                                       causality=document.getRelativeUrl())
          return_list.append(temp_acknowledgement)
        else:
          # If not an event, this means that we have directly the document
          # that we must acknowledge
          return_list.append(document)
    return return_list

  security.declarePublic('getUnreadDocumentUrlList')
  def getUnreadDocumentUrlList(self, portal_type=None, user_name=None, **kw):
    """
116
      returns document that needs to be acknowledged :
117 118 119 120 121 122 123 124 125
      - Acknowledgement (internal email)
      - Site Message

      This method will mainly be used by getUnreadAcknowledgementList. Also,
      because url are used, the result will be easy to cache.
    """
    document_list = []
    if user_name is not None:
      portal = self.getPortalObject()
Romain Courteaud's avatar
Romain Courteaud committed
126 127
      person_value = portal.ERP5Site_getAuthenticatedMemberPersonValue(
                                                      user_name=user_name)
128
      if person_value is not None:
Romain Courteaud's avatar
Romain Courteaud committed
129 130
        now = DateTime()
        # First look at all event that define the current user as destination
131
        all_document_list = [x.getObject() for x in \
Romain Courteaud's avatar
Romain Courteaud committed
132 133 134 135 136 137 138 139 140
           self.portal_catalog(portal_type = portal_type,
                simulation_state = self.getPortalTransitInventoryStateList(),
#               start_date = {'query':now,'range':'max'},
#               stop_date = {'query':now,'range':'min'},
                default_destination_uid=person_value.getUid())]
        # Now we can look directly at acknowledgement document not approved yet
        # so not in a final state
        final_state_list = self.getPortalCurrentInventoryStateList()
        query = NegatedQuery(Query(simulation_state=final_state_list))
141
        for x in self.portal_catalog(portal_type = self._getAcknowledgementTypeList(),
Romain Courteaud's avatar
Romain Courteaud committed
142 143 144
                query=query,
#               start_date = {'query':now,'range':'max'},
#               stop_date = {'query':now,'range':'min'},
145 146 147 148
                default_destination_uid=person_value.getUid()):
          x = x.getObject()
          if x not in all_document_list:
            all_document_list.append(x)
Romain Courteaud's avatar
Romain Courteaud committed
149 150 151 152 153 154 155
        for document in all_document_list:
          # We filter manually on dates until a good solution is found for
          # searching by dates on the catalog
          if (document.getStartDate() < now < (document.getStopDate()+1)):
            acknowledged = document.isAcknowledged(user_name=user_name)
            if not acknowledged:
              document_list.append(document.getRelativeUrl())
156 157 158 159 160 161 162 163 164 165 166 167
    else:
      raise ValueError('No user name given')
    return document_list

  security.declareProtected(Permissions.AccessContentsInformation,
                            'acknowledge')
  def acknowledge(self, uid=None, path=None, user_name=None, **kw):
    """
      Create an acknowledgement document for :
      - a ticket
      - an event
      - an acknowledgement
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182
      This methods needs to check if there is already ongoing ackowledgement
      for the document of for this user. We will have to use activities with
      tag and probably a serialization.
    """
    document = None
    if uid is not None:
      document = self.portal_catalog.getObject(uid)
    elif path is not None:
      document = self.restrictedTraverse(path)
    else:
      raise ValueError("No path or uid given")
    if document is None:
      raise ValueError("Ticket does not exist or you don't have access to it")
    return document.acknowledge(user_name=user_name, **kw)
183

184 185

InitializeClass(AcknowledgementTool)