From 5d4308cfa201535a618a4bbf929d1a36f732519d Mon Sep 17 00:00:00 2001 From: kutyel Date: Tue, 26 Jul 2016 19:01:49 +0200 Subject: [PATCH 1/3] [IMP][web_widget_image_download] save image with the correct type --- web_widget_image_download/README.rst | 10 +++---- web_widget_image_download/__openerp__.py | 2 +- .../src/js/web_widget_image_download.js | 27 ++++++++++++++++--- .../src/xml/web_widget_image_download.xml | 7 +++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/web_widget_image_download/README.rst b/web_widget_image_download/README.rst index 8e3aff0d..f714914b 100644 --- a/web_widget_image_download/README.rst +++ b/web_widget_image_download/README.rst @@ -22,6 +22,11 @@ To use this module, you need to: :alt: Try me on Runbot :target: https://runbot.odoo-community.org/runbot/162/8.0 +Known Issues / Roadmap +====================== + +* In order to work correctly, this widget has to detect image type, the server should include this information in the `Content-Type` header. Right now, odoo is not doing so, but a fix has been `proposed `_. + Bug Tracker =========== @@ -33,11 +38,6 @@ help us smashing it by providing a detailed and welcomed feedback. Credits ======= -Images ------- - -* Odoo Community Association: `Icon `_. - Contributors ------------ diff --git a/web_widget_image_download/__openerp__.py b/web_widget_image_download/__openerp__.py index eafc71d0..ed01155b 100644 --- a/web_widget_image_download/__openerp__.py +++ b/web_widget_image_download/__openerp__.py @@ -3,7 +3,7 @@ # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). { "name": "Web Widget - Image Download", - "summary": "Allows to download the avatar for the contact", + "summary": "Allows to download any image from its widget", "version": "8.0.1.0.0", "category": "web", "website": "https://www.tecnativa.com", diff --git a/web_widget_image_download/static/src/js/web_widget_image_download.js b/web_widget_image_download/static/src/js/web_widget_image_download.js index 092a42b8..bd31db31 100644 --- a/web_widget_image_download/static/src/js/web_widget_image_download.js +++ b/web_widget_image_download/static/src/js/web_widget_image_download.js @@ -2,15 +2,36 @@ * Copyright 2016 Flavio Corpa * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */ -openerp.web_widget_image_download = function(instance) { +openerp.web_widget_image_download = function (instance) { 'use strict'; instance.web.form.web_widget_image_download = instance.web.form.FieldBinaryImage.include({ render_value() { this._super(); - // take image URI from preceding tag - this.$el.find('.oe_form_binary_file_download').attr('href', $('img[name="image"]').attr('src')); + const widget = this.$el.find('.oe_form_binary_file_download'); + + this.imgSrc = this.$el.find('img[name="image"]').attr('src'); + + $.ajax({ + type: 'HEAD', + url: this.imgSrc, + complete(xhr) { + // retrieve image type from server ("Content-Type" header) + widget.attr('download', xhr.getResponseHeader("Content-Type").replace('/', '.')); + } + }); + + // use jquery instead of `replace` with qweb (to avoid breaking inheritance) + if (this.has_custom_image()) { + this.$el.find('.oe_form_binary_file_clear').removeClass('col-md-offset-5'); + } + + widget.attr('href', this.imgSrc); + }, + has_custom_image() { + // check if the image of the widget is different from the default placeholder + return this.imgSrc && !this.imgSrc.includes('/placeholder.png'); } }); } diff --git a/web_widget_image_download/static/src/xml/web_widget_image_download.xml b/web_widget_image_download/static/src/xml/web_widget_image_download.xml index bec197a6..096d3256 100644 --- a/web_widget_image_download/static/src/xml/web_widget_image_download.xml +++ b/web_widget_image_download/static/src/xml/web_widget_image_download.xml @@ -5,10 +5,9 @@ From cb6d2c66ada55aa7729d59991a1c275045d7ab7b Mon Sep 17 00:00:00 2001 From: kutyel Date: Thu, 28 Jul 2016 18:47:29 +0200 Subject: [PATCH 2/3] follow JQuery convention + browser compatibility --- .../static/src/js/web_widget_image_download.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web_widget_image_download/static/src/js/web_widget_image_download.js b/web_widget_image_download/static/src/js/web_widget_image_download.js index bd31db31..adaf7995 100644 --- a/web_widget_image_download/static/src/js/web_widget_image_download.js +++ b/web_widget_image_download/static/src/js/web_widget_image_download.js @@ -6,10 +6,10 @@ openerp.web_widget_image_download = function (instance) { 'use strict'; instance.web.form.web_widget_image_download = instance.web.form.FieldBinaryImage.include({ - render_value() { + render_value: function () { this._super(); - const widget = this.$el.find('.oe_form_binary_file_download'); + var $widget = this.$el.find('.oe_form_binary_file_download'); this.imgSrc = this.$el.find('img[name="image"]').attr('src'); @@ -18,7 +18,7 @@ openerp.web_widget_image_download = function (instance) { url: this.imgSrc, complete(xhr) { // retrieve image type from server ("Content-Type" header) - widget.attr('download', xhr.getResponseHeader("Content-Type").replace('/', '.')); + $widget.attr('download', xhr.getResponseHeader("Content-Type").replace('/', '.')); } }); @@ -27,9 +27,9 @@ openerp.web_widget_image_download = function (instance) { this.$el.find('.oe_form_binary_file_clear').removeClass('col-md-offset-5'); } - widget.attr('href', this.imgSrc); + $widget.attr('href', this.imgSrc); }, - has_custom_image() { + has_custom_image: function () { // check if the image of the widget is different from the default placeholder return this.imgSrc && !this.imgSrc.includes('/placeholder.png'); } From d5c311b57c215eaaf2add4653c84c05ca74fdf38 Mon Sep 17 00:00:00 2001 From: kutyel Date: Fri, 29 Jul 2016 17:34:32 +0200 Subject: [PATCH 3/3] add preferences bug as known issue --- web_widget_image_download/README.rst | 1 + web_widget_image_download/__init__.py | 3 +++ .../static/src/js/web_widget_image_download.js | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/web_widget_image_download/README.rst b/web_widget_image_download/README.rst index f714914b..cc5f3782 100644 --- a/web_widget_image_download/README.rst +++ b/web_widget_image_download/README.rst @@ -26,6 +26,7 @@ Known Issues / Roadmap ====================== * In order to work correctly, this widget has to detect image type, the server should include this information in the `Content-Type` header. Right now, odoo is not doing so, but a fix has been `proposed `_. +* For some unknown reason, the widget does not work in the `Preferences` view, because odoo is not rendering the **QWeb** template. Bug Tracker =========== diff --git a/web_widget_image_download/__init__.py b/web_widget_image_download/__init__.py index e69de29b..c222227a 100644 --- a/web_widget_image_download/__init__.py +++ b/web_widget_image_download/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Flavio Corpa +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). diff --git a/web_widget_image_download/static/src/js/web_widget_image_download.js b/web_widget_image_download/static/src/js/web_widget_image_download.js index adaf7995..bf2754bd 100644 --- a/web_widget_image_download/static/src/js/web_widget_image_download.js +++ b/web_widget_image_download/static/src/js/web_widget_image_download.js @@ -16,7 +16,7 @@ openerp.web_widget_image_download = function (instance) { $.ajax({ type: 'HEAD', url: this.imgSrc, - complete(xhr) { + complete: function (xhr) { // retrieve image type from server ("Content-Type" header) $widget.attr('download', xhr.getResponseHeader("Content-Type").replace('/', '.')); }