From a1c43e51bbc8208f30cdcf7ab024d71de4a05a82 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 11 Aug 2011 15:46:19 +0530 Subject: [PATCH] fixed attachments - now not reqd to save after attaching --- cgi-bin/webnotes/model/doclist.py | 2 +- cgi-bin/webnotes/utils/file_manager.py | 74 ++++++++++++++++++++++++-- cgi-bin/webnotes/widgets/form.py | 4 ++ js/form.compressed.js | 15 +++--- js/wn/widgets/form/attachments.js | 37 ++++++------- 5 files changed, 102 insertions(+), 30 deletions(-) diff --git a/cgi-bin/webnotes/model/doclist.py b/cgi-bin/webnotes/model/doclist.py index cb70bc2df5..2da46a795e 100644 --- a/cgi-bin/webnotes/model/doclist.py +++ b/cgi-bin/webnotes/model/doclist.py @@ -97,7 +97,7 @@ class DocList: """ from webnotes.model.meta import is_single - if (not is_single(self.doc.doctype)) and (not self.doc.fields.get('__islocal')): + if (not is_single(self.doc.doctype)) and (not cint(self.doc.fields.get('__islocal'))): tmp = webnotes.conn.sql(""" SELECT modified FROM `tab%s` WHERE name="%s" for update""" % (self.doc.doctype, self.doc.name)) diff --git a/cgi-bin/webnotes/utils/file_manager.py b/cgi-bin/webnotes/utils/file_manager.py index 26575ffd5f..cccc4f9a64 100644 --- a/cgi-bin/webnotes/utils/file_manager.py +++ b/cgi-bin/webnotes/utils/file_manager.py @@ -7,20 +7,86 @@ def upload(): dn = form.getvalue('docname') at_id = form.getvalue('at_id') + webnotes.response['type'] = 'iframe' + if not webnotes.form['filedata'].filename: + webnotes.response['result'] = """ + """ % dt + return + # save fid, fname = save_uploaded() - if fid: + # save it in the form + updated = add_file_list(dt, dn, fname, fid) + + if fid and updated: # refesh the form! + # with the new modified timestamp webnotes.response['result'] = """ - """ % (dt, dn, fid, fname.replace("'", "\\'"), at_id, dt, dn) + """ % { + 'dt': dt, + 'dn': dn, + 'fid': fid, + 'fname': fname.replace("'", "\\'"), + 'at_id': at_id, + 'mod': webnotes.conn.get_value(dt, dn, 'modified') + } # ------------------------------------------------------- +def add_file_list(dt, dn, fname, fid): + """ + udpate file_list attribute of the record + """ + import webnotes + try: + # get the old file_list + fl = webnotes.conn.sql("select file_list from `tab%s` where name=%s" % (dt, '%s'), dn)[0][0] or '' + if fl: + fl += '\n' + + # add new file id + fl += fname + ',' + fid + + # save + webnotes.conn.set_value(dt, dn, 'file_list', fl) + + return True + + except Exception, e: + webnotes.response['result'] = """ +""" % str(e) + + +def remove_file_list(dt, dn, fid): + """ + Remove fid from the give file_list + """ + import webnotes + + # get the old file_list + fl = webnotes.conn.sql("select file_list from `tab%s` where name=%s" % (dt, '%s'), dn)[0][0] or '' + new_fl = [] + fl = fl.split('\n') + for f in fl: + if f.split(',')[1]!=fid: + new_fl.append(f) + + # update the file_list + webnotes.conn.set_value(dt, dn, 'file_list', '\n'.join(new_fl)) + + # return the new timestamp + return webnotes.conn.get_value(dt, dn, 'modified') + def make_thumbnail(blob, size): from PIL import Image import cStringIO diff --git a/cgi-bin/webnotes/widgets/form.py b/cgi-bin/webnotes/widgets/form.py index fea70d61c0..7f5a325770 100644 --- a/cgi-bin/webnotes/widgets/form.py +++ b/cgi-bin/webnotes/widgets/form.py @@ -197,6 +197,7 @@ def runserverobj(): doclist = DocList() doclist.from_compressed(form.getvalue('docs'), dn) so = doclist.make_obj() + doclist.check_if_latest() check_guest_access(so.doc) @@ -288,6 +289,9 @@ def remove_attach(): fid = webnotes.form.getvalue('fid') webnotes.utils.file_manager.delete_file(fid, verbose=1) + + # remove from dt dn + return str(webnotes.utils.file_manager.remove_file_list(webnotes.form.getvalue('dt'), webnotes.form.getvalue('dn'), fid)) # Get Fields - Counterpart to $c_get_fields #=========================================================================================== diff --git a/js/form.compressed.js b/js/form.compressed.js index 32c2369634..41980e4ee4 100644 --- a/js/form.compressed.js +++ b/js/form.compressed.js @@ -474,7 +474,9 @@ for(var i=0;i