[tor-commits] [ooni-probe/develop] Implement in-browser input file selection views

isis at torproject.org isis at torproject.org
Wed Jun 26 01:02:10 UTC 2013


commit 13a65fd6adb798f6c2bdcc4584a5cff52e694d7c
Author: Arturo Filastò <art at fuffa.org>
Date:   Mon Apr 29 18:54:01 2013 +0200

    Implement in-browser input file selection views
    
    This will allow the user to copy and paste the input file into a textarea.
---
 data/ui/app/scripts/controllers.js |   28 ++++++++++++++++++++++++++++
 data/ui/app/views/test.html        |   12 +++++++++---
 ooni/api/spec.py                   |   33 +++++++++++++++++++++++++++++++--
 ooni/nettest.py                    |    5 ++++-
 4 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/data/ui/app/scripts/controllers.js b/data/ui/app/scripts/controllers.js
index 5ba24e1..c2a9c0d 100644
--- a/data/ui/app/scripts/controllers.js
+++ b/data/ui/app/scripts/controllers.js
@@ -49,7 +49,17 @@ ooniprobe.controller('TestCtrl', ['$scope', '$routeParams', 'testStatus', 'Input
 
 ooniprobe.controller('TestBoxCtrl', ['$scope', 'startTest',
                      function($scope, startTest) {
+  function hasAttributes(obj) {
+    var count = 0;
+    for (var i in obj)
+      count +=1;
+    if ( count == 0 ) {
+      return false;
+    }
+    return true;
+  }
 
+  $scope.manualFileInput = {};
   $scope.startTest = function() {
     var options = {};
 
@@ -58,6 +68,13 @@ ooniprobe.controller('TestBoxCtrl', ['$scope', 'startTest',
       options[key] = option.value;
     });
 
+    if (hasAttributes($scope.manualFileInput)) {
+      options['manual_input'] = {};
+      angular.forEach($scope.manualFileInput, function(value, key) {
+        options['manual_input'][key] = value;
+      });
+    }
+
     startTest($scope.testDetails.id, options).success(function(){
       $scope.updateTestStatus();
     });
@@ -65,4 +82,15 @@ ooniprobe.controller('TestBoxCtrl', ['$scope', 'startTest',
 
 }]);
 
+ooniprobe.controller('FileInput', ['$scope',
+                     function($scope) {
+
+  $scope.manualShow = false;
+  $scope.toggleManualInput = function() {
+    if ($scope.manualShow)
+      $scope.manualShow = false;
+    else
+      $scope.manualShow = true;
+  }
 
+}]);
diff --git a/data/ui/app/views/test.html b/data/ui/app/views/test.html
index f92461a..fdd5931 100644
--- a/data/ui/app/views/test.html
+++ b/data/ui/app/views/test.html
@@ -8,11 +8,17 @@
         <div ng-repeat="(name, options) in testDetails.arguments">
           <div ng-switch on="options.type">
 
-            <div ng-switch-when="file">
-              <label>{{name}}</label>
-              <select ng-model="testDetails.arguments[name].value">
+            <div ng-switch-when="file" ng-controller="FileInput">
+              <label>{{name}}
+              <button class="btn btn-small" ng-click="toggleManualInput()">Toggle manual input</button>
+              </label>
+              <select ng-model="testDetails.arguments[name].value" ng-hide="manualShow">
                 <option ng-repeat="input in inputs" value="input.filename">{{input.filename}}</option>
               </select>
+              <br/>
+              <div class="manualFileInput" ng-show="manualShow">
+                <textarea ng-model="manualFileInput[name]"></textarea>
+              </div>
             </div>
 
             <div ng-switch-default>
diff --git a/ooni/api/spec.py b/ooni/api/spec.py
index 071beaf..fe18ccc 100644
--- a/ooni/api/spec.py
+++ b/ooni/api/spec.py
@@ -3,6 +3,7 @@ import re
 import copy
 import json
 import types
+import tempfile
 
 from twisted.python import usage
 from cyclone import web, escape
@@ -122,6 +123,20 @@ def get_reporters(net_test_loader):
         reporters.append(oonib_reporter)
     return reporters
 
+def write_temporary_input(content):
+    """
+    Creates a temporary file for the given content.
+
+    Returns:
+        the path to the temporary file.
+    """
+    fd, path = tempfile.mkstemp()
+    with open(path, 'w') as f:
+        f.write(content)
+        f.close()
+    print "This is the path %s" % path
+    return fd, path
+
 class StartTest(ORequestHandler):
     def post(self, test_name):
         """
@@ -129,11 +144,25 @@ class StartTest(ORequestHandler):
         """
         test_file = oonidApplication.director.netTests[test_name]['path']
         test_options = json.loads(self.request.body)
+        tmp_files = []
+        if (test_options['manual_input']):
+            for option, content in test_options['manual_input'].items():
+                fd, path = write_temporary_input(content)
+                test_options[option] = path
+                tmp_files.append((fd, path))
+            test_options.pop('manual_input')
+
         net_test_loader = get_net_test_loader(test_options, test_file)
         try:
             net_test_loader.checkOptions()
-            oonidApplication.director.startNetTest(net_test_loader,
-                                                   get_reporters(net_test_loader))
+            d = oonidApplication.director.startNetTest(net_test_loader,
+                                                       get_reporters(net_test_loader))
+            @d.addBoth
+            def cleanup(result):
+                for fd, path in tmp_files:
+                    os.close(fd)
+                    os.remove(path)
+
         except MissingRequiredOption, option_name:
             self.write({'error':
                         'Missing required option: "%s"' % option_name})
diff --git a/ooni/nettest.py b/ooni/nettest.py
index 66639f8..601fa24 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -132,8 +132,11 @@ def getArguments(test_class):
 
     for opt_parameter in test_class.usageOptions.optParameters:
         option_name = opt_parameter[0]
+        opt_type="text"
+        if opt_parameter[3].lower().startswith("file"):
+            opt_type="file"
         arguments[option_name] = getOption(opt_parameter,
-                test_class.requiredOptions)
+                test_class.requiredOptions, type=opt_type)
 
     return arguments
 





More information about the tor-commits mailing list