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.

315 lines
12 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. link_data = {}
  10. class ContactByController(http.Controller):
  11. @http.route('/check_user_exists/', type='json', auth="public", methods=['POST'], website=True, csrf=False)
  12. def check_user_exists(self, email_from, company_name, **kwargs):
  13. res_part_rec = request.env['res.partner'].sudo().search([
  14. ('email', '=', email_from),
  15. ('company_name', '=', company_name)])
  16. res_part_email = request.env['res.partner'].sudo().search([
  17. ('email', '=', email_from)])
  18. if len(res_part_rec) > 0 or len(res_part_rec) == 0 and len(res_part_email) > 0:
  19. # if partner:
  20. return True
  21. return False
  22. @http.route('/contact-us-form-review/',
  23. type='http',
  24. auth="public",
  25. methods=['GET'],
  26. website=True)
  27. def contact_us_form_review(self, **kwargs):
  28. # print(kwargs.get("data"))
  29. # link_data =base64.b64decode(kwargs.get("data")).decode("utf-8") \
  30. # print('FD')
  31. # print(request.session['form_data'])
  32. return http.request.render('website_contact_extend.review_form', {'form_data': request.session['form_data'],
  33. 'form_data_dict': request.session['form_data_dict']})
  34. @http.route('/contact_by/<string:data>',
  35. type='http',
  36. auth="public",
  37. methods=['GET'],
  38. website=True)
  39. def contact_by(self, data, **kwargs):
  40. global link_data
  41. print(data)
  42. # print(kwargs.get("data"))
  43. # link_data =base64.b64decode(kwargs.get("data")).decode("utf-8") \
  44. link_data = data
  45. return http.request.render('website_contact_extend.contactby_form')
  46. # return "<center style='color:red'>You are at the right place</center>"
  47. @http.route('/contact_by_send/<string:model_name>',
  48. type='http',
  49. auth="public",
  50. methods=['POST'],
  51. website=True)
  52. def contact_by_send(self, model_name, **kwargs):
  53. global link_data
  54. # form_contact_name = ""
  55. model_record = request.env['ir.model'].sudo().search([
  56. ('model', '=', model_name),
  57. ('website_form_access', '=', True)
  58. ])
  59. print(model_record)
  60. print(request.params)
  61. means_of_cont_dict = request.params
  62. if 'letter_contact' in means_of_cont_dict:
  63. letter_name = "True"
  64. else:
  65. letter_name = "False"
  66. if 'email_contact' in means_of_cont_dict:
  67. email_name = "True"
  68. else:
  69. email_name = "False"
  70. if 'phone_contact' in means_of_cont_dict:
  71. phone_name = "True"
  72. else:
  73. phone_name = "False"
  74. if not model_record:
  75. return json.dumps(False)
  76. # print(link_data)
  77. link_data_split = base64.b64decode(
  78. link_data).decode("utf-8").split("####")
  79. # print(link_data_split)
  80. email = link_data_split[0]
  81. contact_name = link_data_split[1]
  82. print(email, contact_name, email_name, phone_name, letter_name)
  83. partner = request.env['res.partner'].sudo().search([
  84. ('email', '=', email),
  85. ('name', '=', contact_name)
  86. ])
  87. if not partner:
  88. partner = request.env['res.partner'].sudo().create(
  89. {'name': contact_name, 'email': email})
  90. if partner:
  91. for part in partner:
  92. part.is_verified = True
  93. part.last_updated = part.write_date
  94. if email_name == "True":
  95. part.email_contact = True
  96. else:
  97. part.email_contact = False
  98. if phone_name == "True":
  99. part.phone_contact = True
  100. else:
  101. part.phone_contact = False
  102. if letter_name == "True":
  103. part.letter_contact = True
  104. else:
  105. part.letter_contact = False
  106. request.session['form_builder_model_model'] = model_record.model
  107. request.session['form_builder_model'] = model_record.name
  108. request.session['form_builder_id'] = partner[0].id
  109. return json.dumps({'id': partner[0].id})
  110. # return http.request.render('website_contact_extend.disp_msg_template', {'message_success': 'Means of contact changed!'})
  111. # return "<p style='color: green'>Means of contact changed!</p>"
  112. # else:
  113. # # return http.request.render('website_contact_extend.disp_msg_template', {'message_failure': 'Could not change your means of contact. Please request a new link'})
  114. # return "<p style='color: red'>Could not change your means of contact. Please request a new link</p>"
  115. class FormReview(parent_controller.WebsiteForm):
  116. @http.route('/form_review_send/<string:model_name>',
  117. type='http',
  118. auth="public",
  119. methods=['POST'],
  120. website=True)
  121. def form_review_send(self, model_name, **kwargs):
  122. # model_name = 'crm.lead'
  123. print('Send')
  124. res_partner_data = request.session['form_data_dict']
  125. model_record = request.env['ir.model'].sudo().search([
  126. ('model', '=', model_name),
  127. ('website_form_access', '=', True)
  128. ])
  129. res_part_rec = request.env['res.partner'].sudo().search([
  130. ('email', '=', res_partner_data['email_from']),
  131. ('company_name', '=', res_partner_data['partner_name'])])
  132. res_part_email = request.env['res.partner'].sudo().search([
  133. ('email', '=', res_partner_data['email_from'])], limit=1)
  134. # if len(res_part_rec) == 0 and res_part_email:
  135. # print('Company change detected!')
  136. print(res_part_email)
  137. print('Verified?')
  138. print(res_part_email[0].is_verified)
  139. id_record = self.insert_record(
  140. request,
  141. model_record,
  142. res_partner_data,
  143. '',
  144. ''
  145. )
  146. if len(res_part_email) > 0 and len(res_part_rec) == 0 and res_part_email[0].is_verified == True:
  147. print('Company change detected')
  148. res_cat = request.env['res.partner.category'].search(
  149. [('name', '=', 'New')])
  150. print(res_cat)
  151. cat_id = 0
  152. if len(res_cat) == 0:
  153. print('Hit')
  154. id_part_res_cat = self.insert_record(
  155. request,
  156. request.env['ir.model'].search(
  157. [('model', '=', 'res.partner.category')]),
  158. {'name': 'New', 'create_uid': request.env.uid},
  159. '',
  160. ''
  161. )
  162. cat_id = id_part_res_cat
  163. print(id_part_res_cat)
  164. else:
  165. cat_id = res_cat[0].id
  166. res_partner_dict = {}
  167. res_partner_dict['name'] = res_partner_data['contact_name']
  168. res_partner_dict['display_name'] = res_partner_data['contact_name']
  169. res_partner_dict['email'] = res_partner_data['email_from']
  170. res_partner_dict['company_name'] = res_partner_data['partner_name']
  171. res_partner_dict['category_id'] = [[6, False, [cat_id]]]
  172. # res_partner_dict['parent_id'] = res_part_email[0].id
  173. if 'phone' in res_partner_data:
  174. res_partner_dict['phone'] = res_partner_data['phone']
  175. print('Here')
  176. update_parent_partner = request.env['res.partner'].sudo().search([
  177. ('id', '=', res_part_email[0].id)])
  178. print(update_parent_partner)
  179. if update_parent_partner and len(update_parent_partner) > 0:
  180. update_parent_partner[0].category_id = False
  181. update_parent_partner_comp = request.env['res.partner'].sudo().search([
  182. ('id', '=', res_part_email[0].parent_id.id)])
  183. if update_parent_partner_comp and len(update_parent_partner_comp) > 0:
  184. update_parent_partner_comp[0].category_id = False
  185. res_partner_company_dict = res_partner_dict.copy()
  186. res_partner_company_dict.pop('email')
  187. res_partner_company_dict.pop('company_name')
  188. # res_partner_company_dict.pop('parent_id')
  189. if 'phone' in res_partner_company_dict:
  190. res_partner_company_dict.pop('phone')
  191. res_partner_company_dict['name'] = res_partner_data['partner_name']
  192. res_partner_company_dict['display_name'] = res_partner_data['partner_name']
  193. res_partner_company_dict['customer'] = False
  194. res_partner_company_dict['is_company'] = True
  195. res_partner_company_dict['company_type'] = 'company'
  196. # res_partner_dict['parent_id'] = id_record_res_company
  197. id_record_res = self.insert_record(
  198. request,
  199. request.env['ir.model'].search(
  200. [('model', '=', 'res.partner')]),
  201. res_partner_dict,
  202. '',
  203. ''
  204. )
  205. res_partner_company_dict['child_ids'] = [
  206. [6, 'virtual_1798', [id_record_res]]]
  207. id_record_res_company = self.insert_record(
  208. request,
  209. request.env['ir.model'].search(
  210. [('model', '=', 'res.partner')]),
  211. res_partner_company_dict,
  212. '',
  213. ''
  214. )
  215. if update_parent_partner and len(update_parent_partner) > 0:
  216. update_parent_partner[0].child_ids = [
  217. [6, 'virtual_1798', [id_record_res]]]
  218. id_record = self.insert_record(
  219. request,
  220. model_record,
  221. res_partner_data,
  222. '',
  223. ''
  224. )
  225. if id_record:
  226. self.insert_attachment(
  227. model_record,
  228. id_record,
  229. ''
  230. )
  231. if id_record and model_name == "crm.lead":
  232. crm_lead_obj = request.env['crm.lead'].sudo().search([
  233. ('id', '=', id_record)]
  234. )
  235. email_data = crm_lead_obj.email_from + "####" +\
  236. crm_lead_obj.contact_name + "####" +\
  237. str('') + "####" +\
  238. str('') + "####" +\
  239. str('')+"####" + \
  240. str(res_partner_data['send_mail']) + "####" + \
  241. str(res_partner_data['request_gdpdr']) + "####" + \
  242. str(id_record) + "####" + \
  243. str(crm_lead_obj.create_date)
  244. ency_email = base64.b64encode(email_data.encode()).decode(
  245. "utf-8"
  246. )
  247. action_url = '%s/verify_email/?data=%s' % (
  248. request.env['ir.config_parameter'].
  249. sudo().get_param('web.base.url'),
  250. ency_email,
  251. )
  252. if crm_lead_obj:
  253. crm_lead_obj.email_link = action_url
  254. request.env.ref(
  255. 'website_contact_extend.verification_email_template'
  256. ).sudo().send_mail(id_record)
  257. request.env.ref('website_contact_extend.email_template_onchange_data').sudo(
  258. ).send_mail(id_record)
  259. request.session['form_builder_model_model'] = model_record.model
  260. request.session['form_builder_model'] = model_record.name
  261. request.session['form_builder_id'] = id_record
  262. return json.dumps({'id': id_record})