[IMP] Improved pad module

Generate Pad urls server side instead of client side.
Use a template for pad urls.

bzr revid: fme@openerp.com-20111026210651-fnosw5xhc9ncuqfo
This commit is contained in:
Fabien Meghazi 2011-10-26 23:06:51 +02:00
parent 33c4580c03
commit 226ca06039
6 changed files with 58 additions and 62 deletions

View File

@ -1,2 +1,3 @@
# -*- coding: utf-8 -*-
import res_company
import ir_attachment

View File

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
from osv import fields, osv
import random
import string
class ir_attachment(osv.osv):
_inherit = 'ir.attachment'
def pad_generate_url(self, cr, uid, model, id):
pad_url_template = self.pool.get('res.users').browse(cr,uid,[uid])[0].company_id.pad_url_template
s = string.ascii_uppercase + string.digits
salt = ''.join([s[random.randint(0, len(s) - 1)] for i in range(8)])
template_vars = {
'db' : cr.dbname,
'model' : model,
'id' : id,
'salt' : salt,
'name' : '',
}
return pad_url_template % template_vars
def pad_get(self, cr, uid, model, id):
attachment = self.search(cr, uid, [('res_model', '=', model), ('res_id', '=', id), ('type', '=', 'url'), ('name', '=', 'Pad')])
if attachment:
return self.read(cr, uid, attachment)[0]['url']
else:
url = self.pad_generate_url(cr, uid, model, id)
self.create(cr, uid, {
'res_model' : model,
'res_id' : id,
'type' : 'url',
'name' : 'Pad',
'url' : url,
})
return url

View File

@ -4,11 +4,10 @@ from osv import fields, osv
class company_pad(osv.osv):
_inherit = 'res.company'
_columns = {
'pad_index': fields.char('Pad root URL', size=64, required=True,
help="The root URL of the company's pad "
"instance"),
'pad_url_template': fields.char('Pad URL Template', size=128, required=True,
help="Template used to generate pad URL."),
}
_defaults = {
'pad_index': 'http://ietherpad.com/'
'pad_url_template': 'http://ietherpad.com/%(db)s-%(model)s-%(id)d-%(salt)s-%(name)s'
}

View File

@ -8,7 +8,7 @@
<field name="arch" type="xml">
<xpath expr="//page[@string='Configuration']" position="inside">
<separator string="Pad" colspan="4"/>
<field name="pad_index"/>
<field name="pad_url_template"/>
</xpath>
</field>
</record>

View File

Before

Width:  |  Height:  |  Size: 410 B

After

Width:  |  Height:  |  Size: 410 B

View File

@ -1,71 +1,32 @@
openerp.pad = function(instance) {
var QWeb = instance.web.qweb;
QWeb.add_template('/pad/static/src/xml/pad.xml');
instance.web.form.SidebarAttachments = instance.web.form.SidebarAttachments.extend({
pad_prefix: undefined,
on_attachments_loaded: function(attachments) {
this._super(attachments);
var self = this;
var $padbtn = self.$element.find('button.pad');
var is_pad = function(a) {
return a.type == 'url' && _(a.url).startsWith(self.pad_prefix);
};
if (_.any(attachments, is_pad)) {
$padbtn.hide();
} else {
$padbtn.show().click(self.on_add_pad);
}
},
on_add_pad: function() {
var self = this;
var $padbtn = this.$element.find('button.pad');
$padbtn.attr('disabled', 'true').find('img, span').toggle();
this.do_load_pad_prefix(function() {
var attachment = new instance.web.DataSet(self, 'ir.attachment', self.view.dataset.get_context());
var r = (((1+Math.random())*0x10000)|0).toString(16).substring(1);
attachment.create({
res_model: self.view.dataset.model,
res_id: self.view.datarecord.id,
type: 'url',
name: 'Pad',
url: self.pad_prefix + r,
}, function() {
self.do_update();
});
this.view.dataset.call_button('pad_get', [[this.view.datarecord.id], this.view.dataset.get_context()], function(r) {
$padbtn.hide();
self.do_update();
self.do_action(r.result);
});
},
do_load_pad_prefix: function(continuation) {
var self = this;
if (this.pad_prefix === undefined) {
var user = new instance.web.DataSet(this, 'res.users', this.view.dataset.get_context());
var company = new instance.web.DataSet(this, 'res.company', this.view.dataset.get_context());
user.read_ids([this.session.uid], ['company_id'], function(result) {
company.read_ids([result[0].company_id[0]], ['pad_index'], function(result) {
var pad_index = _(result[0].pad_index).strip().replace(/\/$/, "");;
if (pad_index) {
self.pad_prefix = _('%s/%s-%s-%d-').sprintf(pad_index, self.session.db, self.view.dataset.model, self.view.datarecord.id);
} else {
self.pad_prefix = null;
}
continuation();
});
});
} else {
continuation();
}
},
on_attachments_loaded: function(attachments) {
this._super(attachments);
var self = this;
this.do_load_pad_prefix(function() {
var $padbtn = self.$element.find('button.pad');
var is_pad = function(a) {
return a.type == 'url' && _(a.url).startsWith(self.pad_prefix);
};
if (!self.pad_prefix || _.any(attachments, is_pad)) {
$padbtn.hide();
} else {
$padbtn.show().click(self.on_add_pad);
}
});
},
}
});
};