public inbox for [email protected]  
help / color / mirror / Atom feed
From: Rahul Soshte <[email protected]>
To: Khushboo Vashi <[email protected]>
Cc: Murtuza Zabuawala <[email protected]>
Cc: Joao De Almeida Pereira <[email protected]>
Cc: pgadmin-hackers <[email protected]>
Subject: Re: [pgAdmin4][Patch][Feature #1998] Appends .sql if extension not given when using 'save' or 'save as' feature
Date: Mon, 16 Apr 2018 17:50:40 +0530
Message-ID: <CAKyzeV3FnE3XEoSmKyoN=w0NCP7nXq0AiahUPkBOq2FAQgkWEg@mail.gmail.com> (raw)
In-Reply-To: <CAFOhELcqwGjZGn5zEbWz6UWX0y2K7XkSYZ9TcYzM8QVqkhU4_Q@mail.gmail.com>
References: <CAKyzeV1fMB6rPMF+LCMY7B6_2-j7DZfmh307dBFemtR=4taX6g@mail.gmail.com>
	<CAE+jjam7ks2X=x5WoEp7WEn4tLNzw8Gzu+Mqx1sUm0OUu4Md-A@mail.gmail.com>
	<CAKKotZR_c5xJ+U-4bYQJ0DJHvDPxPK1aWOi7b7APFhNzpr1F9w@mail.gmail.com>
	<CAKyzeV0yp3-W2-=aBW38bg5zqEf44JqTnfPAtxra6ZwgwArHkw@mail.gmail.com>
	<CAKKotZQwA=x1COBZ=GhC1inE1aOk43Y4sjyKBLq4tkUz7H8NiA@mail.gmail.com>
	<CAKKotZSCFBnwZxwGH3VKUnNYtXJFjqBePxt3LBCs=8PPYBT3cw@mail.gmail.com>
	<CAKyzeV1eoSyT-YWac=OA_kjA1aDtV+1v3+fYop+WhBAXLwq8+g@mail.gmail.com>
	<CAKKotZRiEnSjtwDNx3XAW-MsjKCXsut+k_CJS0kjXij_9jeE3g@mail.gmail.com>
	<CAKyzeV3-6yVGRj0i8ZU-4Y-ysgfHk3sRiJm4cTwi=KonMJNgrQ@mail.gmail.com>
	<CAKKotZRcx4ZcQ2+gfa3zLf4nH12DHCbZOi2P+iM3bewP_38hGA@mail.gmail.com>
	<CAKyzeV3ciT_L9OoOG8XcxQPNsCokvwgduhGM0Cha0FtXW7damA@mail.gmail.com>
	<CAKKotZR9JEQm8rA21Cx-buLQUxKXQixoTY8wWfu=bov0pPXAiw@mail.gmail.com>
	<CAKyzeV3UzJe-SvykiG-kK701GpHHkG=GGVL_FBRGwSpvNK8nqA@mail.gmail.com>
	<CAKyzeV3rObAuH_QRA7wredF+rA516TKHWZRxKCryeY6tReJOxQ@mail.gmail.com>
	<CAKyzeV0FMhwH8UvxDN9XgE7nUdU5zh7jXKuB5=ow10N=7wkvNg@mail.gmail.com>
	<CAKyzeV11f+GYC64xA8BzBjSNwg91eH7MRHcFiLYfS4Wgz3A7kg@mail.gmail.com>
	<CAFOhELcqwGjZGn5zEbWz6UWX0y2K7XkSYZ9TcYzM8QVqkhU4_Q@mail.gmail.com>

Hi,

I have attached the WIP Patch.

Regards,
Rahul Soshte

On Mon, Apr 16, 2018 at 4:13 PM, Khushboo Vashi <
[email protected]> wrote:

> Hi Rahul,
>
> Instead of partial code, please send WIP patch, so I can look into this
> and help you.
>
> Thanks,
> Khushboo
>
>
> On Sat, Apr 14, 2018 at 4:51 PM, Rahul Soshte <[email protected]>
> wrote:
>
>> // read data from codemirror and write to file
>>       _save_file_handler: function(e) {
>>         var self = this,
>>           _e = e,
>>           data = {
>>             'file_name': decodeURI(e[0]),
>>             'file_content': self.gridView.query_tool_obj.getValue(),
>>             'file_type': e[1],
>>           };
>>         self.trigger(
>>           'pgadmin-sqleditor:loading-icon:show',
>>           gettext('Saving the queries in the file...')
>>         );
>>
>>         // Make ajax call to save the data to file
>>         $.ajax({
>>           url: url_for('sqleditor.save_file'),
>>           method: 'POST',
>>           contentType: 'application/json',
>>           data: JSON.stringify(data),
>>           success: function(res) {
>>             if (res.data.status) {
>>               alertify.success(res.data.result);
>>               alertify.success(gettext('File saved successfully.'));
>>               self.gridView.current_file = res.data.result;
>>               self.gridView.current_file_type = e[1];
>>               self.setTitle(self.gridView.cu
>> rrent_file.replace(/^.*[\\\/]/g, ''), true);
>>               // disable save button on file save
>>               $('#btn-save').prop('disabled', true);
>>
>> On Sat, Apr 14, 2018 at 4:50 PM, Rahul Soshte <[email protected]>
>> wrote:
>>
>>>
>>>
>>>
>>> On Sat, Apr 14, 2018 at 4:49 PM, Rahul Soshte <[email protected]>
>>> wrote:
>>>
>>>> Also attaching my web/pgadmin/tools/sqleditor/static/sqleditor.js
>>>> my  _save_file_handler code
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Sat, Apr 14, 2018 at 4:46 PM, Rahul Soshte <[email protected]
>>>> > wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have been partially able to solve the problem as you can see in the
>>>>> image. When I select sql in format combobox then only the .sql extension is
>>>>> added.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>  But another issue has arisen. Once the file is saved and now When I
>>>>> change the text in the CodeMirror , the save button is enabled and then
>>>>> when I click on it , a error message pops.The problem is whenever I try to
>>>>> save a file for the first time the code works ,but the 2nd time
>>>>> I try to save it,the code faulters.
>>>>>
>>>>> Error message is as follows:
>>>>>
>>>>> Traceback (most recent call last):
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1997, in __call__
>>>>>     return self.wsgi_app(environ, start_response)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1985, in wsgi_app
>>>>>     response = self.handle_exception(e)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1540, in handle_exception
>>>>>     reraise(exc_type, exc_value, tb)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1982, in wsgi_app
>>>>>     response = self.full_dispach_request()
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1614, in full_dispatch_request
>>>>>     rv = self.handle_user_exception(e)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1517, in handle_user_exception
>>>>>     reraise(exc_type, exc_value, tb)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1612, in full_dispatch_request
>>>>>     rv = self.dispatch_request()
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask/app.py",
>>>>> line 1598, in dispatch_request
>>>>>     return self.view_functions[rule.endpoint](**req.view_args)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/flask_login.py",
>>>>> line 792, in decorated_view
>>>>>     return func(*args, **kwargs)
>>>>>   File "/var/www/flask/pgadmin4/pgadmin4/web/pgadmin/tools/sqleditor/__init__.py",
>>>>> line 1314, in save_file
>>>>>     'file_path': file_path
>>>>>   File "/var/www/flask/pgadmin4/pgadmin4/web/pgadmin/utils/ajax.py",
>>>>> line 61, in make_json_response
>>>>>     separators=(',', ':')),
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/simplejson/__init__.py",
>>>>> line 399, in dumps
>>>>>     **kw).encode(obj)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/simplejson/encoder.py",
>>>>> line 291, in encode
>>>>>     chunks = self.iterencode(o, _one_shot=True)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/simplejson/encoder.py",
>>>>> line 373, in iterencode
>>>>>     return _iterencode(o, 0)
>>>>>   File "/var/www/flask/pgadmin4/pgadmin4/web/pgadmin/utils/ajax.py",
>>>>> line 30, in default
>>>>>     return json.JSONEncoder.default(self, obj)
>>>>>   File "/var/www/flask/pgadmin4/lib/python2.7/site-packages/simplejson/encoder.py",
>>>>> line 268, in default
>>>>>     o.__class__.__name__)
>>>>> TypeError: Object of type Response is not JSON serializable
>>>>>
>>>>> I pass an array which contains the file_name and file_type in
>>>>> /pgadmin/misc/file_manager/static/js/file_manager.js
>>>>>
>>>>>
>>>>>
>>>>> Also I have done similar changes around line 678.
>>>>>
>>>>> Also in web/pgadmin/tools/sqleditor/__init__.py I have made the
>>>>> folllowing changes
>>>>>
>>>>>
>>>>>
>>>>> Also I have attached my web/pgadmin/tools/sqleditor/ut
>>>>> ils/save_file_query_to_file_utils.py
>>>>>
>>>>
>>>>
>>>
>>
>


Attachments:

  [image/png] image.png (64.0K, 3-image.png)
  download | view image

  [image/png] image.png (29.1K, 4-image.png)
  download | view image

  [image/png] image.png (62.9K, 5-image.png)
  download | view image

  [image/png] image.png (62.4K, 6-image.png)
  download | view image

  [image/png] image.png (64.0K, 7-image.png)
  download | view image

  [image/png] image.png (24.9K, 8-image.png)
  download | view image

  [text/x-patch] WIPPatch.diff (12.4K, 9-WIPPatch.diff)
  download | inline diff:
diff --git a/web/pgadmin/misc/file_manager/__init__.py b/web/pgadmin/misc/file_manager/__init__.py
index 32afb5e3..b34caf69 100644
--- a/web/pgadmin/misc/file_manager/__init__.py
+++ b/web/pgadmin/misc/file_manager/__init__.py
@@ -211,7 +211,6 @@ def utility():
         status=200,
         mimetype="application/javascript")
 
-
 @blueprint.route("/file_manager.js")
 @login_required
 def file_manager_js():
diff --git a/web/pgadmin/misc/file_manager/static/js/file_manager.js b/web/pgadmin/misc/file_manager/static/js/file_manager.js
index 10de1ea8..9f28769a 100644
--- a/web/pgadmin/misc/file_manager/static/js/file_manager.js
+++ b/web/pgadmin/misc/file_manager/static/js/file_manager.js
@@ -294,7 +294,8 @@ define('misc.file_manager', [
             var innerbody;
 
             if (closeEvent.button.text == gettext('Select')) {
-              var newFile = $('.storage_dialog #uploader .input-path').val(),
+              var newFile = [$('.storage_dialog #uploader .input-path').val(),
+                  '*'],
                 file_data = {
                   'path': $('.currentpath').val(),
                 };
@@ -305,7 +306,7 @@ define('misc.file_manager', [
               innerbody.remove();
               removeTransId(trans_id);
               // Ajax call to store the last directory visited once user press select button
-
+              
               set_last_traversed_dir(file_data, trans_id);
             } else if (closeEvent.button.text == gettext('Cancel')) {
               innerbody = $(this.elements.body).find('.storage_content');
@@ -578,8 +579,11 @@ define('misc.file_manager', [
               $('.replace_file, .fm_dimmer').hide();
               $yesBtn.off();
               $noBtn.off();
-              var newFile = $('.storage_dialog #uploader .input-path').val();
-
+              var newFile = [
+                $('.storage_dialog #uploader .input-path').val(),
+                $('.change_file_types select').val(),
+              ];
+              
               pgAdmin.Browser.Events.trigger('pgadmin-storage:finish_btn:create_file', newFile);
               $('.file_manager_create_cancel').trigger('click');
               $('.storage_dialog #uploader .input-path').attr('disabled', false);
@@ -660,18 +664,23 @@ define('misc.file_manager', [
           },
           callback: function(closeEvent) {
             if (closeEvent.button.text == gettext('Create')) {
-              var newFile = $('.storage_dialog #uploader .input-path').val(),
+              var newFile = [ 
+                  $('.storage_dialog #uploader .input-path').val(),
+                  $('.change_file_types select').val(),
+                ],
                 file_data = {
                   'path': $('.currentpath').val(),
                 },
                 innerbody;
 
-              if (!this.check_permission(newFile)) {
+            //  var file_type = $('.change_file_types select').val()  
+
+              if (!this.check_permission(newFile[0])) {
                 closeEvent.cancel = true;
                 return;
               }
 
-              if (!_.isUndefined(newFile) && newFile !== '' && this.is_file_exist()) {
+              if (!_.isUndefined(newFile[0]) && newFile[0] !== '' && this.is_file_exist()) {
                 this.replace_file();
                 closeEvent.cancel = true;
               } else {
diff --git a/web/pgadmin/misc/file_manager/static/js/utility.js b/web/pgadmin/misc/file_manager/static/js/utility.js
index 249b0fe8..fc4b8edf 100755
--- a/web/pgadmin/misc/file_manager/static/js/utility.js
+++ b/web/pgadmin/misc/file_manager/static/js/utility.js
@@ -19,6 +19,14 @@ define([
     Define functions used for various operations
   ---------------------------------------------------------*/
 
+  //get Format type.
+//  var getFormattype = function(name){
+  //  $('.allowed_file_types select').on('change', function() {
+    //      var selected_val = $(this).val(),
+      //    return selected_val
+        //});
+ // }
+
   // Return file extension
   var getFileExtension = function(name) {
     var found = name.lastIndexOf('.') + 1;
diff --git a/web/pgadmin/tools/sqleditor/__init__.py b/web/pgadmin/tools/sqleditor/__init__.py
index fae527c7..d348a3d0 100644
--- a/web/pgadmin/tools/sqleditor/__init__.py
+++ b/web/pgadmin/tools/sqleditor/__init__.py
@@ -41,6 +41,7 @@ from pgadmin.tools.sqleditor.utils.query_tool_preferences import \
 from pgadmin.tools.sqleditor.utils.query_tool_fs_utils import \
     read_file_generator
 from pgadmin.tools.sqleditor.utils.filter_dialog import FilterDialog
+from pgadmin.tools.sqleditor.utils.save_query_to_file_utils import save_query_to_file
 
 MODULE_NAME = 'sqleditor'
 
@@ -1294,7 +1295,6 @@ def load_file():
 
     return Response(read_file_generator(file_path, enc), mimetype='text/plain')
 
-
 @blueprint.route('/save_file/', methods=["PUT", "POST"], endpoint='save_file')
 @login_required
 def save_file():
@@ -1305,52 +1305,13 @@ def save_file():
     if request.data:
         file_data = json.loads(request.data, encoding='utf-8')
 
-    # retrieve storage directory path
-    storage_manager_path = get_storage_directory()
-
-    # generate full path of file
-    file_path = unquote(file_data['file_name'])
-    if hasattr(str, 'decode'):
-        file_path = unquote(
-            file_data['file_name']
-        ).encode('utf-8').decode('utf-8')
-
-    try:
-        Filemanager.check_access_permission(storage_manager_path, file_path)
-    except Exception as e:
-        return internal_server_error(errormsg=str(e))
-
-    if storage_manager_path is not None:
-        file_path = os.path.join(
-            storage_manager_path,
-            file_path.lstrip('/').lstrip('\\')
-        )
-
-    if hasattr(str, 'decode'):
-        file_content = file_data['file_content']
-    else:
-        file_content = file_data['file_content'].encode()
-
-    # write to file
-    try:
-        with open(file_path, 'wb+') as output_file:
-            if hasattr(str, 'decode'):
-                output_file.write(file_content.encode('utf-8'))
-            else:
-                output_file.write(file_content)
-    except IOError as e:
-        if e.strerror == 'Permission denied':
-            err_msg = "Error: {0}".format(e.strerror)
-        else:
-            err_msg = "Error: {0}".format(e.strerror)
-        return internal_server_error(errormsg=err_msg)
-    except Exception as e:
-        err_msg = "Error: {0}".format(e.strerror)
-        return internal_server_error(errormsg=err_msg)
+    filepath1=save_query_to_file(file_data);
 
     return make_json_response(
         data={
-            'status': True,
+            'result': filepath1,
+            'status': True
+            
         }
     )
 
diff --git a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
index f8cb05af..3ca436ce 100644
--- a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
+++ b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
@@ -1947,6 +1947,7 @@ define('tools.querytool', [
 
         self.gridView.editor_title = _.unescape(editor_title);
         self.gridView.current_file = undefined;
+        self.gridView.current_file_type = undefined; 
 
         // Render the header
         self.gridView.render();
@@ -2588,8 +2589,10 @@ define('tools.querytool', [
 
         // Open save file dialog if query tool
         if (self.is_query_tool) {
-          var current_file = self.gridView.current_file;
-          if (!_.isUndefined(current_file) && !save_as) {
+       
+          var current_file = [ self.gridView.current_file, self.gridView.current_file_type];
+          alertify.success(self.gridView.current_file);
+          if (!_.isUndefined(current_file[0]) && !save_as) {
             self._save_file_handler(current_file);
           } else {
             // provide custom option to save file dialog
@@ -2868,7 +2871,8 @@ define('tools.querytool', [
         var self = this,
           _e = e,
           data = {
-            'file_name': decodeURI(e),
+            'file_name': decodeURI(e[0]),
+            'file_type': e[1],
           };
 
         self.trigger(
@@ -2887,7 +2891,8 @@ define('tools.querytool', [
           data: JSON.stringify(data),
           success: function(res) {
             self.gridView.query_tool_obj.setValue(res);
-            self.gridView.current_file = e;
+            self.gridView.current_file = e[0];
+            self.gridView.current_file_type= e[1];
             self.setTitle(self.gridView.current_file.split('\\').pop().split('/').pop(), true);
             self.trigger('pgadmin-sqleditor:loading-icon:hide');
             // hide cursor
@@ -2919,8 +2924,9 @@ define('tools.querytool', [
         var self = this,
           _e = e,
           data = {
-            'file_name': decodeURI(e),
+            'file_name': decodeURI(e[0]),
             'file_content': self.gridView.query_tool_obj.getValue(),
+            'file_type': e[1],
           };
         self.trigger(
           'pgadmin-sqleditor:loading-icon:show',
@@ -2935,8 +2941,10 @@ define('tools.querytool', [
           data: JSON.stringify(data),
           success: function(res) {
             if (res.data.status) {
+             // alertify.success(res.data.result);
               alertify.success(gettext('File saved successfully.'));
-              self.gridView.current_file = e;
+              self.gridView.current_file = res.data.result;
+              self.gridView.current_file_type = e[1];
               self.setTitle(self.gridView.current_file.replace(/^.*[\\\/]/g, ''), true);
               // disable save button on file save
               $('#btn-save').prop('disabled', true);
@@ -2971,6 +2979,7 @@ define('tools.querytool', [
           self.is_query_changed = true;
 
           if (self.gridView.current_file) {
+            alertify.success(self.gridView.current_file);
             var title = self.gridView.current_file.replace(/^.*[\\\/]/g, '') + ' *';
             self.setTitle(title, true);
           } else {
diff --git a/web/pgadmin/tools/sqleditor/utils/save_query_to_file_utils.py b/web/pgadmin/tools/sqleditor/utils/save_query_to_file_utils.py
new file mode 100644
index 00000000..a67c3e83
--- /dev/null
+++ b/web/pgadmin/tools/sqleditor/utils/save_query_to_file_utils.py
@@ -0,0 +1,71 @@
+import os
+from pgadmin.misc.file_manager import Filemanager
+from pgadmin.utils import get_storage_directory
+from pgadmin.utils.ajax import make_json_response, bad_request, \
+    success_return, internal_server_error, unauthorized
+
+try:
+    from urllib import unquote
+except ImportError:
+    from urllib.parse import unquote
+
+
+def save_query_to_file(file_data):
+    # retrieve storage directory path
+    storage_manager_path = get_storage_directory()
+
+    # generate full path of file
+    file_path = unquote(file_data['file_name'])
+
+    if hasattr(str, 'decode'):
+        file_path = unquote(
+            file_data['file_name']
+        ).encode('utf-8').decode('utf-8')
+        
+        file_type = file_data['file_type']
+
+    
+    #file_type=file_data['file_type']
+    if file_type == 'sql':
+        if not file_path.endswith('.sql'):
+            file_path = file_path + ".sql"        
+            
+    file_path = file_path.encode('utf-8')
+
+    try:
+        Filemanager.check_access_permission(storage_manager_path, file_path)
+    except Exception as e:
+        return internal_server_error(errormsg=str(e))
+
+    # lstrip() returns a copy of the string
+    # in which all chars have been stripped
+    # from the beginning of the string (default whitespace characters).
+    if storage_manager_path is not None:
+        file_path = os.path.join(
+            storage_manager_path,
+            file_path.lstrip('/').lstrip('\\')
+        )
+
+    if hasattr(str, 'decode'):
+        file_content = file_data['file_content']
+    else:
+        file_content = file_data['file_content'].encode()
+
+    # write to file
+    try:
+        with open(file_path, 'wb+') as output_file:
+            if hasattr(str, 'decode'):
+                output_file.write(file_content.encode('utf-8'))
+                return file_path
+            else:
+                output_file.write(file_content)
+                return file_path
+    except IOError as e:
+        if e.strerror == 'Permission denied':
+            err_msg = "Error: {0}".format(e.strerror)
+        else:
+            err_msg = "Error: {0}".format(e.strerror)
+        return internal_server_error(errormsg=err_msg)
+    except Exception as e:
+        err_msg = "Error: {0}".format(e.strerror)
+        return internal_server_error(errormsg=err_msg)


view thread (20+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: [pgAdmin4][Patch][Feature #1998] Appends .sql if extension not given when using 'save' or 'save as' feature
  In-Reply-To: <CAKyzeV3FnE3XEoSmKyoN=w0NCP7nXq0AiahUPkBOq2FAQgkWEg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox