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.

312 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. import base64
  6. import json
  7. link_data = {}
  8. class ContactByController(http.Controller):
  9. @http.route('/check_user_exists/', type='json', auth="public",
  10. methods=['POST'], website=True, csrf=False)
  11. def check_user_exists(self, email_from, company_name, **kwargs):
  12. res_part_rec = request.env['res.partner'].sudo().search([
  13. ('email', '=', email_from),
  14. ('company_name', '=', company_name)])
  15. res_part_email = request.env['res.partner'].sudo().search([
  16. ('email', '=', email_from)])
  17. if len(res_part_rec) > 0 or len(res_part_rec) == 0 \
  18. 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(
  33. 'website_contact_extend.review_form',
  34. {'form_data': request.session['form_data'],
  35. 'form_data_dict': request.session['form_data_dict']})
  36. @http.route('/contact_by/<string:data>',
  37. type='http',
  38. auth="public",
  39. methods=['GET'],
  40. website=True)
  41. def contact_by(self, data, **kwargs):
  42. global link_data
  43. # print(kwargs.get("data"))
  44. # link_data =base64.b64decode(kwargs.get("data")).decode("utf-8") \
  45. link_data = data
  46. return http.request.render('website_contact_extend.contactby_form')
  47. # return "<center style='color:red'>You are at the right \
  48. # place</center>"
  49. @http.route('/contact_by_send/<string:model_name>',
  50. type='http',
  51. auth="public",
  52. methods=['POST'],
  53. website=True)
  54. def contact_by_send(self, model_name, **kwargs):
  55. global link_data
  56. # form_contact_name = ""
  57. model_record = request.env['ir.model'].sudo().search([
  58. ('model', '=', model_name),
  59. ('website_form_access', '=', True)
  60. ])
  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. partner = request.env['res.partner'].sudo().search([
  83. ('email', '=', email),
  84. ('name', '=', contact_name)
  85. ])
  86. if not partner:
  87. partner = request.env['res.partner'].sudo().create(
  88. {'name': contact_name, 'email': email})
  89. if partner:
  90. for part in partner:
  91. part.is_verified = True
  92. part.last_updated = part.write_date
  93. if email_name == "True":
  94. part.email_contact = True
  95. else:
  96. part.email_contact = False
  97. if phone_name == "True":
  98. part.phone_contact = True
  99. else:
  100. part.phone_contact = False
  101. if letter_name == "True":
  102. part.letter_contact = True
  103. else:
  104. part.letter_contact = False
  105. request.session['form_builder_model_model'] = model_record.model
  106. request.session['form_builder_model'] = model_record.name
  107. request.session['form_builder_id'] = partner[0].id
  108. return json.dumps({'id': partner[0].id})
  109. # return http.request.render('website_contact_extend.\
  110. # disp_msg_template', {'message_success': \
  111. # 'Means of contact changed!'})
  112. # return "<p style='color: green'>Means of contact changed!</p>"
  113. # else:
  114. # return http.request.render('website_contact_extend.\
  115. # disp_msg_template', {'message_failure': \
  116. # 'Could not change your means of contact. \
  117. # Please request a new link'})
  118. # return "<p style='color: red'>Could not change your means\
  119. # of contact. Please request a new link</p>"
  120. class FormReview(parent_controller.WebsiteForm):
  121. @http.route('/form_review_send/<string:model_name>',
  122. type='http',
  123. auth="public",
  124. methods=['POST'],
  125. website=True)
  126. def form_review_send(self, model_name, **kwargs):
  127. # model_name = 'crm.lead'
  128. res_partner_data = request.session['form_data_dict']
  129. model_record = request.env['ir.model'].sudo().search([
  130. ('model', '=', model_name),
  131. ('website_form_access', '=', True)
  132. ])
  133. res_part_rec = request.env['res.partner'].sudo().search([
  134. ('email', '=', res_partner_data['email_from']),
  135. ('company_name', '=', res_partner_data['partner_name'])])
  136. res_part_email = request.env['res.partner'].sudo().search([
  137. ('email', '=', res_partner_data['email_from'])], limit=1)
  138. # if len(res_part_rec) == 0 and res_part_email:
  139. # print('Company change detected!')
  140. id_record = self.insert_record(
  141. request,
  142. model_record,
  143. res_partner_data,
  144. '',
  145. ''
  146. )
  147. if len(res_part_email) > 0 and len(res_part_rec) == 0 and \
  148. res_part_email[0].is_verified is True:
  149. res_cat = request.env['res.partner.category'].search(
  150. [('name', '=', 'New')])
  151. cat_id = 0
  152. if len(res_cat) == 0:
  153. id_part_res_cat = self.insert_record(
  154. request,
  155. request.env['ir.model'].search(
  156. [('model', '=', 'res.partner.category')]),
  157. {'name': 'New', 'create_uid': request.env.uid},
  158. '',
  159. ''
  160. )
  161. cat_id = id_part_res_cat
  162. else:
  163. cat_id = res_cat[0].id
  164. res_partner_dict = {}
  165. res_partner_dict['name'] = res_partner_data['contact_name']
  166. res_partner_dict['display_name'] = res_partner_data['contact_name']
  167. res_partner_dict['email'] = res_partner_data['email_from']
  168. res_partner_dict['company_name'] = res_partner_data['partner_name']
  169. res_partner_dict['category_id'] = [[6, False, [cat_id]]]
  170. # res_partner_dict['parent_id'] = res_part_email[0].id
  171. if 'phone' in res_partner_data:
  172. res_partner_dict['phone'] = res_partner_data['phone']
  173. update_parent_partner = request.env['res.partner'].sudo().search([
  174. ('id', '=', res_part_email[0].id)])
  175. if update_parent_partner and len(update_parent_partner) > 0:
  176. update_parent_partner[0].category_id = False
  177. update_parent_partner_comp = \
  178. request.env['res.partner'].sudo().search([
  179. ('id', '=', res_part_email[0].parent_id.id)])
  180. if update_parent_partner_comp and \
  181. len(update_parent_partner_comp) > 0:
  182. update_parent_partner_comp[0].category_id = False
  183. res_partner_company_dict = res_partner_dict.copy()
  184. res_partner_company_dict.pop('email')
  185. res_partner_company_dict.pop('company_name')
  186. # res_partner_company_dict.pop('parent_id')
  187. if 'phone' in res_partner_company_dict:
  188. res_partner_company_dict.pop('phone')
  189. res_partner_company_dict['name'] = res_partner_data['partner_name']
  190. res_partner_company_dict['display_name'] = \
  191. res_partner_data['partner_name']
  192. res_partner_company_dict['customer'] = False
  193. res_partner_company_dict['is_company'] = True
  194. res_partner_company_dict['company_type'] = 'company'
  195. # res_partner_dict['parent_id'] = id_record_res_company
  196. id_record_res = self.insert_record(
  197. request,
  198. request.env['ir.model'].search(
  199. [('model', '=', 'res.partner')]),
  200. res_partner_dict,
  201. '',
  202. ''
  203. )
  204. res_partner_company_dict['child_ids'] = [
  205. [6, 'virtual_1798', [id_record_res]]]
  206. # id_record_res_company = self.insert_record(
  207. 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.\
  258. email_template_onchange_data').sudo(
  259. ).send_mail(id_record)
  260. request.session['form_builder_model_model'] = model_record.model
  261. request.session['form_builder_model'] = model_record.name
  262. request.session['form_builder_id'] = id_record
  263. return json.dumps({'id': id_record})