65 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
| from odoo import api, fields, models, _
 | |
| 
 | |
| REPLACEMENT_OF_KEY = [('id', 'template_id')]
 | |
| DATE_CONVERSION = ['date_created', 'date_edited']
 | |
| UNWANTED_DATA = ['_links', 'created_by', 'edited_by', 'thumbnail']
 | |
| 
 | |
| 
 | |
| class MailChimpTemplates(models.Model):
 | |
|     _name = "mailchimp.templates"
 | |
|     _description = "Templates"
 | |
| 
 | |
|     name = fields.Char("Name", required=True, help="The name of the template.")
 | |
|     template_id = fields.Char("Template ID", copy=False)
 | |
|     type = fields.Selection([('user', 'User'), ('gallery', 'Gallery'), ('base', 'Base')], default='user', copy=False,
 | |
|                             help="The type of template (user, base, or gallery).")
 | |
|     drag_and_drop = fields.Boolean("Drag and Drop", help="Whether the template uses the drag and drop editor.")
 | |
|     responsive = fields.Boolean("Responsive", help="Whether the template contains media queries to make it responsive.")
 | |
|     category = fields.Char("Template Category", help="If available, the category the template is listed in.")
 | |
|     date_created = fields.Datetime("Created On")
 | |
|     date_edited = fields.Datetime("Edited On")
 | |
|     active = fields.Boolean("Active", default=True)
 | |
|     folder_id = fields.Char("Folder ID", help="The id of the folder the template is currently in.")
 | |
|     share_url = fields.Char("Share URL", help="The URL used for template sharing")
 | |
|     account_id = fields.Many2one("mailchimp.accounts", string="Account", required=True, ondelete='cascade')
 | |
| 
 | |
|     @api.multi
 | |
|     def create_or_update_template(self, values_dict, account=False):
 | |
|         template_id = values_dict.get('id')
 | |
|         existing_list = self.search([('template_id', '=', template_id)])
 | |
|         for item in UNWANTED_DATA:
 | |
|             values_dict.pop(item)
 | |
|         for old_key, new_key in REPLACEMENT_OF_KEY:
 | |
|             values_dict[new_key] = values_dict.pop(old_key)
 | |
|         for item in DATE_CONVERSION:
 | |
|             if values_dict.get(item, False) == '':
 | |
|                 values_dict[item] = False
 | |
|             if values_dict.get(item, False):
 | |
|                 values_dict[item] = account.covert_date(values_dict.get(item))
 | |
|         values_dict.update({'account_id': account.id})
 | |
|         if not existing_list:
 | |
|             existing_list = self.create(values_dict)
 | |
|         else:
 | |
|             existing_list.write(values_dict)
 | |
|         return True
 | |
| 
 | |
|     @api.multi
 | |
|     def import_templates(self, account=False):
 | |
|         if not account:
 | |
|             raise Warning("MailChimp Account not defined to import templates")
 | |
|         count = 1000
 | |
|         offset = 0
 | |
|         template_list = []
 | |
|         while True:
 | |
|             prepared_vals = {'count': count, 'offset': offset}
 | |
|             response = account._send_request('templates', {}, params=prepared_vals)
 | |
|             if len(response.get('templates')) == 0:
 | |
|                 break
 | |
|             if isinstance(response.get('templates'), dict):
 | |
|                 template_list += [response.get('templates')]
 | |
|             template_list += response.get('templates')
 | |
|             offset = offset + 1000
 | |
|         for template_dict in template_list:
 | |
|             self.create_or_update_template(template_dict, account=account)
 | |
|         return True
 |