Browse Source

Merge pull request #666 from Tecnativa/9.0-mig-web_widget_digitized_signature

[ADD] web_widget_digitized_signature: backport from 10.0
pull/690/head
Pedro M. Baeza 7 years ago
committed by GitHub
parent
commit
a7174d7cb8
  1. 4
      web_notify/tests/test_res_users.py
  2. 70
      web_widget_digitized_signature/README.rst
  3. 6
      web_widget_digitized_signature/__init__.py
  4. 29
      web_widget_digitized_signature/__openerp__.py
  5. 79
      web_widget_digitized_signature/i18n/es.po
  6. 7
      web_widget_digitized_signature/models/__init__.py
  7. 40
      web_widget_digitized_signature/models/mail_thread.py
  8. 27
      web_widget_digitized_signature/models/res_users.py
  9. BIN
      web_widget_digitized_signature/static/description/icon.png
  10. 1392
      web_widget_digitized_signature/static/lib/jSignature/jSignatureCustom.js
  11. 122
      web_widget_digitized_signature/static/src/js/digital_sign.js
  12. 24
      web_widget_digitized_signature/static/src/xml/digital_sign.xml
  13. 4
      web_widget_digitized_signature/tests/__init__.py
  14. 34
      web_widget_digitized_signature/tests/test_signature_tracking.py
  15. 27
      web_widget_digitized_signature/views/res_users_view.xml
  16. 11
      web_widget_digitized_signature/views/web_digital_sign_view.xml

4
web_notify/tests/test_res_users.py

@ -44,8 +44,8 @@ class TestResUsers(common.TransactionCase):
# a single call to the bus
with mock.patch('openerp.addons.bus.models.bus.ImBus.sendmany'
) as mockedSendMany:
users = self.env.user.search([(1, "=", 1)])
users = self.env.user.search([])
self.assertTrue(len(users) > 1)
users.notify_warning('message')
self.assertEqual(1, mockedSendMany.call_count)
self.assertEqual(len(users), len(mockedSendMany.call_args))
self.assertEqual(len(users), len(mockedSendMany.call_args[0][0]))

70
web_widget_digitized_signature/README.rst

