Commits

Anonymous committed ebf96c4

Error checking

  • Participants
  • Parent commits 03fd447

Comments (0)

Files changed (3)

File txclib/commands.py

 
 def cmd_wui(argv, path_to_tx):
     """Run a web ui."""
-    from flask import Flask, jsonify, render_template, request
+    from flask import Flask, flash, jsonify, render_template, request
     import webbrowser
     app = Flask(__name__)
-
+    app.secret_key = 'blahdiblah'
 
     @app.route('/tx/home/')
     def home(name=None):
         prj = project.Project(path_to_tx)
         res_old = request.form['elementid']
         res_new = request.form['value']
+        
+        # Raise error with invalid resource ID
+        if res_new.count('.') != 1:
+            error_msg = u"New resource ID should have one dot in it."
+            flash(error_msg, 'error')
+            logger.warning(error_msg)
+            return(res_old)
+
+        # Avoid deleting the same resource if unchanged:
+        if res_new == res_old:
+            return(res_old)
+
         logger.info("Renaming %s to %s" % (res_old, res_new))
         prj.config._sections[res_new] = prj.config._sections[res_old]
         prj.config._sections.pop(res_old)
         prj.save()
         return res_new
 
+
     @app.route('/_modify_expr', methods=['POST'])
     def modify_expr():
         """Modify the file filter of a resource."""
         prj.save()
         return expr
 
+
     logger.info("Running web UI. Please navigate to http://localhost:5000/")
     webbrowser.open("http://localhost:5000/tx/home", new=0)
     app.run(debug=True)

File txclib/templates/base.html

   </div>
   
 	{% block messages %}
+	  {% with messages = get_flashed_messages() %}
     {% if messages %}
 	    <div class="messages">
 	    {% for message in messages %}
 	    {% endfor %}
 	    </div>
     {% endif %}
+    {% endwith %}
   {% endblock %}
 	
 	<div id="header_area" class="tabbed-header">

File txclib/templates/home.html

     ev.preventDefault();
   });
 
+  /* Highlight rows */
   $("input.rowcheckbox").click(function() { 
     $(this).parent().parent().toggleClass('highlight');
   });
 
-  /* Modify resource name */
+  /* AJAX: Modify resource */
   $('.resname.edit').editable(ACTIONS["rename"].url, {
     id: 'elementid',
     style: 'display: inline',
     tooltip: 'Click to edit...',
+    callback: function(value, settings) {
+      /* Refresh the whole row with new resource IDs */
+      location.reload();
+    }
   }, {  });
 
-  /* Modify resource expr */
+  /* AJAX: Modify resource expression */
   $('.resexpr.edit').editable(function(value, settings) {
-  
+    var $this = $(this);
+
     $.ajax({
       type : "POST",
       cache : false,
-      url  : ACTIONS["modify_expr"].url,
+      url: ACTIONS["modify_expr"].url,
       dataType: 'json',
-      data : { 'res_id': $('.resexpr.edit').attr('data-id'),
-               'value': value },
-      success : function(data) {
-       if (data.status == 'ok') {
-        alert('ok');
-        return data.title;
-       } else {
-        alert('n/ok');
-        return this.revert;   
-       }
+      data: { 'res_id': $this.attr('data-id'),
+              'value': value },
+      success: function(data) {
+        location.reload();
+      }
+    }); /* /ajax */
 
-    }});
-
-      return value.replace('<lang>', '<span class="lang">&lt;lang&gt;</span>');
-
-    }, {
-      style: 'display: inline',
-      tooltip: 'Click to edit...',
-      data: function(value, settings) {
-        /* Convert $lang to aaa. */
-        return value.replace('<span class="lang">&lt;lang&gt;</span>', '<lang>');
-      },
-    });
-
+    return value.replace('<lang>', '<span class="lang">&lt;lang&gt;</span>');
+  }, {
+    style: 'display: inline',
+    tooltip: 'Click to edit...',
+    data: function(value, settings) {
+      return value.replace('<span class="lang">&lt;lang&gt;</span>', '<lang>');
+    },
+  });
 });
 
 </script>