Milimetric has submitted this change and it was merged.

Change subject: posting desired responses to /jobs/create/
......................................................................


posting desired responses to /jobs/create/

Change-Id: I0f8db1ea8c9e1c6a77ef2d1a453af10dd7c21513
---
M wikimetrics/controllers/jobs.py
M wikimetrics/static/js/jobCreate.js
M wikimetrics/static/js/knockout.util.js
M wikimetrics/templates/form_for_metrics.html
A wikimetrics/templates/jobs.html
M wikimetrics/templates/request.html
6 files changed, 45 insertions(+), 27 deletions(-)

Approvals:
  Milimetric: Verified; Looks good to me, approved



diff --git a/wikimetrics/controllers/jobs.py b/wikimetrics/controllers/jobs.py
index 4acf035..a96a962 100644
--- a/wikimetrics/controllers/jobs.py
+++ b/wikimetrics/controllers/jobs.py
@@ -15,12 +15,16 @@
     return 'jobs'
 
 
[email protected]('/jobs/create', methods=['GET'])
[email protected]('/jobs/create/', methods=['GET', 'POST'])
 def jobs_request():
     """
     Renders a page that facilitates kicking off a new job
     """
-    return render_template('request.html')
+    if request.method == 'GET':
+        return render_template('request.html')
+    else:
+        print request.form['responses']
+        return render_template('jobs.html')
 
 
 @app.route('/jobs/list/')
diff --git a/wikimetrics/static/js/jobCreate.js 
b/wikimetrics/static/js/jobCreate.js
index 79e18d3..08271ed 100644
--- a/wikimetrics/static/js/jobCreate.js
+++ b/wikimetrics/static/js/jobCreate.js
@@ -1,17 +1,10 @@
 $(document).ready(function(){
-    // set up async handlers for any async forms
-    // TODO: replace with a decent plugin
-    
-    $(document).on('submit', 'form.job-request', function(e){
-        // same thing as metric-configuration, but passing 
viewModel.request().responses()
-        // as the data
-    });
     
     var viewModel = {
         cohorts: ko.observableArray([]),
         toggleCohort: function(cohort){
             // fetch wikiusers
-            if (!cohort.wikiusers) {
+            if (cohort && !cohort.wikiusers) {
                 cohort.wikiusers = ko.observableArray([]);
                 $.get('/cohorts/detail/' + cohort.id, function(data){
                     cohort.wikiusers(data.wikiusers);
@@ -23,17 +16,39 @@
         metrics: ko.observableArray([]),
         toggleMetric: function(metric){
             
-            if (metric.selected()){
-                // fetch form to configure metric with
-                $.get('/metrics/configure/' + metric.name, 
function(configureForm){
-                    metric.configure(configureForm);
-                }).fail(failure);
-            } else {
-                metric.configure('');
+            if (metric) {
+                if (metric.selected()){
+                    // fetch form to configure metric with
+                    $.get('/metrics/configure/' + metric.name, 
function(configureForm){
+                        metric.configure(configureForm);
+                    }).fail(failure);
+                } else {
+                    metric.configure('');
+                }
             }
             return true;
         },
-
+        
+        save: function(formElement){
+            var vm = ko.dataFor(formElement);
+            var form = $(formElement);
+            var data = ko.toJSON(vm.request().responses);
+            data = JSON.parse(data);
+            ko.utils.arrayForEach(data, function(response){
+                delete response.metric.configure;
+                delete response.cohort.wikiusers;
+            });
+            data = JSON.stringify(data);
+            
+            $.ajax({
+                type: 'post',
+                url: form.attr('action'),
+                data: {responses: data},
+            }).done(function(response){
+                alert(response);
+            }).fail(failure);
+        },
+        
         saveMetricConfiguration: function(formElement){
             var metric = ko.dataFor(formElement);
             var form = $(formElement);
@@ -45,9 +60,8 @@
                 url: form.attr('action'),
                 data: data,
             }).done(function(htmlToReplaceWith){
-                    metric.configure(htmlToReplaceWith);
-            }).fail(function(){
-            });
+                metric.configure(htmlToReplaceWith);
+            }).fail(failure);
         },
     };
     
diff --git a/wikimetrics/static/js/knockout.util.js 
b/wikimetrics/static/js/knockout.util.js
index d280b76..1285202 100644
--- a/wikimetrics/static/js/knockout.util.js
+++ b/wikimetrics/static/js/knockout.util.js
@@ -1,10 +1,10 @@
 /**
  * Custom binding that is used as follows:
- * `<section data-bind="subview: observableProperty"></section>`
+ * `<section data-bind="metricConfigurationForm: property"></section>`
  * And works as follows:
- *     In the example above, observableProperty is a ko.observable whose value 
is an object that has a `template` property
- *     The binding finds the template with id `observableProperty().template` 
and fills it as the innerHTML of the section element
- *     The binding then sets the context for the section's child elements as 
the observableProperty (like with: observableProperty)
+ *     In the example above, property is a ko.observable or plain property 
that evaluates to some HTML which
+ *     should be rendered inside the <section></section>
+ *     The binding then sets the context for the section's child elements as 
the same as the current context
  */
 ko.bindingHandlers.metricConfigurationForm = {
     init: function(element, valueAccessor, allBindingsAccessor, viewModel, 
bindingContext){
diff --git a/wikimetrics/templates/form_for_metrics.html 
b/wikimetrics/templates/form_for_metrics.html
index 7530701..377e397 100644
--- a/wikimetrics/templates/form_for_metrics.html
+++ b/wikimetrics/templates/form_for_metrics.html
@@ -1,5 +1,5 @@
 <form class="form-horizontal metric-configuration" method="POST" 
action="{{action}}" data-bind="submit: $root.saveMetricConfiguration">
-    {# form.hidden_tag() #}
+    {# TODO: flask-wtf does nice things, bring this back: form.hidden_tag() #}
     {% for f in form if f.label.text != 'Csrf Token' %}
         <div class="control-group">
             {{ f.label(class="control-label") }}
diff --git a/wikimetrics/templates/jobs.html b/wikimetrics/templates/jobs.html
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/wikimetrics/templates/jobs.html
diff --git a/wikimetrics/templates/request.html 
b/wikimetrics/templates/request.html
index 73ed5b9..b763647 100644
--- a/wikimetrics/templates/request.html
+++ b/wikimetrics/templates/request.html
@@ -80,7 +80,7 @@
     </div>
 </div>
 <div class="form-actions">
-    <form class="job-request pull-right" action="/jobs/create/">
+    <form class="job-request pull-right" action="/jobs/create/" 
data-bind="submit: save">
         <input class="btn btn-primary save" type="submit" value="Run Job"/>
     </form>
 </div>

-- 
To view, visit https://gerrit.wikimedia.org/r/71739
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0f8db1ea8c9e1c6a77ef2d1a453af10dd7c21513
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikimetrics
Gerrit-Branch: master
Gerrit-Owner: Milimetric <[email protected]>
Gerrit-Reviewer: Milimetric <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to