cnc工单处增加NC文件下载的功能已完成
This commit is contained in:
@@ -13,6 +13,11 @@ from odoo.exceptions import ValidationError
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.addons.sf_machine_connect.models import py2opcua, ftp_operate
|
||||
|
||||
import shutil
|
||||
from io import BytesIO
|
||||
from zipfile import ZipFile
|
||||
from odoo.exceptions import MissingError
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -537,21 +542,69 @@ class WorkCenterBarcode(models.Model):
|
||||
else:
|
||||
return False
|
||||
|
||||
def cnc_file_download(self):
|
||||
"""
|
||||
一个下载当前cnc所有nc文件为一个zip包的功能
|
||||
"""
|
||||
self.ensure_one()
|
||||
|
||||
class CuttingTimeToolType(models.Model):
|
||||
_inherit = 'sf.cutting_tool.type'
|
||||
total_cut_time = fields.Char(string='总切削时长')
|
||||
predict_life_time = fields.Char(string='预估寿命')
|
||||
# 只能看到未被占用的刀位,或者有提示说占没占用
|
||||
tool_position = fields.Char(string='选择刀位')
|
||||
# cnc_ids = fields.One2many(string='选择机床')
|
||||
is_connect_tool_position = fields.Boolean(string='是否已绑定刀位', default=False)
|
||||
# 如果没有附件,直接返回
|
||||
if not self.cnc_ids:
|
||||
return
|
||||
|
||||
def tool_connect_machine(self):
|
||||
pass
|
||||
# 处理第一个附件的文件名
|
||||
first_attachment = self.cnc_ids[0].cnc_id
|
||||
file_name = first_attachment.display_name.split('-')[0] + '.zip'
|
||||
|
||||
def tool_unconnect_machine(self):
|
||||
pass
|
||||
# 创建一个内存文件和一个zip文件
|
||||
memory_file = BytesIO()
|
||||
with ZipFile(memory_file, 'w') as zipf:
|
||||
# 遍历需要下载的附件
|
||||
for item in self.cnc_ids:
|
||||
a = item.cnc_id
|
||||
datas = base64.standard_b64decode(a.datas)
|
||||
|
||||
# 将nc文件写入zip文件
|
||||
zipf.writestr(a.display_name, datas)
|
||||
|
||||
# 然后可以创建一个ir.attachment对象,将生成的zip文件保存为一个新的附件
|
||||
memory_file.seek(0)
|
||||
output = base64.b64encode(memory_file.read())
|
||||
memory_file.close()
|
||||
|
||||
attachment_data = {
|
||||
'name': file_name,
|
||||
'type': 'binary',
|
||||
'res_model': self._name,
|
||||
'res_id': self.id,
|
||||
'datas': output
|
||||
}
|
||||
attachment = self.env['ir.attachment'].create(attachment_data)
|
||||
|
||||
# 返回附件的下载链接
|
||||
download_url = '/web/content/%s?download=true' % attachment.id
|
||||
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
|
||||
return {
|
||||
'type': 'ir.actions.act_url',
|
||||
'url': str(base_url) + download_url,
|
||||
'target': 'self',
|
||||
}
|
||||
|
||||
|
||||
# class CuttingTimeToolType(models.Model):
|
||||
# _inherit = 'sf.cutting_tool.type'
|
||||
# total_cut_time = fields.Char(string='总切削时长')
|
||||
# predict_life_time = fields.Char(string='预估寿命')
|
||||
# # 只能看到未被占用的刀位,或者有提示说占没占用
|
||||
# tool_position = fields.Char(string='选择刀位')
|
||||
# # cnc_ids = fields.One2many(string='选择机床')
|
||||
# is_connect_tool_position = fields.Boolean(string='是否已绑定刀位', default=False)
|
||||
#
|
||||
# def tool_connect_machine(self):
|
||||
# pass
|
||||
#
|
||||
# def tool_unconnect_machine(self):
|
||||
# pass
|
||||
|
||||
|
||||
class DeliveryRecord(models.Model):
|
||||
@@ -579,4 +632,3 @@ class DeliveryRecord(models.Model):
|
||||
# return super().create(values)
|
||||
# except:
|
||||
# raise Exception('b_purchase_order.py:create()')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user