mirror of https://github.com/muk-it/muk_base
MuK IT GmbH
6 years ago
6 changed files with 206 additions and 2 deletions
-
2muk_utils/__init__.py
-
5muk_utils/__manifest__.py
-
22muk_utils/models/__init__.py
-
62muk_utils/models/ir_attachment.py
-
64muk_utils/models/res_config_settings.py
-
53muk_utils/views/res_config_settings_view.xml
@ -0,0 +1,22 @@ |
|||||
|
################################################################################### |
||||
|
# |
||||
|
# Copyright (C) 2018 MuK IT GmbH |
||||
|
# |
||||
|
# This program is free software: you can redistribute it and/or modify |
||||
|
# it under the terms of the GNU Affero General Public License as |
||||
|
# published by the Free Software Foundation, either version 3 of the |
||||
|
# License, or (at your option) any later version. |
||||
|
# |
||||
|
# This program is distributed in the hope that it will be useful, |
||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
# GNU Affero General Public License for more details. |
||||
|
# |
||||
|
# You should have received a copy of the GNU Affero General Public License |
||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
################################################################################### |
||||
|
|
||||
|
from . import ir_attachment |
||||
|
from . import res_config_settings |
||||
|
|
@ -0,0 +1,62 @@ |
|||||
|
################################################################################### |
||||
|
# |
||||
|
# Copyright (C) 2018 MuK IT GmbH |
||||
|
# |
||||
|
# This program is free software: you can redistribute it and/or modify |
||||
|
# it under the terms of the GNU Affero General Public License as |
||||
|
# published by the Free Software Foundation, either version 3 of the |
||||
|
# License, or (at your option) any later version. |
||||
|
# |
||||
|
# This program is distributed in the hope that it will be useful, |
||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
# GNU Affero General Public License for more details. |
||||
|
# |
||||
|
# You should have received a copy of the GNU Affero General Public License |
||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
################################################################################### |
||||
|
|
||||
|
import base64 |
||||
|
import logging |
||||
|
import mimetypes |
||||
|
|
||||
|
from odoo import api, models, _ |
||||
|
from odoo.exceptions import AccessError |
||||
|
|
||||
|
_logger = logging.getLogger(__name__) |
||||
|
|
||||
|
class Attachment(models.Model): |
||||
|
|
||||
|
_inherit = 'ir.attachment' |
||||
|
|
||||
|
#---------------------------------------------------------- |
||||
|
# Functions |
||||
|
#---------------------------------------------------------- |
||||
|
|
||||
|
@api.model |
||||
|
def storage_locations(self): |
||||
|
return ['db', 'file'] |
||||
|
|
||||
|
@api.model |
||||
|
def force_storage(self): |
||||
|
if not self.env.user._is_admin(): |
||||
|
raise AccessError(_('Only administrators can execute this action.')) |
||||
|
storage_domain = { |
||||
|
'db': ('db_datas', '=', False), |
||||
|
'file': ('store_fname', '=', False), |
||||
|
} |
||||
|
record_domain = [ |
||||
|
'&', storage_domain[self._storage()], |
||||
|
'|', ('res_field', '=', False), ('res_field', '!=', False) |
||||
|
] |
||||
|
self.search(record_domain).migrate() |
||||
|
return True |
||||
|
|
||||
|
@api.multi |
||||
|
def migrate(self): |
||||
|
record_count = len(self) |
||||
|
storage = self._storage().upper() |
||||
|
for index, attach in enumerate(self): |
||||
|
_logger.info(_("Migrate Attachment %s of %s to %s") % (index + 1, record_count, storage)) |
||||
|
attach.write({'datas': attach.datas}) |
@ -0,0 +1,64 @@ |
|||||
|
################################################################################### |
||||
|
# |
||||
|
# Copyright (C) 2017 MuK IT GmbH |
||||
|
# |
||||
|
# This program is free software: you can redistribute it and/or modify |
||||
|
# it under the terms of the GNU Affero General Public License as |
||||
|
# published by the Free Software Foundation, either version 3 of the |
||||
|
# License, or (at your option) any later version. |
||||
|
# |
||||
|
# This program is distributed in the hope that it will be useful, |
||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
# GNU Affero General Public License for more details. |
||||
|
# |
||||
|
# You should have received a copy of the GNU Affero General Public License |
||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
################################################################################### |
||||
|
|
||||
|
from odoo import api, fields, models |
||||
|
|
||||
|
class ResConfigSettings(models.TransientModel): |
||||
|
|
||||
|
_inherit = 'res.config.settings' |
||||
|
|
||||
|
#---------------------------------------------------------- |
||||
|
# Selections |
||||
|
#---------------------------------------------------------- |
||||
|
|
||||
|
def _attachment_location_selection(self): |
||||
|
locations = self.env['ir.attachment'].storage_locations() |
||||
|
return list(map(lambda location: (location, location.upper()), locations)) |
||||
|
|
||||
|
#---------------------------------------------------------- |
||||
|
# Database |
||||
|
#---------------------------------------------------------- |
||||
|
|
||||
|
attachment_location = fields.Selection( |
||||
|
selection=_attachment_location_selection, |
||||
|
string='Storage Location', |
||||
|
required=True, |
||||
|
help="Attachment storage location.") |
||||
|
|
||||
|
#---------------------------------------------------------- |
||||
|
# Functions |
||||
|
#---------------------------------------------------------- |
||||
|
|
||||
|
@api.multi |
||||
|
def set_values(self): |
||||
|
res = super(ResConfigSettings, self).set_values() |
||||
|
param = self.env['ir.config_parameter'].sudo() |
||||
|
param.set_param('ir_attachment.location', self.attachment_location) |
||||
|
return res |
||||
|
|
||||
|
@api.model |
||||
|
def get_values(self): |
||||
|
res = super(ResConfigSettings, self).get_values() |
||||
|
params = self.env['ir.config_parameter'].sudo() |
||||
|
res.update(attachment_location=params.get_param('ir_attachment.location', 'file')) |
||||
|
return res |
||||
|
|
||||
|
@api.multi |
||||
|
def attachment_force_storage(self): |
||||
|
self.env['ir.attachment'].force_storage() |
@ -0,0 +1,53 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
|
||||
|
<!-- |
||||
|
Copyright (C) 2017 MuK IT GmbH |
||||
|
|
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU Affero General Public License as |
||||
|
published by the Free Software Foundation, either version 3 of the |
||||
|
License, or (at your option) any later version. |
||||
|
|
||||
|
This program is distributed in the hope that it will be useful, |
||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
GNU Affero General Public License for more details. |
||||
|
|
||||
|
You should have received a copy of the GNU Affero General Public License |
||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
--> |
||||
|
|
||||
|
<odoo> |
||||
|
|
||||
|
<record id="res_config_settings_view_form" model="ir.ui.view"> |
||||
|
<field name="name">res.config.settings.view.form</field> |
||||
|
<field name="model">res.config.settings</field> |
||||
|
<field name="inherit_id" ref="base_setup.res_config_settings_view_form"/> |
||||
|
<field name="arch" type="xml"> |
||||
|
<div name="integration" position="after"> |
||||
|
<h2>Storage</h2> |
||||
|
<div class="row mt16 o_settings_container" name="web_client"> |
||||
|
<div class="col-12 col-lg-6 o_setting_box"> |
||||
|
<div class="o_setting_left_pane"></div> |
||||
|
<div class="o_setting_right_pane"> |
||||
|
<label for="attachment_location"/> |
||||
|
<div class="text-muted"> |
||||
|
Attachment storage location. |
||||
|
</div> |
||||
|
<div class="mt8"> |
||||
|
<field name="attachment_location" |
||||
|
class="o_light_label"/> |
||||
|
</div> |
||||
|
<div class="mt8"> |
||||
|
<button name="attachment_force_storage" |
||||
|
string="Force Storage Migration" |
||||
|
type="object" icon="fa-refresh"/> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
</odoo> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue