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.

163 lines
6.6 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  2. from odoo.addons.website_form.controllers import main as parent_controller
  3. from odoo import http
  4. from odoo.http import request
  5. from odoo.exceptions import ValidationError
  6. from psycopg2 import IntegrityError
  7. import base64
  8. import json
  9. class VerifyController(http.Controller):
  10. @http.route('/verify_email',
  11. type='http',
  12. auth="public",
  13. methods=['GET'],
  14. website=True)
  15. def verify_email(self, **kwargs):
  16. if kwargs:
  17. link_data = base64.b64decode(kwargs.get("data")).decode("utf-8")\
  18. .split("####")
  19. email = link_data[0]
  20. contact_name = link_data[1]
  21. email_name = link_data[2]
  22. phone_name = link_data[3]
  23. letter_name = link_data[4]
  24. link_date = link_data[5]
  25. link_date = link_date.split(" ")[0].replace("-", "")
  26. import datetime
  27. today = datetime.date.today()
  28. link = datetime.datetime.strptime(link_date, "%Y%m%d").date()
  29. diff = today - link
  30. if diff.days > 5 or diff.days < 0:
  31. return "<center style='color:red'>Not valid!<br/>"\
  32. "The link you entered is either not valid or expired."\
  33. "<br/>Please request a new link.</center>"
  34. partner = request.env['res.partner'].sudo().search([
  35. ('email', '=', email),
  36. ('name', '=', contact_name)
  37. ])
  38. if partner:
  39. for part in partner:
  40. part.is_verified = True
  41. part.last_updated = part.write_date
  42. if email_name == "True":
  43. part.email_contact = True
  44. else:
  45. part.email_contact = False
  46. if phone_name == "True":
  47. part.phone_contact = True
  48. else:
  49. part.phone_contact = False
  50. if letter_name == "True":
  51. part.letter_contact = True
  52. else:
  53. part.letter_contact = False
  54. request.env.ref(
  55. 'website_contact_extend.confirmation_email_template'
  56. ).sudo().send_mail(part.id)
  57. return "<center style='color:green'>"\
  58. "Thank You! Your email address has been verified!"\
  59. "</center>"
  60. else:
  61. return "<center style='color:red'>Not valid!<br/>"\
  62. "The link you entered is either not valid or expired."\
  63. "<br/>Please request a new link.</center>"
  64. class MyFilter(parent_controller.WebsiteForm):
  65. @http.route('/website_form/<string:model_name>',
  66. type='http',
  67. auth="public",
  68. methods=['POST'],
  69. website=True)
  70. def website_form(self, model_name, **kwargs):
  71. model_record = request.env['ir.model'].sudo().search([
  72. ('model', '=', model_name),
  73. ('website_form_access', '=', True)
  74. ])
  75. if not model_record:
  76. return json.dumps(False)
  77. # need_send_email = False
  78. try:
  79. data = self.extract_data(model_record, request.params)
  80. # contact_type = False
  81. phone_contact = False
  82. letter_contact = False
  83. email_contact = False
  84. send_mail = True
  85. index = 0
  86. for field_name, field_value in request.params.items():
  87. # if field_name == "contact_type":
  88. # contact_type = field_value
  89. if field_name == "send_mail" and field_value == "send_mail":
  90. send_mail = True
  91. if field_name == "phone_contact" \
  92. and field_value == "phone_contact":
  93. phone_contact = True
  94. if field_name == "letter_contact" \
  95. and field_value == "letter_contact":
  96. letter_contact = True
  97. if field_name == "email_contact" \
  98. and field_value == "email_contact":
  99. email_contact = True
  100. index += 1
  101. # contact_name = data.get("record").get("contact_name")
  102. # email_from = data.get("record").get("email_from")
  103. # If we encounter an issue while extracting data
  104. except ValidationError as e:
  105. # I couldn't find a cleaner way to pass data to an exception
  106. return json.dumps({'error_fields': e.args[0]})
  107. try:
  108. id_record = self.insert_record(
  109. request,
  110. model_record,
  111. data['record'],
  112. data['custom'],
  113. data.get('meta')
  114. )
  115. if id_record:
  116. self.insert_attachment(
  117. model_record,
  118. id_record,
  119. data['attachments']
  120. )
  121. if id_record and send_mail and model_name == "crm.lead":
  122. crm_lead_obj = request.env['crm.lead'].sudo().search([
  123. ('id', '=', id_record)]
  124. )
  125. email_data = crm_lead_obj.email_from + "####" +\
  126. crm_lead_obj.contact_name + "####" +\
  127. str(email_contact) + "####" +\
  128. str(phone_contact) + "####" +\
  129. str(letter_contact)+"####" +\
  130. str(crm_lead_obj.create_date)
  131. ency_email = base64.b64encode(email_data.encode()).decode(
  132. "utf-8"
  133. )
  134. action_url = '%s/verify_email/?data=%s' % (
  135. request.env['ir.config_parameter'].
  136. sudo().get_param('web.base.url'),
  137. ency_email,
  138. )
  139. if crm_lead_obj:
  140. crm_lead_obj.email_link = action_url
  141. request.env.ref(
  142. 'website_contact_extend.verification_email_template'
  143. ).send_mail(id_record)
  144. # Some fields have additional SQL constraints
  145. # that we can't check generically
  146. # Ex: crm.lead.probability which is a float between 0 and 1
  147. # TODO: How to get the name of the erroneous field ?
  148. except IntegrityError:
  149. return json.dumps(False)
  150. request.session['form_builder_model_model'] = model_record.model
  151. request.session['form_builder_model'] = model_record.name
  152. request.session['form_builder_id'] = id_record
  153. return json.dumps({'id': id_record})