diff --git a/muk_web_utils/__manifest__.py b/muk_web_utils/__manifest__.py index 725fdf9..2ca05d3 100644 --- a/muk_web_utils/__manifest__.py +++ b/muk_web_utils/__manifest__.py @@ -20,7 +20,7 @@ { "name": "MuK Web Utils", "summary": """Utility Features""", - "version": "12.0.2.8.23", + "version": "12.0.2.9.0", "category": "Extra Tools", "license": "AGPL-3", "author": "MuK IT", @@ -31,11 +31,12 @@ ], "depends": [ "web_editor", - "muk_utils", + "muk_autovacuum", ], "data": [ "template/assets.xml", "views/res_config_settings_view.xml", + "data/autovacuum.xml", ], "qweb": [ "static/src/xml/*.xml", diff --git a/muk_web_utils/controllers/__init__.py b/muk_web_utils/controllers/__init__.py index 4631769..bc915bd 100644 --- a/muk_web_utils/controllers/__init__.py +++ b/muk_web_utils/controllers/__init__.py @@ -18,3 +18,4 @@ ################################################################################### from . import backend +from . import attachment diff --git a/muk_web_utils/controllers/attachment.py b/muk_web_utils/controllers/attachment.py new file mode 100644 index 0000000..54c9ab1 --- /dev/null +++ b/muk_web_utils/controllers/attachment.py @@ -0,0 +1,52 @@ +################################################################################### +# +# 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 . +# +################################################################################### + +import json +import base64 +import logging + +from odoo import http +from odoo.http import request +from odoo.tools.misc import str2bool + +_logger = logging.getLogger(__name__) + +class AttachmentController(http.Controller): + + @http.route('/utils/attachment/add', type='http', auth="user", methods=['POST']) + def add_attachment(self, ufile, temporary=False, **kw): + tmp = temporary and str2bool(temporary) or False + attachment = request.env['ir.attachment'].create({ + 'name': tmp and "%s (Temporary)" % name or name, + 'datas': base64.b64encode(ufile.read()), + 'datas_fname': ufile.filename, + 'type': 'binary', + 'public': False, + 'temporary': tmp, + }) + attachment.generate_access_token() + base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url') + result = attachment.read(['name', 'datas_fname', 'mimetype', 'checksum', 'access_token'])[0] + result['url'] = '%s/web/content/%s?access_token=%s' % (base_url, attachment.id, attachment.access_token) + return json.dumps(result) + + @http.route('/utils/attachment/remove', type='http', auth="user", methods=['DELETE']) + def remove_attachment(self, id, **kw): + return json.dumps(request.env['ir.attachment'].browse(id).unlink()) + \ No newline at end of file diff --git a/muk_web_utils/controllers/backend.py b/muk_web_utils/controllers/backend.py index a5da2af..3a3ae40 100644 --- a/muk_web_utils/controllers/backend.py +++ b/muk_web_utils/controllers/backend.py @@ -26,7 +26,7 @@ _logger = logging.getLogger(__name__) class BackendController(http.Controller): - @http.route('/params/muk_web_utils.binary_max_size', type='json', auth="user") + @http.route('/config/muk_web_utils.binary_max_size', type='json', auth="user") def max_upload_size(self, **kw): params = request.env['ir.config_parameter'].sudo() return { diff --git a/muk_web_utils/data/autovacuum.xml b/muk_web_utils/data/autovacuum.xml new file mode 100644 index 0000000..e11924e --- /dev/null +++ b/muk_web_utils/data/autovacuum.xml @@ -0,0 +1,31 @@ + + + + + + + + Delete temporary Attachments + + + domain + [('temporary','=',True), ('create_date', '<=', (datetime.datetime.utcnow() - datetime.timedelta(hours=24)).strftime('%Y-%m-%d %H:%M:%S'))] + + + + \ No newline at end of file diff --git a/muk_web_utils/doc/changelog.rst b/muk_web_utils/doc/changelog.rst index 1ea418c..faf2ed6 100644 --- a/muk_web_utils/doc/changelog.rst +++ b/muk_web_utils/doc/changelog.rst @@ -1,3 +1,8 @@ +`2.9.0` +------- + +- Temporary attachments + `2.8.0` ------- diff --git a/muk_web_utils/models/__init__.py b/muk_web_utils/models/__init__.py index af61df3..5d586a5 100644 --- a/muk_web_utils/models/__init__.py +++ b/muk_web_utils/models/__init__.py @@ -17,5 +17,6 @@ # ################################################################################### +from . import ir_attachment from . import res_config_settings diff --git a/muk_web_utils/models/ir_attachment.py b/muk_web_utils/models/ir_attachment.py new file mode 100644 index 0000000..74fe88c --- /dev/null +++ b/muk_web_utils/models/ir_attachment.py @@ -0,0 +1,43 @@ +################################################################################### +# +# 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 . +# +################################################################################### + +import base64 +import logging +import mimetypes + +from odoo import api, models, fields, _ +from odoo.exceptions import AccessError +from odoo.tools.mimetypes import guess_mimetype + +_logger = logging.getLogger(__name__) + +class IrAttachment(models.Model): + + _inherit = 'ir.attachment' + + #---------------------------------------------------------- + # Database + #---------------------------------------------------------- + + temporary = fields.Boolean( + string="Temporary", + default=False, + help="Attachments will be deleted by Autovacuum.", + ) + \ No newline at end of file diff --git a/muk_web_utils/static/src/js/fields/binary.js b/muk_web_utils/static/src/js/fields/binary.js index f05d85c..0b713a5 100644 --- a/muk_web_utils/static/src/js/fields/binary.js +++ b/muk_web_utils/static/src/js/fields/binary.js @@ -32,7 +32,7 @@ var QWeb = core.qweb; fields.FieldBinaryFile.include({ willStart: function () { var def = this._rpc({ - route: '/params/muk_web_utils.binary_max_size', + route: '/config/muk_web_utils.binary_max_size', }).done(function(result) { this.max_upload_size = result.max_upload_size * 1024 * 1024; }.bind(this)); diff --git a/muk_web_utils/static/src/js/fields/image.js b/muk_web_utils/static/src/js/fields/image.js index a54bf3b..9f8a43e 100644 --- a/muk_web_utils/static/src/js/fields/image.js +++ b/muk_web_utils/static/src/js/fields/image.js @@ -46,7 +46,7 @@ var QWeb = core.qweb; fields.FieldBinaryImage.include({ willStart: function () { var def = this._rpc({ - route: '/params/muk_web_utils.binary_max_size', + route: '/config/muk_web_utils.binary_max_size', }).done(function(result) { this.max_upload_size = result.max_upload_size * 1024 * 1024; }.bind(this));