You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2010 Camptocamp SA (http://www.camptocamp.com)
  5. # All Right Reserved
  6. #
  7. # Author : Vincent Renaville
  8. #
  9. # WARNING: This program as such is intended to be used by professional
  10. # programmers who take the whole responsability of assessing all potential
  11. # consequences resulting from its eventual inadequacies and bugs
  12. # End users who are looking for a ready-to-use solution with commercial
  13. # garantees and support are strongly adviced to contract a Free Software
  14. # Service Company
  15. #
  16. # This program is Free Software; you can redistribute it and/or
  17. # modify it under the terms of the GNU General Public License
  18. # as published by the Free Software Foundation; either version 2
  19. # of the License, or (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU General Public License
  27. # along with this program; if not, write to the Free Software
  28. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. #
  30. ##############################################################################
  31. # init_import.py
  32. #
  33. # Created by Nicolas Bessi on 28.04.09.
  34. # Copyright (c) 2009 CamptoCamp. All rights reserved.
  35. #
  36. import base64
  37. import unicodedata
  38. import re
  39. import wizard
  40. from openerp import pooler
  41. from openerp import netsvc
  42. _FORM = '''<?xml version="1.0"?>
  43. <form string="Export addresses to ldap">
  44. </form>'''
  45. _FORM1 = """<?xml version="1.0"?>
  46. <form string="Export log">
  47. <separator colspan="4" string="Clic on 'Save as' to save the log file :" />
  48. <field name="errors"/>
  49. </form>
  50. """
  51. _FIELDS = {
  52. 'errors': {
  53. 'string': 'Error report',
  54. 'type': 'binary',
  55. 'readonly': True,
  56. },
  57. }
  58. # As this is a bulk batch wizard the performance process was not really taken in account ###
  59. # The ideal way of doing would be to modify the connexion settings in order to have a connexion singleton
  60. # in the file partner.py it will avoid connexion renegotiation for each partner.
  61. def _action_import_addresses(self, cr, uid, data, context):
  62. """ This function create or update each addresses present in the database.
  63. It will also generate an error report"""
  64. logger = netsvc.Logger()
  65. error_report = [u'Error report']
  66. add_obj = pooler.get_pool(cr.dbname).get('res.partner.address')
  67. add_ids = add_obj.search(cr, uid, [])
  68. addresses = add_obj.browse(cr, uid, add_ids)
  69. phone_fields = ['phone', 'fax', 'mobile', 'private_phone']
  70. for add in addresses:
  71. vals = {
  72. 'partner_id': add.partner_id.id,
  73. 'email': add.email,
  74. 'phone': add.phone,
  75. 'fax': add.fax,
  76. 'mobile': add.mobile,
  77. 'firstname': add.firstname,
  78. 'lastname': add.lastname,
  79. 'private_phone': add.private_phone,
  80. 'street': add.street,
  81. 'street2': add.street2,
  82. 'city': add.city,
  83. }
  84. # Validating the mail
  85. if add.email:
  86. if re.match(
  87. "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", add.email) is None or\
  88. re.search(u"[éèàêöüäï&]", add.email) is not None:
  89. msg = u'Address %s for partner %s has email that is invalid %s' % (
  90. unicode(vals['firstname']) + ' ' + unicode(vals['lastname']),
  91. add.partner_id.name,
  92. unicode(add.email)
  93. )
  94. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  95. error_report.append(msg)
  96. vals['email'] = False
  97. # Validating the Phone
  98. for key in phone_fields:
  99. if (not unicode(vals[key]).startswith('+') or unicode(vals[key]).find("\n") != -1
  100. or re.search(u"[éèàêöüä#&]", unicode(vals[key])) is not None):
  101. vals[key] = False
  102. msg = u'Addresse %s for partner %s has %s that is invalid ' % (
  103. unicode(vals['firstname']) + ' ' + unicode(vals['lastname']),
  104. add.partner_id.name,
  105. key
  106. )
  107. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  108. error_report.append(msg)
  109. # Validating the CN
  110. if not add.lastname and add.firstname:
  111. msg = (u'!!! Address %s for partner %s has no last name and first name that is valid partner name was used'
  112. % (unicode(add.id), add.partner_id.name))
  113. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  114. error_report.append(msg)
  115. # We save to LDAP
  116. add.write(vals, {'init_mode': True})
  117. # we by pass the encoding errors
  118. map(lambda x: unicodedata.normalize("NFKD", x).encode('ascii', 'ignore'), error_report)
  119. error_report = "\n".join(error_report)
  120. logger.notifyChannel("MY TOPIC", netsvc.LOG_ERROR, error_report)
  121. try:
  122. data = base64.encodestring(error_report.encode())
  123. except:
  124. data = base64.encodestring("Could not generate report file. Please look in the log for details")
  125. return {'errors': data}
  126. class Wiz_import_addresses(wizard.interface):
  127. states = {
  128. 'init': {
  129. 'actions': [],
  130. 'result': {
  131. 'type': 'form',
  132. 'arch': _FORM,
  133. 'fields': {},
  134. 'state': [
  135. ('end', 'Cancel'),
  136. ('importadd', 'Export adresses into company LDAP')
  137. ]
  138. }
  139. },
  140. 'importadd': {
  141. 'actions': [_action_import_addresses],
  142. 'result': {
  143. 'state': [('end', 'OK', 'gtk-ok', True)],
  144. 'arch': _FORM1,
  145. 'fields': _FIELDS,
  146. 'type': 'form'
  147. }
  148. }
  149. }
  150. Wiz_import_addresses('ldap.import_adresses')