InternetProtocolAddress.py 4.3 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 30
##############################################################################
#
# Copyright (c) 2008 Nexedi SA and Contributors. All Rights Reserved.
#                    Yusei TAHARA <yusei@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

31
from Products.ERP5Type import Permissions, PropertySheet
32
from Products.ERP5Type.Base import Base
33
from Products.ERP5Type.Utils import convertToUpperCase
Nicolas Delaby's avatar
Nicolas Delaby committed
34
from Products.ERP5Type.Utils import deprecated
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

from Products.ERP5.Document.Coordinate import Coordinate

class InternetProtocolAddress(Base, Coordinate):
  """
  A internet protocol address holds a address of
  a computer on computer network using TCP/IP.
  """
  meta_type = 'ERP5 Internet Protocol Address'
  portal_type = 'Internet Protocol Address'
  add_permission = Permissions.AddPortalContent

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                      , PropertySheet.SimpleItem
54
                      , PropertySheet.CategoryCore
55
                      , PropertySheet.Coordinate
56 57 58
                      , PropertySheet.InternetProtocolAddress
                      )

59 60 61 62 63 64 65 66 67 68
  def _splitCoordinateText(self, coordinate_text):
    property_id_list = [i['id'] for i in PropertySheet.InternetProtocolAddress._properties]
    kw_dict = {}
    for line in coordinate_text.split('\n'):
      if not ':' in line:
        continue
      name, value = line.split(':', 1)
      if name in property_id_list:
        kw_dict[name] = value
    return kw_dict
69

Nicolas Delaby's avatar
Nicolas Delaby committed
70
  security.declareProtected(Permissions.AccessContentsInformation, 'asText')
71 72
  def asText(self):
    """
73
    Return the address as a complete formatted string.
74
    """
75 76
    result = Coordinate.asText(self)
    if result is None:
77 78 79 80 81 82 83 84 85 86 87 88
      if self.hasData():
        result = '\n'.join(('%s:%s' % (k, v) for k, v in\
                                    self._splitCoordinateText(self.getData())))
      else:
        tmp = []
        for prop in PropertySheet.InternetProtocolAddress._properties:
          property_id = prop['id']
          getter_name = 'get%s' % convertToUpperCase(property_id)
          getter_method = getattr(self, getter_name)
          value = getter_method() or ''
          tmp.append('%s:%s' % (property_id, value))
        result = '\n'.join(tmp)
89
    return result
90

Nicolas Delaby's avatar
Nicolas Delaby committed
91
  security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
Nicolas Delaby's avatar
Nicolas Delaby committed
92
  @deprecated
93
  def fromText(self, coordinate_text):
94 95
    """Save given data then continue parsing 
    (deprecated because computed values are stored)
96
    """
97 98
    self._setData(coordinate_text)
    kw_dict = self._splitCoordinateText(coordinate_text)
99

100 101 102 103
    for name, value in kw_dict.iteritems():
      setter_name = 'set%s' % convertToUpperCase(name)
      setter_method = getattr(self, setter_name)
      setter_method(value)
104 105 106

  def standardTextFormat(self):
    """
107
    Return the standard format string.
108
    """
109 110 111 112 113 114 115 116 117 118
    return """
host_name:mycomputer
ip_address:192.168.0.10
netmask:255.255.255.0
netmask_bit:24
network_address:192.168.0.0
broadcast_address:192.168.0.255
dns_server_ip_address:192.168.0.1
gateway_ip_address:192.168.0.1
network_interface:eth0"""