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.

174 lines
6.2 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
  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. from openerp.tools.translate import _
  43. from openerp.tools import ustr
  44. _FORM = '''<?xml version="1.0"?>
  45. <form string="Export addresses to ldap">
  46. </form>'''
  47. _FORM1 = """<?xml version="1.0"?>
  48. <form string="Export log">
  49. <separator colspan="4" string="Clic on 'Save as' to save the log file :" />
  50. <field name="errors"/>
  51. </form>
  52. """
  53. _FIELDS = {
  54. 'errors': {
  55. 'string': 'Error report',
  56. 'type': 'binary',
  57. 'readonly': True,
  58. },
  59. }
  60. # As this is a bulk batch wizard the performance process was not really
  61. # taken in account ###
  62. # The ideal way of doing would be to modify the connexion settings in
  63. # order to have a connexion singleton in the file partner.py it will
  64. # avoid connexion renegotiation for each partner.
  65. def _action_import_addresses(self, cr, uid, data, context):
  66. """ This function create or update each addresses present in the database.
  67. It will also generate an error report"""
  68. logger = netsvc.Logger()
  69. error_report = [u'Error report']
  70. add_obj = pooler.get_pool(cr.dbname).get('res.partner.address')
  71. add_ids = add_obj.search(cr, uid, [])
  72. addresses = add_obj.browse(cr, uid, add_ids)
  73. phone_fields = ['phone', 'fax', 'mobile', 'private_phone']
  74. for add in addresses:
  75. vals = {
  76. 'partner_id': add.partner_id.id,
  77. 'email': add.email,
  78. 'phone': add.phone,
  79. 'fax': add.fax,
  80. 'mobile': add.mobile,
  81. 'firstname': add.firstname,
  82. 'lastname': add.lastname,
  83. 'private_phone': add.private_phone,
  84. 'street': add.street,
  85. 'street2': add.street2,
  86. 'city': add.city,
  87. }
  88. # Validating the mail
  89. if add.email:
  90. if re.match(
  91. "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\."
  92. "([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", add.email) is None or\
  93. re.search(u"[éèàêöüäï&]", add.email) is not None:
  94. msg = _(
  95. 'Address %s for partner %s has email that is invalid %s'
  96. ) % (
  97. ustr(vals['firstname']) + ' ' + ustr(vals['lastname']),
  98. add.partner_id.name,
  99. ustr(add.email)
  100. )
  101. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  102. error_report.append(msg)
  103. vals['email'] = False
  104. # Validating the Phone
  105. for key in phone_fields:
  106. if (not ustr(vals[key]).startswith('+')
  107. or ustr(vals[key]).find("\n") != -1
  108. or re.search(u"[éèàêöüä#&]", ustr(vals[key])) is not None):
  109. vals[key] = False
  110. msg = _(
  111. 'Address %s for partner %s has %s that is invalid '
  112. ) % (
  113. ustr(vals['firstname']) + ' ' + ustr(vals['lastname']),
  114. add.partner_id.name,
  115. key
  116. )
  117. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  118. error_report.append(msg)
  119. # Validating the CN
  120. if not add.lastname and add.firstname:
  121. msg = (_('!!! Address %s for partner %s has no last name and '
  122. 'first name that is valid partner name was used')
  123. % (ustr(add.id), add.partner_id.name))
  124. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  125. error_report.append(msg)
  126. # We save to LDAP
  127. add.write(vals, {'init_mode': True})
  128. # we by pass the encoding errors
  129. map(lambda x: unicodedata.normalize("NFKD", x).encode('ascii', 'ignore'),
  130. error_report)
  131. error_report = "\n".join(error_report)
  132. logger.notifyChannel("MY TOPIC", netsvc.LOG_ERROR, error_report)
  133. try:
  134. data = base64.encodestring(error_report.encode())
  135. except:
  136. data = base64.encodestring("Could not generate report file. "
  137. "Please look in the log for details")
  138. return {'errors': data}
  139. class Wiz_import_addresses(wizard.interface):
  140. states = {
  141. 'init': {
  142. 'actions': [],
  143. 'result': {
  144. 'type': 'form',
  145. 'arch': _FORM,
  146. 'fields': {},
  147. 'state': [
  148. ('end', 'Cancel'),
  149. ('importadd', 'Export adresses into company LDAP')
  150. ]
  151. }
  152. },
  153. 'importadd': {
  154. 'actions': [_action_import_addresses],
  155. 'result': {
  156. 'state': [('end', 'OK', 'gtk-ok', True)],
  157. 'arch': _FORM1,
  158. 'fields': _FIELDS,
  159. 'type': 'form'
  160. }
  161. }
  162. }
  163. Wiz_import_addresses('ldap.import_adresses')