@ -0,0 +1,70 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
=======================
Web Digitized Signature
=======================
This module provides a widget for binary fields that allows to digitize a
signature and store it as an image.
As demonstration, it includes this widget at user level, so that we can store
a signature image for each user.
Configuration
=============
#. To use this module, you need to add ``widget="signature"`` to your binary
field in your view.
#. You can specifify signature dimensions like the following:
``<field name="signature_image" widget="signature" width="400" height="100"/>``
Usage
=====
#. Go to *Settings > Users > Users*.
#. Open one of the existing users.
#. You can set a digital signature for it on the field "Signature".
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/162/9.0
Bug Tracker
===========
Bugs are tracked on `GitHub Issues
<https://github.com/OCA/web/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.
Credits
=======
Images
------
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
Contributors
------------
* Jay Vora <jay.vora@serpentcs.com>
* Vicent Cubells <vicent.cubells@tecnativa.com>
Maintainer
----------
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org
This module is maintained by the OCA.
OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.
To contribute to this module, please visit https://odoo-community.org.

6
web_widget_digitized_signature/__init__.py

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2004-2010 OpenERP SA (<http://www.openerp.com>)
# Copyright 2011-2015 Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import models

29
web_widget_digitized_signature/__openerp__.py

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Copyright 2004-2010 OpenERP SA (<http://www.openerp.com>)
# Copyright 2011-2015 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Web Widget Digitized Signature",
"version": "9.0.1.0.0",
"author": "Serpent Consulting Services Pvt. Ltd., "
"Agile Business Group, "
"Tecnativa, "
"Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": 'Web',
'depends': [
'web',
'mail',
],
'data': [
'views/web_digital_sign_view.xml',
'views/res_users_view.xml',
],
'website': 'http://www.serpentcs.com',
'qweb': [
'static/src/xml/digital_sign.xml',
],
'installable': True,
}

79
web_widget_digitized_signature/i18n/es.po

@ -0,0 +1,79 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * web_widget_digitized_signature
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 9.0c\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-07-11 04:42+0000\n"
"PO-Revision-Date: 2017-07-11 04:42+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
#. module: web_widget_digitized_signature
#. openerp-web
#: code:addons/web_widget_digitized_signature/static/src/xml/digital_sign.xml:7
#, python-format
msgid "Clear"
msgstr "Limpiar"
#. module: web_widget_digitized_signature
#. openerp-web
#: code:addons/web_widget_digitized_signature/static/src/js/digital_sign.js:81
#, python-format
msgid "Could not display the selected image."
msgstr "No se puede mostrar la imagen seleccionada."
#. module: web_widget_digitized_signature
#: code:addons/web_widget_digitized_signature/models/mail_thread.py:26
#, python-format
msgid "Deletion date: %s"
msgstr "Fecha de eliminación: %s"
#. module: web_widget_digitized_signature
#. openerp-web
#: code:addons/web_widget_digitized_signature/static/src/xml/digital_sign.xml:10
#, python-format
msgid "Draw your signature"
msgstr "Dibuje su firma"
#. module: web_widget_digitized_signature
#. openerp-web
#: code:addons/web_widget_digitized_signature/static/src/js/digital_sign.js:81
#, python-format
msgid "Image"
msgstr "Imagen"
#. module: web_widget_digitized_signature
#: model:ir.model.fields,field_description:web_widget_digitized_signature.field_res_users_signature_image
msgid "Signature"
msgstr "Firma"
#. module: web_widget_digitized_signature
#: code:addons/web_widget_digitized_signature/models/mail_thread.py:23
#, python-format
msgid "Signature date: %s"
msgstr "Fecha de la firma: %s"
#. module: web_widget_digitized_signature
#: code:addons/web_widget_digitized_signature/models/mail_thread.py:21
#, python-format
msgid "Signature has been created."
msgstr "La firma se ha creado."
#. module: web_widget_digitized_signature
#: code:addons/web_widget_digitized_signature/models/mail_thread.py:25
#, python-format
msgid "Signature has been deleted."
msgstr "La firma se ha eliminado."
#. module: web_widget_digitized_signature
#: model:ir.model,name:web_widget_digitized_signature.model_res_users
msgid "Users"
msgstr "Usuarios"

7
web_widget_digitized_signature/models/__init__.py

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2004-2010 OpenERP SA (<http://www.openerp.com>)
# Copyright 2011-2015 Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import mail_thread
from . import res_users

40
web_widget_digitized_signature/models/mail_thread.py

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Copyright 2004-2010 OpenERP SA (<http://www.openerp.com>)
# Copyright 2011-2015 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import base64
from openerp import _, models, fields
class MailThread(models.Model):
_inherit = "mail.thread"
def _track_signature(self, values, field):
""" This method allows to track creation and deletion of signature
field. You must call this method in order to display a message
in the chatter with information of the changes in the signature.
:param values: a dict with the values being written
:param field: name of the field that must be tracked
"""
if field in values:
attachments = []
messages = []
if values.get(field):
content = base64.b64decode(values.get(field))
attachments = [('signature', content)]
messages.append(_('Signature has been created.'))
messages.append(
_('Signature date: %s' % fields.Datetime.now()))
else:
messages.append(_('Signature has been deleted.'))
messages.append(_('Deletion date: %s' % fields.Datetime.now()))
msg_body = '<ul>'
for message in messages:
msg_body += '<li>'
msg_body += message
msg_body += '</li>'
msg_body += '</ul>'
self.message_post(body=msg_body, attachments=attachments)

27
web_widget_digitized_signature/models/res_users.py

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright 2004-2010 OpenERP SA (<http://www.openerp.com>)
# Copyright 2011-2015 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import api, fields, models
class ResUsers(models.Model):
_name = 'res.users'
_inherit = ['res.users', 'mail.thread']
signature_image = fields.Binary(
string='Signature',
)
@api.model
def create(self, vals):
res = super(ResUsers, self).create(vals)
res._track_signature(vals, 'signature')
return res
@api.multi
def write(self, vals):
self._track_signature(vals, 'signature')
return super(ResUsers, self).write(vals)

BIN
web_widget_digitized_signature/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 8.9 KiB

1392
web_widget_digitized_signature/static/lib/jSignature/jSignatureCustom.js
File diff suppressed because it is too large
View File

122
web_widget_digitized_signature/static/src/js/digital_sign.js

@ -0,0 +1,122 @@
odoo.define('web_widget_digitized_signature.web_digital_sign', function(require) {
"use strict";
var core = require('web.core');
var FormView = require('web.FormView');
var utils = require('web.utils');
var session = require('web.session');
var Model = require('web.Model');
var _t = core._t;
var QWeb = core.qweb;
var FieldSignature = core.form_widget_registry.map.image.extend({
template: 'FieldSignature',
placeholder: "/web/static/src/img/placeholder.png",
initialize_content: function() {
var self = this;
this.$el.find('> img').remove();
this.$el.find('.signature > canvas').remove();
var sign_options = {'decor-color' : '#D1D0CE', 'color': '#000', 'background-color': '#fff','height':'150','width':'550'};
this.$el.find(".signature").jSignature("init",sign_options);
this.$el.find(".signature").attr({"tabindex": "0",'height':"100"});
this.empty_sign = this.$el.find(".signature").jSignature("getData",'image');
this.$el.find('#sign_clean').click(this.on_clear_sign);
this.$el.find('.save_sign').click(this.on_save_sign);
},
on_clear_sign: function() {
var self = this;
this.$el.find(".signature > canvas").remove();
this.$el.find('> img').remove();
this.$el.find(".signature").attr("tabindex", "0");
var sign_options = {'decor-color' : '#D1D0CE', 'color': '#000', 'background-color': '#fff','height':'150','width':'550','clear': true};
this.$el.find(".signature").jSignature(sign_options);
this.$el.find(".signature").focus();
self.set('value', false);
},
on_save_sign: function(value_) {
var self = this;
this.$el.find('> img').remove();
var signature = self.$el.find(".signature").jSignature("getData",'image');
var is_empty = signature
? self.empty_sign[1] === signature[1]
: false;
if (! is_empty && typeof signature !== "undefined" && signature[1]) {
self.set('value',signature[1]);
}
},
render_value: function() {
var self = this;
var url = this.placeholder;
if (this.get('value') && !utils.is_bin_size(this.get('value'))) {
url = 'data:image/png;base64,' + this.get('value');
} else if (this.get('value')) {
url = this.session.url('/web/binary/image', {
model: this.view.dataset.model,
id: JSON.stringify(this.view.datarecord.id || null),
field: this.options.preview_image
? this.options.preview_image
: this.name,
t: new Date().getTime()
});
} else {
url = this.placeholder;
}
if (this.view.get("actual_mode") === 'view') {
var $img = $(QWeb.render("FieldBinaryImage-extend", { widget: this, url: url }));
this.$el.find('> img').remove();
this.$el.find(".signature").hide();
this.$el.prepend($img);
$img.load(function() {
if (! self.options.size) {
return;
}
$img.css("max-width", "" + self.options.size[0] + "px");
$img.css("max-height", "" + self.options.size[1] + "px");
$img.css("margin-left", "" + (self.options.size[0] - $img.width()) / 2 + "px");
$img.css("margin-top", "" + (self.options.size[1] - $img.height()) / 2 + "px");
});
$img.on('error', function() {
$img.attr('src', self.placeholder);
self.do_warn(_t("Image"), _t("Could not display the selected image."));
});
} else if (this.view.get("actual_mode") === 'edit') {
this.$el.find('> img').remove();
if (this.get('value')) {
var field_name = this.options.preview_image
? this.options.preview_image
: this.name;
new Model(this.view.dataset.model).call("read", [this.view.datarecord.id, [field_name]]).done(function(data) {
if (data) {
var field_desc = _.values(_.pick(data, field_name));
self.$el.find(".signature").jSignature("reset");
self.$el.find(".signature").jSignature("setData",'data:image/png;base64,'+field_desc[0]);
}
});
} else {
this.$el.find('> img').remove();
this.$el.find('.signature > canvas').remove();
var sign_options = {'decor-color' : '#D1D0CE', 'color': '#000','background-color': '#fff','height':'150','width':'550'};
this.$el.find(".signature").jSignature("init",sign_options);
}
} else if (this.view.get("actual_mode") === 'create') {
this.$el.find('> img').remove();
this.$el.find('> canvas').remove();
if (!this.get('value')) {
this.$el.find(".signature").empty().jSignature("init",{'decor-color' : '#D1D0CE', 'color': '#000','background-color': '#fff','height':'150','width':'550'});
}
}
}
});
core.form_widget_registry.add('signature', FieldSignature);
FormView.include({
save: function() {
this.$el.find('.save_sign').click();
return this._super.apply(this, arguments);
}
});
});

24
web_widget_digitized_signature/static/src/xml/digital_sign.xml

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<template id="template" xml:space="preserve">
<t t-name="FieldSignature">
<div class="panel panel-default mt16 mb0 " id="drawsign">
<div class="panel-heading oe_edit_only">
<div class="pull-right">
<a id="sign_clean" class="btn btn-xs oe_edit_only">Clear</a>
<a class="oe_edit_only save_sign"></a>
</div>
<strong>Draw your signature</strong>
</div>
<div class="signature panel-body"></div>
</div>
</t>
<t t-name="FieldBinaryImage-extend">
<img t-att-src='url'
t-att-border="widget.readonly ? 0 : 1"
t-att-name="widget.name"
t-att-width="widget.node.attrs.img_width || widget.node.attrs.width"
t-att-height="widget.node.attrs.img_height || widget.node.attrs.height"
t-att-tabindex="widget.node.attrs.img_tabindex || widget.node.attrs.tabindex"
/>
</t>
</template>

4
web_widget_digitized_signature/tests/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import test_signature_tracking

34
web_widget_digitized_signature/tests/test_signature_tracking.py

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp.tests import common
class TestSignatureTracking(common.SavepointCase):
@classmethod
def setUpClass(cls):
super(TestSignatureTracking, cls).setUpClass()
cls.user = cls.env.user
cls.user.lang = 'en_US'
# Simple 1x1 transparent base64 encoded GIF
cls.image = 'R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
cls.attachment_obj = cls.env['ir.attachment']
cls.message_obj = cls.env['mail.message']
def test_signature_tracking(self):
"""We have to test in a tricky way, as res.users doesn't allow a
direct chatter"""
prev_attachment_num = self.attachment_obj.search_count([])
prev_messages = self.message_obj.search([])
self.user.signature = self.image
current_attachment_num = self.attachment_obj.search_count([])
self.assertEqual(current_attachment_num - prev_attachment_num, 1)
current_messages = self.message_obj.search([])
message = current_messages - prev_messages
self.assertIn('Signature has been created.', message.body)
prev_messages = current_messages
self.user.signature = False
current_messages = self.message_obj.search([])
message = current_messages - prev_messages
self.assertIn('Signature has been deleted.', message.body)

27
web_widget_digitized_signature/views/res_users_view.xml

@ -0,0 +1,27 @@
<?xml version="1.0" ?>
<odoo>
<record id="inherited_res_users_form" model="ir.ui.view">
<field name="name">inherited.res.users.form</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='signature']" position="after">
<label for="signature_image" class="oe_edit_only"/>
<h2><field name="signature_image" widget="signature"/></h2>
</xpath>
</field>
</record>
<record id="view_users_form_simple_modif" model="ir.ui.view">
<field name="name">res.users.preferences.form</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form_simple_modif"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='signature']" position="after">
<label for="signature_image" class="oe_edit_only"/>
<h2><field name="signature_image" widget="signature"/></h2>
</xpath>
</field>
</record>
</odoo>

11
web_widget_digitized_signature/views/web_digital_sign_view.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="web_widget_digitized_signature_backend" name="web_widget_digitized_signature assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/web_widget_digitized_signature/static/lib/jSignature/jSignatureCustom.js"></script>
<script type="text/javascript" src="/web_widget_digitized_signature/static/src/js/digital_sign.js"></script>
</xpath>
</template>
</odoo>
Loading…
Cancel
Save