94 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
| # -*- coding: utf-8 -*-
 | |
| import json
 | |
| import time
 | |
| import requests
 | |
| from email.utils import formataddr
 | |
| from odoo import api, fields, models, _
 | |
| from odoo.tools.safe_eval import safe_eval
 | |
| from odoo.exceptions import ValidationError, Warning
 | |
| 
 | |
| 
 | |
| class MailChimpAccounts(models.Model):
 | |
|     _name = "mailchimp.accounts"
 | |
| 
 | |
|     name = fields.Char("Name", required=True, copy=False, help="Name of your MailChimp account")
 | |
| 
 | |
|     # Authentication
 | |
|     api_key = fields.Char('API Key', required=True, copy=False)
 | |
|     auto_refresh_member = fields.Boolean("Auto Sync Member?", copy=False, default=True)
 | |
|     auto_create_member = fields.Boolean("Auto Create Member?", copy=False, default=True)
 | |
|     auto_create_partner = fields.Boolean("Auto Create Customer?", copy=False, default=False)
 | |
|     list_ids = fields.One2many('mailchimp.lists', 'account_id', string="Lists/Audience", copy=False)
 | |
|     campaign_ids = fields.One2many('mail.mass_mailing', 'mailchimp_account_id', string="Campaigns", copy=False)
 | |
| 
 | |
|     _sql_constraints = [
 | |
|         ('api_keys_uniq', 'unique(api_key)', 'API keys must be unique per MailChimp Account!'),
 | |
|     ]
 | |
| 
 | |
|     @api.model
 | |
|     def _send_request(self, request_url, request_data, params=False, method='GET'):
 | |
|         if '-' not in self.api_key:
 | |
|             raise ValidationError(_("MailChimp API key is invalid!"))
 | |
|         if len(self.api_key.split('-')) > 2:
 | |
|             raise ValidationError(_("MailChimp API key is invalid!"))
 | |
| 
 | |
|         api_key, dc = self.api_key.split('-')
 | |
|         headers = {
 | |
|             'Content-Type': 'application/json'
 | |
|         }
 | |
|         data = json.dumps(request_data)
 | |
|         api_url = "https://{dc}.api.mailchimp.com/3.0/{url}".format(dc=dc, url=request_url)
 | |
|         try:
 | |
|             req = requests.request(method, api_url, auth=('apikey', api_key), headers=headers, params=params, data=data)
 | |
|             req.raise_for_status()
 | |
|             response_text = req.text
 | |
|         except requests.HTTPError as e:
 | |
|             raise Warning("%s" % req.text)
 | |
|         response = json.loads(response_text) if response_text else {}
 | |
|         return response
 | |
| 
 | |
|     @api.multi
 | |
|     def get_refresh_member_action(self):
 | |
|         action = self.env.ref('base.ir_cron_act').read()[0]
 | |
|         refresh_member_cron = self.env.ref('mailchimp.fetch_member')
 | |
|         if refresh_member_cron:
 | |
|             action['views'] = [(False, 'form')]
 | |
|             action['res_id'] = refresh_member_cron.id
 | |
|         else:
 | |
|             raise ValidationError(_("Scheduled action isn't found! Please upgrade app to get it back!"))
 | |
|         return action
 | |
| 
 | |
|     def covert_date(self, value):
 | |
|         before_date = value[:19]
 | |
|         coverted_date = time.strptime(before_date, "%Y-%m-%dT%H:%M:%S")
 | |
|         final_date = time.strftime("%Y-%m-%d %H:%M:%S", coverted_date)
 | |
|         return final_date
 | |
| 
 | |
|     @api.multi
 | |
|     def import_lists(self):
 | |
|         mailchimp_lists = self.env['mailchimp.lists']
 | |
|         for account in self:
 | |
|             mailchimp_lists.import_lists(account)
 | |
|         return True
 | |
| 
 | |
|     @api.multi
 | |
|     def import_templates(self):
 | |
|         mailchimp_templates = self.env['mailchimp.templates']
 | |
|         for account in self:
 | |
|             mailchimp_templates.import_templates(account)
 | |
|         return True
 | |
| 
 | |
|     @api.multi
 | |
|     def import_campaigns(self):
 | |
|         mass_mailing_obj = self.env['mail.mass_mailing']
 | |
|         for account in self:
 | |
|             mass_mailing_obj.import_campaigns(account)
 | |
|         return True
 | |
| 
 | |
|     @api.one
 | |
|     def test_connection(self):
 | |
|         response = self._send_request('lists', {})
 | |
|         if response:
 | |
|             raise Warning("Test Connection Succeeded")
 | |
|         return True
 |