diff --git a/poweremail_campaign/poweremail_campaign.py b/poweremail_campaign/poweremail_campaign.py index da44f96..613e896 100644 --- a/poweremail_campaign/poweremail_campaign.py +++ b/poweremail_campaign/poweremail_campaign.py @@ -3,6 +3,8 @@ from osv import osv, fields from poweremail.poweremail_template import get_value from tools import config +from oorq.decorators import job +from tqdm import tqdm class PoweremailCampaign(osv.osv): @@ -57,58 +59,63 @@ def _ff_created_sent_object(self, cursor, uid, ids, field_name, arg, context=Non } return res - def update_linies_campanya(self, cursor, uid, ids, context=None): - if not isinstance(ids, list): - ids = [ids] + def update_linies_campanya(self, cursor, uid, campaing_id, context=None): + if not isinstance(campaing_id, list): + ids = [campaing_id] template_o = self.pool.get('poweremail.templates') pm_camp_obj = self.pool.get('poweremail.campaign') pm_camp_q = pm_camp_obj.q(cursor, uid) pm_camp_line_obj = self.pool.get('poweremail.campaign.line') pm_camp_line_q = pm_camp_line_obj.q(cursor, uid) mails_unics = set() - for camp_id in ids: - pm_camp_vs = pm_camp_q.read(['domain', 'template_id', 'distinct_mails']).where([('id', '=', camp_id)])[0] - line_vs = pm_camp_line_q.read(['id']).where([('campaign_id', '=', camp_id)]) - # Borra línies existents de la campanya - for line_v in line_vs: - pm_camp_line_obj.unlink(cursor, uid, line_v['id'], context=context) - # Recalcula linies aplicant el domain al model de dades - domain = eval(pm_camp_vs['domain']) - template_id = pm_camp_vs['template_id'] - if not template_id: - continue - template = template_o.browse(cursor, uid, template_id, context=context) - if template.model_int_name: - model = str(template.model_int_name) - model_obj = self.pool.get(model) - res_ids = model_obj.search(cursor, uid, domain, context=context) - - # Crear campaign line per cada registre trobat - from tqdm import tqdm - for record_id in tqdm(res_ids): - ref = '{},{}'.format(model, record_id) - lang = get_value( - cursor, uid, record_id, template.lang, template, + pm_camp_vs = pm_camp_q.read(['domain', 'template_id', 'distinct_mails']).where([('id', '=', campaing_id)])[0] + line_vs = pm_camp_line_q.read(['id']).where([('campaign_id', '=', campaing_id)]) + # Borra línies existents de la campanya + for line_v in line_vs: + pm_camp_line_obj.unlink(cursor, uid, line_v['id'], context=context) + # Recalcula linies aplicant el domain al model de dades + domain = eval(pm_camp_vs['domain']) + template_id = pm_camp_vs['template_id'] + template = template_o.browse(cursor, uid, template_id, context=context) + if template.model_int_name: + model = str(template.model_int_name) + model_obj = self.pool.get(model) + lines_ids = model_obj.search(cursor, uid, domain, context=context) + # Crear campaign line per cada registre trobat + for line_id in tqdm(lines_ids): + ref = '{},{}'.format(model, line_id) + # retorna l'idioma configurat a la plantilla + lang = get_value( + cursor, uid, line_id, template.lang, template, + context=context + ) + state = 'to_send' + params = { + 'campaign_id': campaing_id, + 'ref': ref, + 'state': state, + 'lang': lang != 'False' and lang or config.get('lang', 'en_US') + } + if pm_camp_vs['distinct_mails']: + #retorna correu configurat a la plantilla + email = get_value( + cursor, uid, line_id, template.def_to, template, context=context ) - state = 'to_send' - if pm_camp_vs['distinct_mails']: - email = get_value( - cursor, uid, record_id, template.def_to, template, - context=context - ) - if email in mails_unics: - state = 'avoid_duplicate' - else: - mails_unics.add(email) - - params = { - 'campaign_id': camp_id, - 'ref': ref, - 'state': state, - 'lang': lang != 'False' and lang or config.get('lang', 'en_US') - } + if email in mails_unics: + state = 'avoid_duplicate' + else: + mails_unics.add(email) pm_camp_line_obj.create(cursor, uid, params) + else: + self.create_lines_async(cursor, uid, params, context=context) + + return True + + @job(queue=config.get('poweremail_sender_queue', 'poweremail')) + def create_lines_async(self, cursor, uid, params, context=None): + pm_camp_line_obj = self.pool.get('poweremail.campaign.line') + pm_camp_line_obj.create(cursor, uid, params) return True def send_emails(self, cursor, uid, ids, context=None):