send data to server for downloading grid report data if size exceeds abt 200 kb

This commit is contained in:
Anand Doshi 2013-01-25 20:04:07 +05:30
parent 2b10152a21
commit 1c17d4f69f
3 changed files with 74 additions and 47 deletions

View file

@ -1,13 +1,11 @@
from __future__ import unicode_literals
import csv, cStringIO
import webnotes
import webnotes
import webnotes.model.doc
import webnotes.model.doctype
from webnotes.model.doc import Document
from webnotes.utils import encode
from webnotes.utils import cstr
from webnotes.utils.datautils import UnicodeWriter
data_keys = webnotes._dict({
"data_separator": '----Start entering data below this line----',
@ -302,17 +300,3 @@ def import_doc(d, doctype, overwrite, row_idx):
dl.save()
return 'Inserted row (#%d) %s' % (row_idx, getlink(doctype,
dl.doc.fields['name']))
class UnicodeWriter:
def __init__(self, encoding="utf-8"):
self.encoding = encoding
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, quoting=csv.QUOTE_NONNUMERIC)
def writerow(self, row):
row = encode(row, self.encoding)
self.writer.writerow(row)
def getvalue(self):
return self.queue.getvalue()

View file

@ -6,34 +6,43 @@ wn.tools.downloadify = function(data, roles, me) {
+ " Role to export.");
return;
}
wn.require("lib/js/lib/downloadify/downloadify.min.js");
wn.require("lib/js/lib/downloadify/swfobject.js");
var id = wn.dom.set_unique_id();
var msgobj = msgprint('<p id="'+ id +'">You must have Flash 10 installed to \
download this file.</p>');
var _get_data = function(){
return wn.tools.to_csv(data);
};
Downloadify.create(id ,{
filename: function(){
return me.title + '.csv';
},
data: function(){
return wn.tools.to_csv(data);
},
swf: 'lib/js/lib/downloadify/downloadify.swf',
downloadImage: 'lib/js/lib/downloadify/download.png',
onComplete: function(){
msgobj.hide();
msgprint("Saved.");
},
onCancel: function(){ msgobj.hide(); },
onError: function(){ msgobj.hide(); },
width: 100,
height: 30,
transparent: true,
append: false
});
}
// save file > abt 200 kb using server call
if(_get_data().length > 200000) {
open_url_post("server.py?cmd=webnotes.utils.datautils.send_csv_to_client",
{args: {data: data, filename: me.title}}, true);
} else {
wn.require("lib/js/lib/downloadify/downloadify.min.js");
wn.require("lib/js/lib/downloadify/swfobject.js");
var id = wn.dom.set_unique_id();
var msgobj = msgprint('<p id="'+ id +'">You must have Flash 10 installed to \
download this file.</p>');
Downloadify.create(id ,{
filename: function(){
return me.title + '.csv';
},
data: _get_data,
swf: 'lib/js/lib/downloadify/downloadify.swf',
downloadImage: 'lib/js/lib/downloadify/download.png',
onComplete: function(){
msgobj.hide();
msgprint("Saved.");
},
onCancel: function(){ msgobj.hide(); },
onError: function(){ msgobj.hide(); },
width: 100,
height: 30,
transparent: true,
append: false
});
}
};
wn.tools.to_csv = function(data) {
var res = [];
@ -44,7 +53,7 @@ wn.tools.to_csv = function(data) {
res.push(row.join(","));
});
return res.join("\n");
}
};
wn.slickgrid_tools = {
get_view_data: function(columns, dataView, filter) {
@ -87,11 +96,11 @@ wn.slickgrid_tools = {
"__islocal": 1
}]
}
})
});
col.previousWidth = col.width;
col.docfield.width = col.width;
}
});
});
}
}
};

View file

@ -22,6 +22,9 @@
from __future__ import unicode_literals
import webnotes
import json
import csv, cStringIO
from webnotes.utils import encode, cstr
def read_csv_content_from_uploaded_file():
from webnotes.utils.file_manager import get_uploaded_content
@ -70,4 +73,35 @@ def read_csv_content(fcontent, ignore_encoding=False):
return rows
except Exception, e:
webnotes.msgprint("Not a valid Comma Separated Value (CSV File)")
raise e
raise e
@webnotes.whitelist()
def send_csv_to_client(args):
if isinstance(args, basestring):
args = json.loads(args)
args = webnotes._dict(args)
webnotes.response["result"] = cstr(to_csv(args.data))
webnotes.response["doctype"] = args.filename
webnotes.response["type"] = "csv"
def to_csv(data):
writer = UnicodeWriter()
for row in data:
writer.writerow(row)
return writer.getvalue()
class UnicodeWriter:
def __init__(self, encoding="utf-8"):
self.encoding = encoding
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, quoting=csv.QUOTE_NONNUMERIC)
def writerow(self, row):
row = encode(row, self.encoding)
self.writer.writerow(row)
def getvalue(self):
return self.queue.getvalue()