[IMP] implement onchange method of exoprt type field and change the tree structure of export fields.
bzr revid: ysa@tinyerp.com-20110719123209-kvuqo2s4y57zx3br
This commit is contained in:
@@ -187,7 +187,7 @@ openerp.base.ViewManager = openerp.base.Controller.extend({
|
||||
if(this.active_view == 'list' || this.active_view == 'form') {
|
||||
this.views[this.active_view].controller.$element.after(QWeb.render('ExportView'))
|
||||
this.$element.find('#exportview').click(function(ev) {
|
||||
var export_view = new openerp.base_export.Export(self, self.dataset);
|
||||
var export_view = new openerp.base_export.Export(self, self.dataset, self.views);
|
||||
export_view.start(false);
|
||||
ev.preventDefault();
|
||||
});
|
||||
|
||||
@@ -2,6 +2,47 @@ from base.controllers.main import View
|
||||
import openerpweb
|
||||
import StringIO
|
||||
import csv
|
||||
import xml.dom.minidom
|
||||
|
||||
def node_attributes(node):
|
||||
attrs = node.attributes
|
||||
|
||||
if not attrs:
|
||||
return {}
|
||||
# localName can be a unicode string, we're using attribute names as
|
||||
# **kwargs keys and python-level kwargs don't take unicode keys kindly
|
||||
# (they blow up) so we need to ensure all keys are ``str``
|
||||
return dict([(str(attrs.item(i).localName), attrs.item(i).nodeValue)
|
||||
for i in range(attrs.length)])
|
||||
|
||||
def _fields_get_all(req, model, views, context=None):
|
||||
|
||||
if context is None:
|
||||
context = {}
|
||||
|
||||
def parse(root, fields):
|
||||
for node in root.childNodes:
|
||||
if node.nodeName in ('form', 'notebook', 'page', 'group', 'tree', 'hpaned', 'vpaned'):
|
||||
parse(node, fields)
|
||||
elif node.nodeName=='field':
|
||||
attrs = node_attributes(node)
|
||||
name = attrs['name']
|
||||
fields[name].update(attrs)
|
||||
return fields
|
||||
|
||||
def get_view_fields(view):
|
||||
return parse(
|
||||
xml.dom.minidom.parseString(view['arch'].encode('utf-8')).documentElement,
|
||||
view['fields'])
|
||||
|
||||
model_obj = req.session.model(model)
|
||||
tree_view = model_obj.fields_view_get(views.get('tree', False), 'tree', context)
|
||||
form_view = model_obj.fields_view_get(views.get('form', False), 'form', context)
|
||||
fields = {}
|
||||
fields.update(get_view_fields(tree_view))
|
||||
fields.update(get_view_fields(form_view))
|
||||
return fields
|
||||
|
||||
|
||||
class Export(View):
|
||||
_cp_path = "/base_export/export"
|
||||
@@ -9,42 +50,60 @@ class Export(View):
|
||||
def fields_get(self, req, model):
|
||||
Model = req.session.model(model)
|
||||
fields = Model.fields_get(False, req.session.eval_context(req.context))
|
||||
fields.update({'id': {'string': 'ID'}, '.id': {'string': 'Database ID'}})
|
||||
return fields
|
||||
|
||||
@openerpweb.jsonrequest
|
||||
def get_fields(self, req, model, prefix='', field_parent=None, name= ''):
|
||||
fields = self.fields_get(req, model)
|
||||
def get_fields(self, req, model, prefix='', name= '', field_parent=None, import_compat=False, views_id={}):
|
||||
fields = _fields_get_all(req, model, views=views_id, context=req.session.eval_context(req.context))
|
||||
|
||||
if field_parent and import_compat:
|
||||
fields = {}
|
||||
fields.update({'id': {'string': 'ID'}, '.id': {'string': 'Database ID'}})
|
||||
records = []
|
||||
for key, value in fields.items():
|
||||
fields_order = fields.keys()
|
||||
fields_order.sort(lambda x,y: -cmp(fields[x].get('string', ''), fields[y].get('string', '')))
|
||||
for index, field in enumerate(fields_order):
|
||||
value = fields[field]
|
||||
record = {}
|
||||
if import_compat and value.get('readonly', False):
|
||||
ok = False
|
||||
for sl in value.get('states', {}).values():
|
||||
for s in sl:
|
||||
ok = ok or (s==['readonly',False])
|
||||
if not ok: continue
|
||||
|
||||
id = prefix + (prefix and '/'or '') + key
|
||||
id = prefix + (prefix and '/'or '') + field
|
||||
nm = name + (name and '/' or '') + value['string']
|
||||
|
||||
record.update(id=id, string= nm, action='javascript: void(0)',
|
||||
target=None, icon=None, children=[])
|
||||
records.append(record)
|
||||
|
||||
if value.get('relation', False):
|
||||
ref = value.pop('relation')
|
||||
cfields = self.fields_get(req, ref)
|
||||
if (value['type'] == 'many2many'):
|
||||
record['children'] = []
|
||||
record['params'] = {'model': ref, 'prefix': id, 'name': nm}
|
||||
if len(nm.split('/')) < 3 and value.get('relation', False):
|
||||
if import_compat:
|
||||
ref = value.pop('relation')
|
||||
cfields = self.fields_get(req, ref)
|
||||
if (value['type'] == 'many2many'):
|
||||
record['children'] = []
|
||||
record['params'] = {'model': ref, 'prefix': id, 'name': nm}
|
||||
|
||||
elif (value['type'] == 'many2one') or (value['type'] == 'many2many'):
|
||||
cfields_order = cfields.keys()
|
||||
cfields_order.sort(lambda x,y: -cmp(cfields[x].get('string', ''), cfields[y].get('string', '')))
|
||||
children = []
|
||||
for j, fld in enumerate(cfields_order):
|
||||
cid = id + '/' + fld
|
||||
cid = cid.replace(' ', '_')
|
||||
children.append(cid)
|
||||
record['children'] = children or []
|
||||
record['params'] = {'model': ref, 'prefix': id, 'name': nm}
|
||||
elif value['type'] == 'many2one':
|
||||
record['children'] = [id + '/id', id + '/.id']
|
||||
record['params'] = {'model': ref, 'prefix': id, 'name': nm}
|
||||
|
||||
else:
|
||||
cfields_order = cfields.keys()
|
||||
cfields_order.sort(lambda x,y: -cmp(cfields[x].get('string', ''), cfields[y].get('string', '')))
|
||||
children = []
|
||||
for j, fld in enumerate(cfields_order):
|
||||
cid = id + '/' + fld
|
||||
cid = cid.replace(' ', '_')
|
||||
children.append(cid)
|
||||
record['children'] = children or []
|
||||
record['params'] = {'model': ref, 'prefix': id, 'name': nm}
|
||||
else:
|
||||
ref = value.pop('relation')
|
||||
cfields = self.fields_get(req, ref)
|
||||
cfields_order = cfields.keys()
|
||||
cfields_order.sort(lambda x,y: -cmp(cfields[x].get('string', ''), cfields[y].get('string', '')))
|
||||
children = []
|
||||
@@ -102,6 +161,7 @@ class Export(View):
|
||||
|
||||
fields = dict(f1)
|
||||
fields.update(f2)
|
||||
fields.update({'id': {'string': 'ID'}, '.id': {'string': 'Database ID'}})
|
||||
|
||||
def rec(fields):
|
||||
_fields = {'id': 'ID' , '.id': 'Database ID' }
|
||||
@@ -117,6 +177,7 @@ class Export(View):
|
||||
_fields[prefix_node+field] = st_name
|
||||
if fields[field].get('relation', False) and level>0:
|
||||
fields2 = self.fields_get(req, fields[field]['relation'])
|
||||
fields2.update({'id': {'string': 'ID'}, '.id': {'string': 'Database ID'}})
|
||||
model_populate(fields2, prefix_node+field+'/', None, st_name+'/', level-1)
|
||||
model_populate(fields)
|
||||
return _fields
|
||||
|
||||
@@ -3,10 +3,15 @@ QWeb.add_template('/base_export/static/src/xml/base_export.xml');
|
||||
openerp.base.views.add('export', 'openerp.base_export.Export');
|
||||
openerp.base_export.Export = openerp.base.Dialog.extend({
|
||||
|
||||
init: function(parent, dataset){
|
||||
init: function(parent, dataset, views){
|
||||
this._super(parent);
|
||||
this.dataset = dataset
|
||||
this.views = views
|
||||
this.selected_fields = {};
|
||||
this.views_id = {};
|
||||
for (var key in this.views) {
|
||||
this.views_id[key] = this.views[key].view_id
|
||||
}
|
||||
},
|
||||
|
||||
start: function() {
|
||||
@@ -44,8 +49,16 @@ openerp.base_export.Export = openerp.base.Dialog.extend({
|
||||
$('#export_new_list').click(function(){
|
||||
self.on_show_save_list();
|
||||
});
|
||||
this.rpc("/base_export/export/get_fields", {"model": this.dataset.model}, this.on_show_data);
|
||||
|
||||
import_comp = $("#import_compat option:selected").val()
|
||||
this.rpc("/base_export/export/get_fields", {"model": this.dataset.model,"import_compat":parseInt(import_comp), "views_id": this.views_id}, this.on_show_data);
|
||||
$("#import_compat").change(function(){
|
||||
$("#fields_list option").remove();
|
||||
$("tr[id^='treerow-']").remove();
|
||||
import_comp = $("#import_compat option:selected").val();
|
||||
if(import_comp){
|
||||
self.rpc("/base_export/export/get_fields", {"model": self.dataset.model,"import_compat":parseInt(import_comp), "views_id": this.views_id}, self.on_show_data);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
on_show_exists_export_list: function(){
|
||||
@@ -157,7 +170,8 @@ openerp.base_export.Export = openerp.base.Dialog.extend({
|
||||
if (is_loaded == 0) {
|
||||
if ($("tr[id='treerow-" + self.field_id +"']").find('img').attr('src') == '/base/static/src/img/expand.gif') {
|
||||
if (model){
|
||||
self.rpc("/base_export/export/get_fields", {"model": model, "prefix": prefix, "field_parent" : self.field_id, "name": name}, function (results) {
|
||||
import_comp = $("#import_compat option:selected").val()
|
||||
self.rpc("/base_export/export/get_fields", {"model": model, "prefix": prefix, "name": name, "field_parent" : self.field_id, "import_compat":parseInt(import_comp), "views_id": this.views_id}, function (results) {
|
||||
self.on_show_data(results);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user