Coordinate.py 8.17 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.
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
#
# 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 AccessControl import ClassSecurityInfo

from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.Base import Base
33
from Products.CMFDefault.utils import formatRFC822Headers
34 35
import re

Jean-Paul Smets's avatar
Jean-Paul Smets committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class Coordinate(Base):
    """
        Coordinates is a mix-in class which is used to store elementary
        coordinates of an Entity (ex. Person, Organisation)

        UNKNOWN Coordinates are treated as a document
        with a subject (to find a given type of coordinate).

        Coordinates use a URL approach. Any coordinate can
        be rendered in a URL style. For example::

            - tel:+33(0)662057614

            - fax:+33(0)153010929

            - mailto:jp@nexedi.com

            - naf:722Z

Romain Courteaud's avatar
Romain Courteaud committed
55
            - smail://Nexedi/Jean-Paul Smets/943, av de la Republique/59700
Jean-Paul Smets's avatar
Jean-Paul Smets committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
                        Marcq-en-Baroeul/France

        Coordinates are stored as content within an Entity. Coordinate ids
        are generated as a combination of a so-called role (ex. shipping,
        billing, etc.) and a class string. This way, coordinates can
        be accessed as attributes of the containing Entity::

            - default_phone

            - default_fax

            - billing_address

            - shipping_address

        In order to be able to list all coordinates of an Entity,
        a list of Coordinate metatypes has to be defined and
        stored somewhere. (TODO)
        """
75 76 77 78 79 80 81

    meta_type = 'ERP5 Coordinate'
    portal_type = 'Coordinate'
    add_permission = Permissions.AddPortalContent
    isPortalContent = 1
    isRADContent = 1

Jean-Paul Smets's avatar
Jean-Paul Smets committed
82 83
    # Declarative security (replaces __ac_permissions__)
    security = ClassSecurityInfo()
84
    security.declareObjectProtected(Permissions.AccessContentsInformation)
85 86 87 88 89 90 91

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.SimpleItem
                      )

    ### helper methods
92 93
    security.declareProtected( Permissions.AccessContentsInformation,
                               'getRegularExpressionFindAll')
94 95 96 97 98 99
    def getRegularExpressionFindAll(self, regular_expression, string):
      """
      allows call of re.findall in a python script used for Coordinate
      """
      return re.findall(regular_expression, string)

100 101
    security.declareProtected( Permissions.AccessContentsInformation,
                               'getRegularExpressionGroups')
102 103 104 105 106 107 108 109
    def getRegularExpressionGroups(self, regular_expression, string):
      """
      allows call of re.search.groups in a python script used for Coordinate
      """
      match = re.search(regular_expression, string)
      if match is None:
        return ()
      return re.search(regular_expression, string).groups()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
110 111 112

    ### Mix-in methods
    security.declareProtected( Permissions.View, 'view' )
113
    def view(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
114 115 116 117 118
        """
            Return the default view even if index_html is overridden.
        """
        return self()

119 120
    security.declareProtected( Permissions.AccessContentsInformation,
                               'SearchableText' )
Yoshinori Okuji's avatar
Yoshinori Okuji committed
121
    def getSearchableText(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
122 123 124 125
        """
            text for indexing
        """
        return "%s %s %s" % (self.title, self.description, self.asText())
Yoshinori Okuji's avatar
Yoshinori Okuji committed
126
    SearchableText = getSearchableText
Jean-Paul Smets's avatar
Jean-Paul Smets committed
127

128 129
    security.declareProtected( Permissions.AccessContentsInformation,
                               'asText' )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
130 131 132 133
    def asText(self):
        """
            returns the coordinate as a text string
        """
134 135 136 137
        script = self._getTypeBasedMethod('asText')
        if script is not None:
          return script()

138 139
    security.declareProtected( Permissions.AccessContentsInformation,
                               'getText')
140 141 142 143 144 145
    def getText(self):
      """
      calls asText
      """
      return self.asText()
      
Jean-Paul Smets's avatar
Jean-Paul Smets committed
146 147

    security.declareProtected( Permissions.ModifyPortalContent, 'fromText' )
148
    def fromText(self, coordinate_text):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
149 150 151 152
        """
             modifies the coordinate according to the input text
             must be implemented by subclasses
        """
153 154 155 156 157
        script = self._getTypeBasedMethod('fromText')
        if script is not None:
          return script(text=coordinate_text)

    security.declareProtected(Permissions.ModifyPortalContent, '_setText')
158 159 160 161 162
    def _setText(self, value):
      """
      calls fromText
      """
      return self.fromText(value)
163

164 165
    security.declareProtected( Permissions.AccessContentsInformation,
                               'standardTextFormat')
166 167 168 169
    def standardTextFormat(self):
        """
        Returns the standard text formats for telephone numbers
        """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
170 171 172 173 174 175
        pass

    security.declarePrivate( '_writeFromPUT' )
    def _writeFromPUT( self, body ):
        headers = {}
        headers, body = parseHeadersBody(body, headers)
176
        lines = body.split( '\n' )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        self.edit( lines[0] )
        headers['Format'] = self.COORDINATE_FORMAT
        new_subject = keywordsplitter(headers)
        headers['Subject'] = new_subject or self.Subject()
        haveheader = headers.has_key
        for key, value in self.getMetadataHeaders():
            if key != 'Format' and not haveheader(key):
                headers[key] = value

        self._editMetadata(title=headers['Title'],
                          subject=headers['Subject'],
                          description=headers['Description'],
                          contributors=headers['Contributors'],
                          effective_date=headers['Effective_date'],
                          expiration_date=headers['Expiration_date'],
                          format=headers['Format'],
                          language=headers['Language'],
                          rights=headers['Rights'],
                          )

    ## FTP handlers
    security.declareProtected( Permissions.ModifyPortalContent, 'PUT')
    def PUT(self, REQUEST, RESPONSE):
        """
            Handle HTTP / WebDAV / FTP PUT requests.
        """
        if not NoWL:
            self.dav__init(REQUEST, RESPONSE)
            self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)
        body = REQUEST.get('BODY', '')
        try:
            self._writeFromPUT( body )
            RESPONSE.setStatus(204)
            return RESPONSE
        except ResourceLockedError, msg:
            get_transaction().abort()
            RESPONSE.setStatus(423)
            return RESPONSE

    security.declareProtected( Permissions.View, 'manage_FTPget' )
    def manage_FTPget(self):
        """
            Get the coordinate as text for WebDAV src / FTP download.
        """
        hdrlist = self.getMetadataHeaders()
        hdrtext = formatRFC822Headers( hdrlist )
        bodytext = '%s\n\n%s' % ( hdrtext, self.asText() )

        return bodytext

    security.declareProtected( Permissions.View, 'get_size' )
    def get_size( self ):
        """
            Used for FTP and apparently the ZMI now too
        """
        return len(self.manage_FTPget())