Selection.py 7.25 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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 30 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
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from Globals import InitializeClass, Persistent, Acquisition
from OFS.SimpleItem import SimpleItem
from OFS.Traversable import Traversable
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions as ERP5Permissions
import string

from zLOG import LOG

class Selection(Acquisition.Implicit, Traversable, Persistent):
    """
        Selection

        A Selection instance allows a ListBox object to browse the data
        resulting from a method call such as an SQL Method Call. Selection
        instances are used to implement persistent selections in ERP5.

        Selection uses the following control variables

        - selection_method      --  a method which will be used
                                    to select objects

        - selection_params      --  a dictionnary of parameters to call the
                                    method with

        - selection_sort_on     --  a dictionnary of parameters to sort
                                    the selection

        - selection_uids        --  a list of object uids which defines the
                                    selection

        - selection_invert_mode --  defines the mode of the selection
                                    if mode is 1, then only show the
                                    ob

        - selection_list_url    --  the URL to go back to list mode

        - selection_checked_uids --  a list of uids checked

        - selection_domain_path --  the path to the root of the selection tree


        - selection_domain_list --  the relative path of the current selected domain

        - selection_report_path --  the report path

        - selection_report_list -- list of open report nodes

    """

    security = ClassSecurityInfo()

    def __init__(self, method_path=None, params={}, sort_on=[],
                 uids=[], invert_mode=0, list_url='',
                 columns=[], checked_uids=[]):
        self.selection_method_path = method_path
        self.selection_params = params
        self.selection_uids = uids
        self.selection_invert_mode = invert_mode
        self.selection_list_url = list_url
        self.selection_columns = columns
        self.selection_sort_on = sort_on
        self.selection_checked_uids = checked_uids
        self.selection_domain_path = ('portal_categories',)
        self.selection_domain_list = ((),)
        self.selection_report_path = ('portal_categories',)
        self.selection_report_list = ((),)

    def edit(self, params=None, **kw):
        if params is not None:
          self.selection_params = {}
          for key in params.keys():
            # We should only keep params which do not start with field_
            # in order to make sure we do not collect unwanted params
            # resulting form the REQUEST generated by an ERP5Form submit
            if key[0:6] != 'field_':
              self.selection_params[key] = params[key]
        if kw is not None:
          for k,v in kw.items():
            if v is not None:
              setattr(self, 'selection_%s' % k, v)

    def __call__(self, selection_method = None, context=None, REQUEST=None):
        if self.selection_invert_mode is 0:
          if selection_method is None:
            selection_method = context.unrestrictedTraverse(self.selection_method_path)
          if hasattr(self, 'selection_sort_on'):
            if len(self.selection_sort_on) > 0:
              new_sort_index = []
              for (k , v) in self.selection_sort_on:
                if v == 'descending' or v == 'reverse':
                  new_sort_index += ['%s DESC' % k]
                else:
                  new_sort_index += ['%s' % k]
              sort_order_string = string.join(new_sort_index,',')
              self.selection_params['sort_on'] = sort_order_string
            elif self.selection_params.has_key('sort_on'):
              del self.selection_params['sort_on']
          if selection_method is not None:
            if callable(selection_method):
              return selection_method(**self.selection_params)
            else:
              return []
          else:
            return []
        else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
135
          # We sould try to allow more filtering
Jean-Paul Smets's avatar
Jean-Paul Smets committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
          return context.portal_catalog(uid = self.selection_uids)

    def __getitem__(self, index, REQUEST=None):
        return self(REQUEST)[index]

    security.declareProtected(ERP5Permissions.View, 'getSelectionParams')
    def getSelectionParams(self):
        LOG('getSelectionParams',0,'selection_params: %s' % str(self.selection_params))
        if self.selection_params is None:
          self.selection_params = {}
        if type(self.selection_params) != type({}):
          self.selection_params = {}
        return self.selection_params

    def getSelectionListUrl(self):
Sebastien Robin's avatar
Sebastien Robin committed
151
        result = ''
Jean-Paul Smets's avatar
Jean-Paul Smets committed
152 153
        if self.selection_list_url is None:
          self.selection_list_url = ''
Sebastien Robin's avatar
Sebastien Robin committed
154 155 156
        else:
          result = self.selection_list_url
        return result
Jean-Paul Smets's avatar
Jean-Paul Smets committed
157 158 159 160

    def getSelectionCheckedUids(self):
        if not hasattr(self, 'selection_checked_uids'):
          self.selection_checked_uids = []
161
        elif self.selection_checked_uids is None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
          self.selection_checked_uids = []
        return self.selection_checked_uids

    def getSelectionDomainPath(self):
        if self.selection_domain_path is None:
          self.selection_domain_path = self.getSelectionDomainList()[0]
        return self.selection_domain_path

    def getSelectionDomainList(self):
        if self.selection_domain_list is None:
          self.selection_domain_list = (('portal_categories',),)
        return self.selection_domain_list

    def getSelectionReportPath(self):
        if self.selection_report_path is None:
          self.selection_report_path = ('portal_categories')
        return self.selection_report_path

    def getSelectionReportList(self):
        if self.selection_report_list is None:
          self.selection_report_list = (('portal_categories',),)
        return self.selection_report_list