84 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
# -*- coding: utf-8 -*-
 | 
						|
# Part of BrowseInfo. See LICENSE file for full copyright and licensing details.
 | 
						|
 | 
						|
from odoo import models, fields, api, _
 | 
						|
import urllib
 | 
						|
import re
 | 
						|
#from BeautifulSoup import BeautifulSoup
 | 
						|
from bs4 import BeautifulSoup
 | 
						|
import base64
 | 
						|
 | 
						|
 | 
						|
class bi_product_image(models.Model):
 | 
						|
    _name = "bi.product.image"
 | 
						|
 | 
						|
    product_id = fields.Many2one('product.template', 'Product')
 | 
						|
    name = fields.Char('Name')
 | 
						|
    color = fields.Integer('Color Index')
 | 
						|
    image = fields.Binary('Image')
 | 
						|
 | 
						|
    @api.multi
 | 
						|
    def set_image(self):
 | 
						|
        tmpl_id_bi = self.product_id
 | 
						|
        tmpl_id_bi.image_medium = self.image
 | 
						|
        return True
 | 
						|
 | 
						|
 | 
						|
class product_template(models.Model):
 | 
						|
    _inherit = "product.template"
 | 
						|
 | 
						|
    url = fields.Char('URL')
 | 
						|
    product_images_bi = fields.One2many('bi.product.image', 'product_id', 'Multiple Image For Product')
 | 
						|
 | 
						|
    @api.multi
 | 
						|
    def write(self, vals):
 | 
						|
        super(product_template, self).write(vals)
 | 
						|
        if vals.get('url'):
 | 
						|
            img = self.process_url(vals.get('url'), self.id)
 | 
						|
        return True
 | 
						|
 | 
						|
    @api.multi
 | 
						|
    def pre_process_url(self, raw_url):
 | 
						|
     if ' ' not in raw_url[-1]:
 | 
						|
        raw_url = raw_url.replace(' ', '%20')
 | 
						|
        return raw_url
 | 
						|
     elif ' ' in raw_url[-1]:
 | 
						|
        raw_url = raw_url[:-1]
 | 
						|
        raw_url = raw_url.replace(' ', '%20')
 | 
						|
        return raw_url
 | 
						|
 | 
						|
    @api.multi
 | 
						|
    def process_url(self, url, prod_temp_id_bi):
 | 
						|
        html_data = urllib.request.urlopen(url)
 | 
						|
        soup = BeautifulSoup(html_data)
 | 
						|
        images = []
 | 
						|
        for img in soup.findAll('img'):
 | 
						|
            images.append(img.get('src'))
 | 
						|
        if not images:
 | 
						|
            imgdata = base64.encodestring(urllib.request.urlopen(url).read())
 | 
						|
            file_name = url.split('/')[-1]
 | 
						|
            prod = self.env['bi.product.image'].create({
 | 
						|
                                          'product_id':prod_temp_id_bi,
 | 
						|
                                          'name':file_name,
 | 
						|
                                          'image':imgdata
 | 
						|
                                          })
 | 
						|
        for imgurl in images:
 | 
						|
            try:
 | 
						|
                imgurl = self.pre_process_url(imgurl)
 | 
						|
                imgdata = base64.encodestring(urllib2.request.urlopen(imgurl).read())
 | 
						|
                file_name = imgurl.split('/')[-1]
 | 
						|
                prod = self.env['bi.product.image'].create({
 | 
						|
                                              'product_id':prod_temp_id_bi,
 | 
						|
                                              'name':file_name,
 | 
						|
                                              'image':imgdata
 | 
						|
                                              })
 | 
						|
            except:
 | 
						|
                pass
 | 
						|
 | 
						|
    @api.model
 | 
						|
    def create(self, vals):
 | 
						|
        res = super(product_template, self).create(vals)
 | 
						|
        if res.url:
 | 
						|
            img = self.process_url(vals.get('url'), res.id)
 | 
						|
        return res
 |