+
+
+
+ product.product.only.form.view.inherit.product.import
+ product.product
+
+
+
+
+
+
+
+
+
diff --git a/product_import/views/product_template_views.xml b/product_import/views/product_template_views.xml
new file mode 100644
index 000000000..1ec721683
--- /dev/null
+++ b/product_import/views/product_template_views.xml
@@ -0,0 +1,15 @@
+
+
+
+
+ product.template.only.form.view.inherit.product.import
+ product.template
+
+
+
+
+
+
+
+
+
diff --git a/product_import/views/product_url.xml b/product_import/views/product_url.xml
deleted file mode 100644
index eab94f692..000000000
--- a/product_import/views/product_url.xml
+++ /dev/null
@@ -1,29 +0,0 @@
-
-
-
- Product URL
- product.template
-
-
-
-
-
-
-
-
-
-
-
- Product URL
- product.product
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/product_import/wizard/product_import.py b/product_import/wizard/product_import.py
index 0b8a32b72..81d298bf2 100644
--- a/product_import/wizard/product_import.py
+++ b/product_import/wizard/product_import.py
@@ -19,27 +19,28 @@
# If not, see .
#
#############################################################################
-import tempfile
-import binascii
import base64
+import binascii
+import tempfile
import certifi
import urllib3
import xlrd
-from odoo.exceptions import Warning
+from odoo.exceptions import ValidationError
from odoo import models, fields, _
class ProductImport(models.Model):
+ """Model to import the product"""
_name = 'product.import'
+ _description = 'Product Import'
file = fields.Binary(string="Upload File")
file_name = fields.Char(string="File Name")
- option = fields.Selection([
- ('csv', 'CSV'),
- ('xlsx', 'XLSX')], default='csv')
+ option = fields.Selection([('csv', 'CSV'), ('xlsx', 'XLSX')],
+ default='csv')
def import_file(self):
- """ function to import product details from csv and xlsx file """
+ """Function to import product details from csv and xlsx file """
if self.option == 'csv':
try:
product_temp_data = self.env['product.template'].search([])
@@ -48,9 +49,8 @@ class ProductImport(models.Model):
file_string = file_string.split('\n')
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
- except:
- raise Warning(_("Please choose the correct file!"))
-
+ except Exception:
+ raise ValidationError(_("Please choose the correct file!"))
firstline = True
for file_item in file_string:
if firstline:
@@ -59,49 +59,34 @@ class ProductImport(models.Model):
product_temp = self.env['product.template'].search(
[('name', '=', file_item.split(",")[0])], limit=0)
if not product_temp.id:
- if file_item.split(",")[0]:
- if "http://" in file_item.split(",")[4] or "https://" in \
- file_item.split(",")[4]:
- link = file_item.split(",")[4]
- image_response = http.request('GET', link)
+ file_parts = file_item.split(",")
+ if len(file_parts) >= 5:
+ name, detailed_type, barcode, list_price, file_path_or_url = file_parts[:5]
+ product_name = {
+ 'name': name,
+ 'detailed_type': detailed_type,
+ 'barcode': barcode,
+ 'list_price': list_price,
+ }
+ if (file_path_or_url.startswith("http://") or
+ file_path_or_url.startswith("https://")):
+ image_response = http.request('GET',
+ file_path_or_url)
image_thumbnail = base64.b64encode(
image_response.data)
- product_name = {
- 'name': file_item.split(",")[0],
- 'detailed_type': file_item.split(",")[1],
- 'barcode': file_item.split(",")[2],
- 'list_price': file_item.split(",")[3],
- 'image_1920': image_thumbnail,
- }
- product_line = product_temp_data.create(
- product_name)
- elif '/home' in file_item.split(",")[4]:
- with open(file_item.split(",")[4], 'rb') as file:
+ product_name['image_1920'] = image_thumbnail
+ elif file_path_or_url.startswith('/home'):
+ with open(file_path_or_url, 'rb') as file:
data = base64.b64encode(file.read())
- product_name = {
- 'name': file_item.split(",")[0],
- 'detailed_type': file_item.split(",")[1],
- 'barcode': file_item.split(",")[2],
- 'list_price': file_item.split(",")[3],
- 'image_1920': data,
- }
- product_line = product_temp_data.create(
- product_name)
- else:
- product_name = {
- 'name': file_item.split(",")[0],
- 'detailed_type': file_item.split(",")[1],
- 'barcode': file_item.split(",")[2],
- 'list_price': file_item.split(",")[3],
- }
- product_line = product_temp_data.create(
- product_name)
+ product_name['image_1920'] = data
+ product_temp_data.create(product_name)
else:
- raise Warning(_("Add the product which is not available in products"))
-
- if self.option == 'xlsx':
+ raise ValidationError(
+ _("Add the product which is not available in products")
+ )
+ elif self.option == 'xlsx':
try:
- product_temp_data = self.env['product.template'].search([])
+ product_temp_data = self.env['product.template']
file_string = tempfile.NamedTemporaryFile(suffix=".xlsx")
file_string.write(binascii.a2b_base64(self.file))
book = xlrd.open_workbook(file_string.name)
@@ -109,8 +94,7 @@ class ProductImport(models.Model):
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
except:
- raise Warning(_("Please choose the correct file"))
-
+ raise ValidationError(_("Please choose the correct file"))
startline = True
for i in range(sheet.nrows):
if startline:
@@ -119,42 +103,26 @@ class ProductImport(models.Model):
line = list(sheet.row_values(i))
product_temp = self.env['product.template'].search(
[('name', '=', line[0])], limit=0)
- if not product_temp.id:
- if line[0]:
- if "http://" in line[4] or "https://" in line[4]:
- link = line[4]
- image_response = http.request('GET', link)
- image_thumbnail = base64.b64encode(
- image_response.data)
- product_name = {
- 'name': line[0],
- 'detailed_type': line[1],
- 'barcode': line[2],
- 'list_price': line[3],
- 'image_1920': image_thumbnail,
- }
- product_line = product_temp_data.create(
- product_name)
- elif "/home" in line[4]:
- with open(line[4], 'rb') as file:
- data = base64.b64encode(file.read())
- product_name = {
- 'name': line[0],
- 'detailed_type': line[1],
- 'barcode': line[2],
- 'list_price': line[3],
- 'image_1920': data,
- }
- product_line = product_temp_data.create(
- product_name)
- else:
- product_name = {
- 'name': line[0],
- 'detailed_type': line[1],
- 'barcode': line[2],
- 'list_price': line[3],
- }
- product_line = product_temp_data.create(
- product_name)
- else:
- raise Warning(_("Add the product which not available in products"))
+ if product_temp.id:
+ raise ValidationError(
+ _("Add the product which is not available in"
+ " products"))
+ if line[0]:
+ if "http://" in line[4] or "https://" in line[4]:
+ link = line[4]
+ image_response = http.request('GET', link)
+ image_thumbnail = base64.b64encode(
+ image_response.data)
+ elif "/home" in line[4]:
+ with open(line[4], 'rb') as file:
+ image_thumbnail = base64.b64encode(file.read())
+ else:
+ image_thumbnail = False # or None
+ product_name = {
+ 'name': line[0],
+ 'detailed_type': line[1],
+ 'barcode': line[2],
+ 'list_price': line[3],
+ 'image_1920': image_thumbnail,
+ }
+ product_temp_data.create(product_name)
diff --git a/product_import/wizard/product_import.xml b/product_import/wizard/product_import_views.xml
similarity index 78%
rename from product_import/wizard/product_import.xml
rename to product_import/wizard/product_import_views.xml
index b53c29a0f..5a8196618 100644
--- a/product_import/wizard/product_import.xml
+++ b/product_import/wizard/product_import_views.xml
@@ -1,8 +1,8 @@
-
-
- Import Product
+
+
+ product.import.view.form
product.import
-
+
Import Product
product.import
ir.actions.act_window
tree,form
-
+
{}
new
-
+
-