You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

87 lines
3.3 KiB

# -*- coding: utf-8 -*-
#################################################################################
# Author : Expert IT Solutions (<www.expertpk.com>)
# Copyright(c): 2012-Present Expert IT Solutions
# All Rights Reserved.
#
# This program is copyright property of the author mentioned above.
# You can`t redistribute it and/or modify it.
#
#################################################################################
from odoo import models, fields, api
from lxml import etree
import logging
_logger = logging.getLogger(__name__)
class Base(models.AbstractModel):
_inherit = 'base'
@api.model
def get_view(self, view_id=None, view_type='form', **options):
res = super().get_view(view_id, view_type, **options)
# Parse the XML architecture from the result
doc = etree.XML(res['arch'])
if view_type == 'form':
for node in doc.xpath("//field[@widget='d_and_d_images']"):
_logger.info("Found node: %s", node.get('name'))
field_name = node.get('name')
# Create a new field node with kanban mode and style display none
new_node = etree.Element('field', {
'name': field_name,
'mode': 'kanban',
'nolabel': '1',
'style': 'display:none; max-width: 1px; max-height: 1px;',
'context': "{'default_name': name}",
})
parent = node.getparent()
index = parent.index(node)
parent.insert(index - 1, new_node)
res['arch'] = etree.tostring(doc, pretty_print=True, encoding='unicode')
return res
return res
class IrAttachment(models.Model):
_inherit = "ir.attachment"
@api.model
def action_save_drag_and_drop_images(self, resModel, resId, resField, childField, fileDatas, extraData=None):
parent = self.env[resModel].browse(int(resId))
if not parent.exists():
_logger.error(f"Parent record not found: Model={resModel}, ID={resId}")
return False
records_data = []
for fileData in fileDatas:
if 'filename' in fileData and 'base64' in fileData:
record_vals = {
'name': fileData['filename'],
childField: fileData['base64'],
}
if extraData and isinstance(extraData, dict):
filtered_data = {k: v for k, v in extraData.items() if k not in ['cssStyles', 'previewImage', 'showConfirm']}
record_vals.update(filtered_data) # Merge any extra required fields
records_data.append(record_vals)
else:
_logger.warning("Missing 'filename' or 'base64' in file data")
if records_data:
try:
parent.write({
resField: [(0, 0, vals) for vals in records_data]
})
_logger.info(f"Images successfully added to {resModel} ID {resId} in field {resField}")
return True
except Exception as e:
_logger.error(f"Error updating parent record: {e}")
return False
else:
_logger.info("No valid image data provided")
return False