Commit 5204504f authored by Alexandre Boeglin's avatar Alexandre Boeglin

Removed the CreditCard class. Modified the Coordinate class to allow using it

as a portal type information.

The Coordinate class now provides methods that allow to do all the
"presentation" specific part of a Coordinate in a set of python scripts.

For example, to add a CreditCard portal type information :
- go to portal_types, choose 'Add ERP5 Type Information', select 'ERP5
  Coordinate' and name your new type 'Credit Card'
- go to your newly created 'Credit Card' type definition, and add 'CreditCard'
  to its list of Property Sheets.
- create a new form for your CreditCard : CreditCard_view and add a field
  named my_text to it.
- create two scripts, which will interact with the 'my_text' field :
  - CreditCard_fromText(text) : used to analyse the text entered by the user,
    and fill in the object's properties accordingly.
  - CreditCard_asText() : used to return a user-readable aggregation of the
    object's properties.

These two python scripts can use regular expressions to analyse the text
entered by the user, using the object's methods getRegularExpressionFindAll
and getRegularExpressionGroups for example.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4051 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 4adbd048
...@@ -31,6 +31,8 @@ from AccessControl import ClassSecurityInfo ...@@ -31,6 +31,8 @@ from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.Base import Base from Products.ERP5Type.Base import Base
import re
class Coordinate(Base): class Coordinate(Base):
""" """
Coordinates is a mix-in class which is used to store elementary Coordinates is a mix-in class which is used to store elementary
...@@ -70,12 +72,43 @@ class Coordinate(Base): ...@@ -70,12 +72,43 @@ class Coordinate(Base):
a list of Coordinate metatypes has to be defined and a list of Coordinate metatypes has to be defined and
stored somewhere. (TODO) stored somewhere. (TODO)
""" """
meta_type = 'ERP5 Coordinate'
portal_type = 'Coordinate'
add_permission = Permissions.AddPortalContent
isPortalContent = 1
isRADContent = 1
# Declarative security (replaces __ac_permissions__) # Declarative security (replaces __ac_permissions__)
security = ClassSecurityInfo() security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.View)
# Declarative properties
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem
)
### helper methods
security.declareProtected( Permissions.View, 'getRegularExpressionFindAll')
def getRegularExpressionFindAll(self, regular_expression, string):
"""
allows call of re.findall in a python script used for Coordinate
"""
return re.findall(regular_expression, string)
security.declareProtected( Permissions.View, 'getRegularExpressionGroups')
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()
### Mix-in methods ### Mix-in methods
security.declareProtected( Permissions.View, 'view' ) security.declareProtected( Permissions.View, 'view' )
def view( self ): def view(self):
""" """
Return the default view even if index_html is overridden. Return the default view even if index_html is overridden.
""" """
...@@ -94,14 +127,31 @@ class Coordinate(Base): ...@@ -94,14 +127,31 @@ class Coordinate(Base):
""" """
returns the coordinate as a text string returns the coordinate as a text string
""" """
pass script = self._getTypeBasedMethod('asText')
if script is not None:
return script()
security.declareProtected(Permissions.View, 'getText')
getText = asText
security.declareProtected( Permissions.ModifyPortalContent, 'fromText' ) security.declareProtected( Permissions.ModifyPortalContent, 'fromText' )
def fromText(self,coordinate_text): def fromText(self, coordinate_text):
""" """
modifies the coordinate according to the input text modifies the coordinate according to the input text
must be implemented by subclasses must be implemented by subclasses
""" """
script = self._getTypeBasedMethod('fromText')
if script is not None:
return script(text=coordinate_text)
security.declareProtected(Permissions.ModifyPortalContent, '_setText')
_setText = fromText
security.declareProtected(Permissions.View, 'standardTextFormat')
def standardTextFormat(self):
"""
Returns the standard text formats for telephone numbers
"""
pass pass
security.declarePrivate( '_writeFromPUT' ) security.declarePrivate( '_writeFromPUT' )
......
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
# Alexandre Boeglin <alex@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., 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
from Products.ERP5.Document.Coordinate import Coordinate
import re
class CreditCard(Coordinate, Base):
"""A credit card is a coordinate which stores a credit card information.
A credit card is a terminating leaf
in the OFS. It can not contain anything.
CreditCard inherits from Base and
from the mix-in Coordinate."""
meta_type = 'ERP5 Credit Card'
portal_type = 'Credit Card'
add_permission = Permissions.AddPortalContent
isPortalContent = 1
isRADContent = 1
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.View) # ???????
# The standard parser is used to read credit card numbers by taking
# only the digits from the input string.
standard_parser = re.compile('[0-9]+')
# The output parser is used to generate a human readable credit card
# number from a string
output_parser = re.compile('.{1,4}')
# Declarative properties
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem
, PropertySheet.CreditCard
)
security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
def fromText(self, coordinate_text,reindex_object=1):
if coordinate_text is None:
filtered_card_number = ''
else:
filtered_card_number =\
''.join(self.standard_parser.findall(coordinate_text))
self.edit(card_number=filtered_card_number)
security.declareProtected(Permissions.ModifyPortalContent, '_setText')
_setText = fromText
security.declareProtected(Permissions.View, 'asText')
def asText(self):
"""
Returns the telephone number in standard format
"""
script = self._getTypeBasedMethod('asText')
if script is not None:
return script()
if self.card_number is None:
return ''
text = ' '.join(self.output_parser.findall(self.card_number))
return text
security.declareProtected(Permissions.View, 'getText')
getText = asText
security.declareProtected(Permissions.View, 'standardTextFormat')
def standardTextFormat(self):
"""
Returns the standard text formats for telephone numbers
"""
return ("1234 5678 9012 3456",)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment