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.

131 lines
5.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2019 Coop IT Easy (http://www.coopiteasy.be)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import logging
  5. from openerp import tools
  6. from openerp.tools.translate import _
  7. from openerp import http
  8. from openerp.http import request
  9. from openerp.addons.auth_signup.res_users import SignupError
  10. from openerp.addons.auth_signup.controllers.main import AuthSignupHome
  11. from openerp.addons.base_iban import base_iban
  12. from openerp.exceptions import ValidationError
  13. _logger = logging.getLogger(__name__)
  14. FORM_FIELDS = ['login', 'firstname', 'password', 'phone', 'street',
  15. 'city', 'zip_code', 'country_id', 'gender', 'birthdate']
  16. class AuthSignupHome(AuthSignupHome):
  17. def _signup_with_values(self, token, values):
  18. user_obj = request.env['res.users']
  19. db, login, password = user_obj.sudo().signup(values, token)
  20. # as authenticate will use its own cursor we need to commit
  21. # the current transaction
  22. request.cr.commit()
  23. uid = request.session.authenticate(db, login, password)
  24. if not uid:
  25. raise SignupError(_('Authentication Failed.'))
  26. return uid
  27. def do_signup(self, qcontext):
  28. """ Shared helper that creates a res.partner out of a token """
  29. bank_obj = request.env['res.partner.bank']
  30. lang_obj = request.env['res.lang']
  31. values = dict((key, qcontext.get(key)) for key in FORM_FIELDS)
  32. assert any([k for k in values.values()]),"The form was not properly filled in."
  33. assert values.get('password') == qcontext.get('confirm_password'), "Passwords do not match; please retype them."
  34. supported_langs = [lang['code'] for lang
  35. in lang_obj.sudo().search_read([], ['code'])]
  36. values['lang'] = qcontext.get("lang")
  37. firstname = qcontext.get("firstname").title()
  38. lastname = qcontext.get('name').upper()
  39. values['lastname'] = lastname
  40. values['name'] = firstname + ' ' + lastname
  41. values['zip'] = values['zip_code']
  42. uid = self._signup_with_values(qcontext.get('token'), values)
  43. iban = qcontext.get('iban')
  44. user = request.env['res.users'].sudo().search([('id', '=', uid)])
  45. bank_obj.sudo().create({'partner_id': user.partner_id.id,
  46. 'acc_number': iban})
  47. request.cr.commit()
  48. @http.route('/web/company_signup', type='http', auth='public',
  49. website=True)
  50. def web_auth_company_signup(self, *args, **kw):
  51. vals = {}
  52. vals['company'] = True
  53. return self.web_auth_signup(**vals)
  54. @http.route('/web/individual_signup', type='http', auth='public',
  55. website=True)
  56. def web_auth_individua_signup(self, *args, **kw):
  57. vals = {}
  58. vals['company'] = False
  59. return self.web_auth_signup(**vals)
  60. @http.route('/web/signup', type='http', auth='public', website=True)
  61. def web_auth_signup(self, *args, **kw):
  62. qcontext = self.get_auth_signup_qcontext()
  63. users_obj = request.env["res.users"]
  64. country_obj = request.env['res.country']
  65. lang_obj = request.env['res.lang']
  66. sub_req_obj = request.env['subscription.request']
  67. render_template = 'auth_signup.signup'
  68. if kw.get('company', False):
  69. render_template = 'easy_my_coop.company_signup'
  70. qcontext['is_company'] = True
  71. qcontext['name'] = self.company_name
  72. qcontext['last_name'] = self.company_name
  73. qcontext['company_register_number'] = self.company_register_number
  74. qcontext['email'] = self.company_email
  75. qcontext['out_inv_comm_type'] = 'bba'
  76. qcontext['cooperator'] = True
  77. qcontext['out_inv_comm_algorithm'] = 'random'
  78. if qcontext.get("login") != qcontext.get("confirm_email"):
  79. qcontext["error"] = _("The email address doesn't seem to match"
  80. " the email confirmation.")
  81. if qcontext.get("login", False) and not tools.single_email_re.match(qcontext.get("login", "")):
  82. qcontext["error"] = _("That does not seem to be an email address.")
  83. if qcontext.get("iban", False):
  84. try:
  85. base_iban.validate_iban(qcontext.get("iban"))
  86. except ValidationError:
  87. qcontext["error"] = _("Please give a correct IBAN number.")
  88. if not qcontext.get('token') and not qcontext.get('signup_enabled'):
  89. raise werkzeug.exceptions.NotFound()
  90. if 'error' not in qcontext and request.httprequest.method == 'POST':
  91. try:
  92. self.do_signup(qcontext)
  93. return super(AuthSignupHome, self).web_login(*args, **kw)
  94. except (SignupError, AssertionError), e:
  95. domain = [("login", "=", qcontext.get("login"))]
  96. if users_obj.sudo().search(domain):
  97. qcontext["error"] = _("Another user is already registered "
  98. "using this email address.")
  99. else:
  100. _logger.error(e.message)
  101. qcontext['error'] = _("Could not create a new account.")
  102. qcontext['langs'] = lang_obj.sudo().search([])
  103. qcontext['countries'] = country_obj.sudo().search([])
  104. qcontext['country_id'] = '21'
  105. fields_desc = sub_req_obj.sudo().fields_get(['company_type', 'gender'])
  106. qcontext['company_types'] = fields_desc['company_type']['selection']
  107. qcontext['genders'] = fields_desc['gender']['selection']
  108. return request.render(render_template, qcontext)
  109. def get_partner_company_vals(self):
  110. partner_vals = {
  111. 'data_policy_approved': self.data_policy_approved,
  112. 'internal_rules_approved': self.internal_rules_approved
  113. }
  114. return partner_vals