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.

159 lines
6.1 KiB

  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 wizard
  37. import pooler
  38. import base64
  39. import unicodedata
  40. import netsvc
  41. import re
  42. _FORM = '''<?xml version="1.0"?>
  43. <form string="Export adresses 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 bulck batch wizzard the performance process was not reallay taken in account ###
  59. ##The ideal way of doing would be to modify the connexion settings in order to have a connexion singelton
  60. ## in the file partner.py it will avoid connexion renegotiation for each partner.
  61. def _action_import_adresses(self, cr, uid, data, context):
  62. """ This function create or update each adresses present in the database.
  63. It will also genreate 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. vals['partner_id'] = add.partner_id.id
  73. vals['email'] = add.email
  74. vals['phone'] = add.phone
  75. vals['fax'] = add.fax
  76. vals['mobile'] = add.mobile
  77. vals['firstname'] = add.firstname
  78. vals['lastname'] = add.lastname
  79. vals['private_phone'] = add.private_phone
  80. vals['street'] = add.street
  81. vals['street2'] = add.street2
  82. vals['city'] = add.city
  83. # Validating the mail
  84. if add.email :
  85. if re.match(
  86. "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", add.email) is None or\
  87. re.search(u"[éèàêöüäï&]", add.email) is not None:
  88. msg=u'Addresse %s for partner %s has email that is invalid %s'%(
  89. unicode(vals['firstname']) +' '+ unicode(vals['lastname']),
  90. add.partner_id.name,
  91. unicode(add.email)
  92. )
  93. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  94. error_report.append(msg)
  95. vals['email'] = False
  96. # Validating the Phone
  97. for key in phone_fields :
  98. if not unicode(vals[key]).startswith('+') or unicode(vals[key]).find("\n") != -1\
  99. or re.search(u"[éèàêöüä#&]", unicode(vals[key])) is not None:
  100. vals[key] = False
  101. msg = u'Addresse %s for partner %s has %s that is invalid '%(
  102. unicode(vals['firstname']) +' '+ unicode(vals['lastname']),
  103. add.partner_id.name,
  104. key
  105. )
  106. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  107. error_report.append(msg)
  108. # Validating the CN
  109. if not add.lastname and add.firstname:
  110. msg = u'!!! Addresse %s for partner %s has no last name and first name that is valid partner name was used'%(
  111. unicode(add.id),
  112. add.partner_id.name,
  113. )
  114. logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
  115. error_report.append(msg)
  116. # We save to LDAP
  117. add.write(vals, {'init_mode':True})
  118. #we by pass the encoding errors
  119. map(lambda x: unicodedata.normalize("NFKD",x).encode('ascii','ignore'), error_report)
  120. error_report = "\n".join(error_report)
  121. logger.notifyChannel("MY TOPIC", netsvc.LOG_ERROR, error_report)
  122. try:
  123. data= base64.encodestring(error_report.encode())
  124. except Exception, e:
  125. data= base64.encodestring("Could not generate report file. Please look in the log for details")
  126. return {'errors': data}
  127. class Wiz_import_addresses(wizard.interface):
  128. states = {
  129. 'init': {
  130. 'actions': [],
  131. 'result': {
  132. 'type': 'form',
  133. 'arch':_FORM,
  134. 'fields':{},
  135. 'state':[
  136. ('end','Cancel'),
  137. ('importadd','Export adresses into company LDAP')
  138. ]
  139. }
  140. },
  141. 'importadd': {
  142. 'actions': [_action_import_adresses],
  143. 'result': {
  144. 'state':[('end', 'OK', 'gtk-ok', True)],
  145. 'arch' : _FORM1,
  146. 'fields' : _FIELDS,
  147. 'type':'form'
  148. }
  149. }
  150. }
  151. Wiz_import_addresses('ldap.import_adresses')