From 44f393746c4955707ece4f2a422c622d4781e249 Mon Sep 17 00:00:00 2001 From: ITISAG-OCA Date: Wed, 4 Jul 2018 14:07:04 +0200 Subject: [PATCH] GDPR Modules for OCA V11 Code cleanup and removed use of commit() --- contact_search_form/models/contact_search.py | 56 ++++--- .../controllers/myfilter.py | 82 ++++++----- website_contact_extend/models/res_partner.py | 16 +- .../views/contact_report.xml | 138 ++++++++++++++---- 4 files changed, 188 insertions(+), 104 deletions(-) diff --git a/contact_search_form/models/contact_search.py b/contact_search_form/models/contact_search.py index 7e8666d..e7752b4 100755 --- a/contact_search_form/models/contact_search.py +++ b/contact_search_form/models/contact_search.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from odoo import api, fields, models, tools, SUPERUSER_ID, _ +from odoo import api, fields, models, _ from odoo.exceptions import UserError class SearchLine(models.Model): @@ -8,21 +8,21 @@ class SearchLine(models.Model): name = fields.Char(string="Model Name") field_list = fields.Char(string="Fields Name") - model_id = fields.Many2one('ir.model',string="Found in Model") + model_id = fields.Many2one('ir.model', string="Found in Model") record_id = fields.Integer(string="Record ID") - search_id = fields.Many2one("dpo.view",string="Search Terms") + search_id = fields.Many2one("dpo.view", string="Search Terms") record_name = fields.Char(string="Record Name", compute="_compute_record_name") @api.multi def open_record(self): return { - 'name': _('Found record'), - 'view_type': 'form', - 'view_mode': 'form', - 'res_model': self.model_id.model, - 'type': 'ir.actions.act_window', - 'res_id': self.record_id, - 'target': 'new' + 'name': _('Found record'), + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': self.model_id.model, + 'type': 'ir.actions.act_window', + 'res_id': self.record_id, + 'target': 'new' } @api.one @@ -35,23 +35,24 @@ class ItisDpoView(models.Model): _name = "dpo.view" name = fields.Char(string="Search Term") - model_ids = fields.Many2many('ir.model','dpo_view_ir_model_rel',string="Search in Model") + model_ids = fields.Many2many('ir.model', 'dpo_view_ir_model_rel', string="Search in Model") search_lines = fields.One2many('search.line', 'search_id', string='Search Result') @api.multi def search_string(self): search_line_ids = self.env['search.line'].search([('search_id', '=', self.id)]) search_line_ids.unlink() - self._cr.commit() found = False for model in self.model_ids: - table_name = model.model.replace(".","_") - query = '''select * from '''+table_name+''' where ''' - field_list = self.env['ir.model.fields'].search([('model_id.id', '=', model.id),('ttype', 'in', ['char','html','text']),('store', '=', True)]) + table_name = model.model.replace(".", "_") + query = '''select * from ''' + table_name + ''' where ''' + field_list = self.env['ir.model.fields'].search([('model_id.id', '=', model.id), + ('ttype', 'in', ['char', 'html', 'text']), + ('store', '=', True)]) for field in field_list: - query = query +table_name+'''."'''+ field.name +'''" like '%'''+self.name+'''%' or ''' + query = query + table_name + '''."''' + field.name + '''" like '%''' + self.name + '''%' or ''' query = query[:-3] - query = query+''';''' + query += ''';''' self._cr.execute(query) colnames = [desc[0] for desc in self._cr.description] @@ -68,19 +69,16 @@ class ItisDpoView(models.Model): if str(row).find(self.name) >= 0: founded_col.append(colnames[ind]) found = True - ind = ind + 1 - fields_data = self.env['ir.model.fields'].search([('name', 'in', founded_col),('model_id', '=', model.id)]) + ind += 1 + fields_data = self.env['ir.model.fields'].search([('name', 'in', founded_col), + ('model_id', '=', model.id)]) field_desc = [] for field in fields_data: field_desc.append(field.field_description) - create_id = self.env['search.line'].create({"field_list":str(field_desc),"name":model.name,"model_id":int(model.id),"search_id":int(self.id),"record_id":int(rec_id)}) + create_id = self.env['search.line'].create({"field_list": str(field_desc), + "name": model.name, + "model_id": int(model.id), + "search_id": int(self.id), + "record_id": int(rec_id)}) if not found: - raise UserError(_("No record found with "+self.name+".")) - - - # def _search_tables(self): - # user_id = self.env['res.users'].browse('email', '=', self.email) - # # field user_info could be an array containg the fields we want to display... - # partner_id = self.env['res.partner'].browse('email', '=', self.email) - # crm_id = self.env['crm.lead'].browse('email', '=', self.email) - # mm_id = self.env['mail.mass_mailing'].browse('email', '=', self.email) \ No newline at end of file + raise UserError(_("No record found with "+self.name+".")) \ No newline at end of file diff --git a/website_contact_extend/controllers/myfilter.py b/website_contact_extend/controllers/myfilter.py index 1b4e8ef..f9821d5 100755 --- a/website_contact_extend/controllers/myfilter.py +++ b/website_contact_extend/controllers/myfilter.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- -from odoo import api, fields, models, tools, _ + from odoo.addons.website_form.controllers import main as parent_controller from odoo import http from odoo.http import request -import json from odoo.exceptions import ValidationError from psycopg2 import IntegrityError import base64 +import json class VerifyController(http.Controller): @@ -20,14 +20,16 @@ class VerifyController(http.Controller): phone_name = link_data[3] letter_name = link_data[4] link_date = link_data[5] - link_date = link_date.split(" ")[0].replace("-","") + link_date = link_date.split(" ")[0].replace("-", "") import datetime today = datetime.date.today() - link = datetime.datetime.strptime(link_date,"%Y%m%d").date() + link = datetime.datetime.strptime(link_date, "%Y%m%d").date() diff = today - link if diff.days > 5 or diff.days < 0: - return "
Not valid!
The link you entered is either not valid or expired.
Please request a new link.
" - partner = request.env['res.partner'].sudo().search([('email','=',email),('name','=',contact_name)]) + return "
Not valid!
" \ + "The link you entered is either not valid or expired." \ + "
Please request a new link.
" + partner = request.env['res.partner'].sudo().search([('email', '=', email), ('name', '=', contact_name)]) if partner: for part in partner: part.is_verified = True @@ -47,18 +49,20 @@ class VerifyController(http.Controller): template = request.env.ref('website_contact_extend.confirmation_email_template').sudo().send_mail(part.id) return "
Thank You! Your email address has been verified!
" else: - return "
Not valid!
The link you entered is either not valid or expired.
Please request a new link.
" - + return "
Not valid!
" \ + "The link you entered is either not valid or expired." \ + "
Please request a new link.
" class MyFilter(parent_controller.WebsiteForm): @http.route('/website_form/', type='http', auth="public", methods=['POST'], website=True) def website_form(self, model_name, **kwargs): - model_record = request.env['ir.model'].sudo().search([('model', '=', model_name), ('website_form_access', '=', True)]) + model_record = request.env['ir.model'].sudo().search([('model', '=', model_name), + ('website_form_access', '=', True)]) if not model_record: return json.dumps(False) - need_send_email = False + # need_send_email = False try: data = self.extract_data(model_record, request.params) contact_type=False @@ -68,46 +72,52 @@ class MyFilter(parent_controller.WebsiteForm): send_mail = True index = 0 for field_name, field_value in request.params.items(): - if field_name=="contact_type": + if field_name == "contact_type": contact_type = field_value - if field_name=="send_mail" and field_value=="send_mail": - send_mail=True - if field_name=="phone_contact" and field_value=="phone_contact": - phone_contact=True - if field_name=="letter_contact" and field_value=="letter_contact": - letter_contact=True - if field_name=="email_contact" and field_value=="email_contact": - email_contact=True + if field_name == "send_mail" and field_value == "send_mail": + send_mail = True + if field_name == "phone_contact" and field_value == "phone_contact": + phone_contact = True + if field_name == "letter_contact" and field_value == "letter_contact": + letter_contact = True + if field_name == "email_contact" and field_value == "email_contact": + email_contact = True - index = index + 1 + index += 1 contact_name = data.get("record").get("contact_name") email_from = data.get("record").get("email_from") - #if data.get("record",False): - #partner_records = request.env['res.partner'].sudo().search([('name', '=', contact_name), ('email', '=', email_from)]) - #for partner_record in partner_records: - #if partner_record: - #if partner_record.is_verified: - #partner_record.contact_type = contact_type - #partner_record.phone_contact = phone_contact - #partner_record.email_contact = email_contact - #partner_record.letter_contact = letter_contact - #if not partner_record.is_verified: - # need_send_email = True + # if data.get("record",False): + # partner_records = request.env['res.partner'].sudo().search([('name', '=', contact_name), + # ('email', '=', email_from)]) + # for partner_record in partner_records: + # if partner_record: + # if partner_record.is_verified: + # partner_record.contact_type = contact_type + # partner_record.phone_contact = phone_contact + # partner_record.email_contact = email_contact + # partner_record.letter_contact = letter_contact + # if not partner_record.is_verified: + # need_send_email = True # If we encounter an issue while extracting data except ValidationError as e: # I couldn't find a cleaner way to pass data to an exception - return json.dumps({'error_fields' : e.args[0]}) + return json.dumps({'error_fields': e.args[0]}) try: id_record = self.insert_record(request, model_record, data['record'], data['custom'], data.get('meta')) if id_record: self.insert_attachment(model_record, id_record, data['attachments']) - if id_record and send_mail and model_name=="crm.lead": - crm_lead_obj = request.env['crm.lead'].sudo().search([('id','=',id_record)]) - email_data = crm_lead_obj.email_from +"####"+crm_lead_obj.contact_name+"####"+str(email_contact)+"####"+str(phone_contact)+"####"+str(letter_contact)+"####"+str(crm_lead_obj.create_date) + if id_record and send_mail and model_name == "crm.lead": + crm_lead_obj = request.env['crm.lead'].sudo().search([('id', '=', id_record)]) + email_data = crm_lead_obj.email_from + "####" +\ + crm_lead_obj.contact_name + "####" +\ + str(email_contact) + "####" +\ + str(phone_contact) + "####" +\ + str(letter_contact)+"####" +\ + str(crm_lead_obj.create_date) ency_email = base64.b64encode(email_data.encode()).decode("utf-8") - action_url = '%s/verify_email/?data=%s'%( + action_url = '%s/verify_email/?data=%s' % ( request.env['ir.config_parameter'].sudo().get_param('web.base.url'), ency_email, ) diff --git a/website_contact_extend/models/res_partner.py b/website_contact_extend/models/res_partner.py index e73dbde..ec391a7 100755 --- a/website_contact_extend/models/res_partner.py +++ b/website_contact_extend/models/res_partner.py @@ -1,17 +1,17 @@ # -*- coding: utf-8 -*- -from odoo import api, fields, models, _ +from odoo import fields, models class ResContacts(models.Model): _inherit = "res.partner" - - contact_type = fields.Selection( - [('no_contact', 'I do not want to be contacted.'), - ('email_contact', 'I only want to be contacted by Email.'), - ('phone_contact', 'I only want to be contacted by Phone.'), - ('email_phone_contact', 'You can contact me by Email or Phone.') - ], string='Contact Type', + contact_type = fields.Selection([ + ('no_contact', 'I do not want to be contacted.'), + ('email_contact', 'I only want to be contacted by Email.'), + ('phone_contact', 'I only want to be contacted by Phone.'), + ('email_phone_contact', 'You can contact me by Email or Phone.') + ], + string='Contact Type', default='email_contact', help="Which way user want to be contacted.") letter_contact = fields.Boolean("Letter Contact") diff --git a/website_contact_extend/views/contact_report.xml b/website_contact_extend/views/contact_report.xml index 29d68a0..4391fbc 100755 --- a/website_contact_extend/views/contact_report.xml +++ b/website_contact_extend/views/contact_report.xml @@ -2,13 +2,13 @@ + id="report_contact" + string="Contact Report" + model="res.partner" + report_type="qweb-pdf" + file="website_contact_extend.contact_report" + name="website_contact_extend.contact_report" + />