AcknowledgementTool.py 7.16 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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
    
    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.

    This tools take into account that for some kind of document, 
    acknowledgements are not created in advance. For Site Message, 
    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.
  
    Use Case: who read the emails I sent ?
    Use Case: who said OK to Site Message ?
  """
  id = 'portal_acknowledgements'
  meta_type = 'ERP5 Acknowledgement Tool'
  portal_type = 'Acknowledgement Tool'   
  allowed_types = ('ERP5 Acknowledgement',)
  # Declarative Security
  security = ClassSecurityInfo()


  security.declarePublic('getUnreadAcknowledgementList')
  def countUnread(self, *args, **kw):
    """
      counts number of acknowledgements pending
    """
    return len(self.getUnreadAcknowledgementList(*args, **kw))

  security.declarePublic('getUnreadAcknowledgementList')
  def getUnreadAcknowledgementList(self, portal_type=None, user_name=None, 
                                   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:
      url_list = self.getUnreadDocumentUrlList(portal_type=portal_type, 
                                               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):
    """
      returns document that needs to be acknowledged : 
      - 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()
      now = DateTime()
      # First look at all event that define the current user as destination
      all_document_list = [x for x in \
         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_reference=user_name)]
      # 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))
      all_document_list.extend([x for x in \
         self.portal_catalog(portal_type = portal_type,
              query=query,
  #           start_date = {'query':now,'range':'max'},
  #           stop_date = {'query':now,'range':'min'},
              destination_reference=user_name)])
      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())
    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
      
      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)
 

InitializeClass(AcknowledgementTool)