Skip to content
Extraits de code Groupes Projets

odoo integration

Fusionnées Julien - Le Filament a demandé de fusionner 12.0-alfresco vers 12.0
7 files
+ 122
60
Comparer les modifications
  • Côte à côte
  • En ligne

Fichiers

+ 60
37
@@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import base64
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from ..tools.alfresco import AlfrescoApi
@@ -54,51 +54,74 @@ class IrAttachment(models.Model):
# Default methods
# ------------------------------------------------------
@api.model
def _file_read(self, fname, bin_size=False):
_logger.debug(
f" --------------------------- READ {self.name} TYPE {self.type}"
@api.depends("store_fname", "db_datas")
def _compute_datas(self):
print(f" --------------------------- COMPUTE")
bin_size = self._context.get("bin_size")
for attach in self:
if attach.store_fname:
attach.datas = self._file_read(attach.store_fname, bin_size)
elif attach.type == "alfresco" and attach.alfresco_node_id:
attach.datas = self._file_read_alfresco(attach.alfresco_node_id)
else:
attach.datas = attach.db_datas
@api.multi
def unlink(self):
print(f" --------------------------- UNLINK")
if not self:
return True
self.check("unlink")
# First delete in the database, *then* in the filesystem if the
# database allowed it. Helps avoid errors when concurrent transactions
# are deleting the same file, and some of the transactions are
# rolled back by PostgreSQL (due to concurrent updates detection).
to_delete = set(attach.store_fname for attach in self if attach.store_fname)
to_delete_from_alfresco = set(
attach.alfresco_node_id for attach in self if attach.alfresco_node_id
)
if self.type in ["alfresco"]:
try:
alfresco_api = AlfrescoApi(user=user, password=password)
return alfresco_api.download_file(file_id=self.alfresco_node_id)
except AlfrescoApiException as e:
raise UserError(_(f"Unable to get file from alfresco : {e}"))
res = super(IrAttachment, self).unlink()
for file_path in to_delete:
self._file_delete(file_path)
for node_id in to_delete_from_alfresco:
self._file_delete_from_alfresco(node_id=node_id)
return res
return super(IrAttachment, self)._file_read(fname, bin_size=bin_size)
@api.model
def _file_read_alfresco(self, node_id):
print(f" --------------------------- READ ALFRESCO")
try:
alfresco_api = AlfrescoApi(user=user, password=password)
return base64.b64encode(alfresco_api.download_file(file_id=node_id))
except AlfrescoApiException as e:
raise UserError(_(f"Unable to get file from alfresco : {e}"))
@api.model
def _file_write(self, value, checksum):
_logger.debug(
f" --------------------------- WRITE {self.name} TYPE {self.type}"
)
if self.type in ["alfresco"]:
print(f" --------------------------- WRITE {self.name} TYPE {self.type}")
if self.type in ["alfresco"] and self.alfresco_node_id:
bin_value = base64.b64decode(value)
fname, _ = self._get_path(bin_value, checksum)
try:
alfresco_api = AlfrescoApi(user=user, password=password)
self.alfresco_node_id = alfresco_api.upload_file(
parent_folder_id=parent_node_id, file_name=self.name, data=value
parent_folder_id=parent_node_id, file_name=fname, data=value
)
return self.name
return fname
except AlfrescoApiException as e:
raise UserError(_(f"Unable to push file to alfresco : {e}"))
return super(IrAttachment, self)._file_write(value, checksum)
return super()._file_write(value, checksum)
@api.model
def _file_delete(self, fname):
_logger.debug(
f" --------------------------- DELETE {self.name} TYPE {self.type}"
)
if self.type in ["alfresco"]:
try:
alfresco_api = AlfrescoApi(user=user, password=password)
alfresco_api.delete_file(self.alfresco_node_id)
except AlfrescoApiException as e:
raise UserError(_(f"Unable to delete file in alfresco : {e}"))
else:
# simply add fname to checklist, it will be garbage-collected later
super(IrAttachment, self)._file_delete(fname)
def _file_delete_from_alfresco(self, node_id):
try:
alfresco_api = AlfrescoApi(user=user, password=password)
alfresco_api.delete_file(node_id)
except AlfrescoApiException as e:
raise UserError(_(f"Unable to delete file in alfresco : {e}"))
# ------------------------------------------------------
# Computed fields / Search Fields
@@ -113,21 +136,21 @@ class IrAttachment(models.Model):
if a file change to alfresco type move it to alfresco and delete from storage
on a contrary rewrite it on storage and delete it from alfresco
"""
_logger.debug(
f" --------------------------- CHANGE {self.name} TYPE {self.type}"
)
print(f" --------------------------- CHANGE {self.name} TYPE {self.type}")
alfresco_api = AlfrescoApi(user=user, password=password)
if self.type in ["alfresco"]:
try:
alfresco_api.upload_file(
node_id = alfresco_api.upload_file(
parent_folder_id=parent_node_id,
file_name=self.name,
data=self.datas,
data=base64.b64decode(self.datas),
description=self.descritpion,
tag="odoo_file",
)
self.alfresco_node_id = node_id
except AlfrescoApiException as e:
raise UserError(_(f"Unable to delete file in alfresco : {e}"))
super(IrAttachment, self)._file_delete(self.name)
else:
Chargement en cours