Milimetric has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/73192


Change subject: fixed up the namespaces parameter and job create ajax 
interaction
......................................................................

fixed up the namespaces parameter and job create ajax interaction

Change-Id: I4df52af9c743b05982811f1e8437399c99af14e6
---
M tests/test_controllers/test_metrics.py
M wikimetrics/controllers/authentication.py
M wikimetrics/metrics/bytes_added.py
M wikimetrics/metrics/form_fields.py
M wikimetrics/metrics/namespace_edits.py
M wikimetrics/metrics/revert_rate.py
M wikimetrics/static/js/jobCreate.js
M wikimetrics/static/js/site.js
8 files changed, 57 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics 
refs/changes/92/73192/1

diff --git a/tests/test_controllers/test_metrics.py 
b/tests/test_controllers/test_metrics.py
index 68b5975..d6a1852 100644
--- a/tests/test_controllers/test_metrics.py
+++ b/tests/test_controllers/test_metrics.py
@@ -50,3 +50,13 @@
             -1,
             'Validation on a BytesAdded configuration is not happening'
         )
+    
+    def test_configure_namespaces_post(self):
+        response = self.app.post('/metrics/configure/NamespaceEdits', 
data=dict(
+            namespaces='1,a,2,3,4',
+        ))
+        assert_not_equal(
+            response.data.find('<li class="text-error">'),
+            -1,
+            'Validation on the NamespaceEdits configuration, namespaces field 
is not happening.'
+        )
diff --git a/wikimetrics/controllers/authentication.py 
b/wikimetrics/controllers/authentication.py
index 0321896..83232a7 100644
--- a/wikimetrics/controllers/authentication.py
+++ b/wikimetrics/controllers/authentication.py
@@ -35,7 +35,6 @@
     if current_user.is_authenticated():
         return
     
-    # TODO: put static resources in a new Blueprint
     if (
             request.endpoint
         and not request.path.startswith('/static/')
diff --git a/wikimetrics/metrics/bytes_added.py 
b/wikimetrics/metrics/bytes_added.py
index 8472ae1..7853980 100644
--- a/wikimetrics/metrics/bytes_added.py
+++ b/wikimetrics/metrics/bytes_added.py
@@ -3,6 +3,7 @@
 from ..models import Revision, Page
 from metric import Metric
 from form_fields import BetterBooleanField, CommaSeparatedIntegerListField
+from wtforms.validators import Required
 from wtforms import DateField
 from sqlalchemy import func, case, between
 from sqlalchemy.sql.expression import label
@@ -61,7 +62,12 @@
     
     start_date          = DateField(default=thirty_days_ago)
     end_date            = DateField(default=date.today)
-    namespaces          = CommaSeparatedIntegerListField(default=[0], 
description='0, 2, 4, etc.')
+    namespaces          = CommaSeparatedIntegerListField(
+        None,
+        [Required()],
+        default='0',
+        description='0, 2, 4, etc.',
+    )
     positive_only_sum   = BetterBooleanField(default=True)
     negative_only_sum   = BetterBooleanField(default=True)
     absolute_sum        = BetterBooleanField(default=True)
diff --git a/wikimetrics/metrics/form_fields.py 
b/wikimetrics/metrics/form_fields.py
index 31ac500..3dd6fbf 100644
--- a/wikimetrics/metrics/form_fields.py
+++ b/wikimetrics/metrics/form_fields.py
@@ -30,15 +30,15 @@
     widget = TextInput()
     
     def _value(self):
-        """ overrides wtforms representation which is sends to server """
-        if self.data:
+        """ overrides the representation wtforms sends to the server """
+        if self.data and len(self.data) > 0:
             return u', '.join(map(unicode, self.data))
         else:
             return u''
-
+    
     def process_formdata(self, valuelist):
         """ overrides wtforms parsing to split list into namespaces """
         if valuelist:
-            self.data = [int(x.strip()) for x in valuelist[0].split(',')]
+            self.data = [int(x.strip()) for x in valuelist[0].split(',') if 
x.strip().isdigit()]
         else:
             self.data = []
diff --git a/wikimetrics/metrics/namespace_edits.py 
b/wikimetrics/metrics/namespace_edits.py
index 43881e9..9eed69d 100644
--- a/wikimetrics/metrics/namespace_edits.py
+++ b/wikimetrics/metrics/namespace_edits.py
@@ -1,6 +1,7 @@
 from sqlalchemy import func
 from metric import Metric
 from form_fields import CommaSeparatedIntegerListField
+from wtforms.validators import Required
 from wikimetrics.models import Page, Revision
 import logging
 logger = logging.getLogger(__name__)
@@ -33,7 +34,12 @@
     label       = 'Edits'
     description = 'Compute the number of edits in a specific namespace of a 
mediawiki project'
     
-    namespaces = CommaSeparatedIntegerListField(default=[0], description='0, 
2, 4, etc.')
+    namespaces = CommaSeparatedIntegerListField(
+        None,
+        [Required()],
+        default='0',
+        description='0, 2, 4, etc.',
+    )
     
     def __call__(self, user_ids, session):
         """
diff --git a/wikimetrics/metrics/revert_rate.py 
b/wikimetrics/metrics/revert_rate.py
index c52a68e..93391df 100644
--- a/wikimetrics/metrics/revert_rate.py
+++ b/wikimetrics/metrics/revert_rate.py
@@ -2,6 +2,7 @@
 from metric import Metric
 from datetime import date
 from ..utils import thirty_days_ago
+from form_fields import CommaSeparatedIntegerListField
 
 __all__ = [
     'RevertRate',
@@ -40,7 +41,12 @@
     
     start_date  = wtf.DateField(default=thirty_days_ago)
     end_date    = wtf.DateField(default=date.today)
-    #namespace   = wtf.IntegerField(default=0)
+    namespaces  = CommaSeparatedIntegerListField(
+        None,
+        [Required()],
+        default='0',
+        description='0, 2, 4, etc.',
+    )
     
     def __call__(self, user_ids, session):
         """
diff --git a/wikimetrics/static/js/jobCreate.js 
b/wikimetrics/static/js/jobCreate.js
index 2c1e071..d08c3af 100644
--- a/wikimetrics/static/js/jobCreate.js
+++ b/wikimetrics/static/js/jobCreate.js
@@ -34,7 +34,16 @@
         },
         
         save: function(formElement){
+            if (site.hasValidationErrors()){
+                site.showWarning('Please configure and click Save 
Configuration for each selected metric.');
+                return;
+            }
+            
             var vm = ko.dataFor(formElement);
+            if (vm.request().responses().length == 0){
+                site.showWarning('Please select at least one cohort and one 
metric.');
+                return;
+            }
             var form = $(formElement);
             var data = ko.toJSON(vm.request().responses);
             data = JSON.parse(data);
@@ -47,7 +56,7 @@
             $.ajax({ type: 'post', url: form.attr('action'), data: {responses: 
data} })
                 .done(site.handleWith(function(response){
                     // should redirect to the jobs page, so show an error 
otherwise
-                    site.showError('unexpected response: ' + response);
+                    site.showWarning('Unexpected: ' + 
JSON.stringify(response));
                 }))
                 .fail(site.failure);
         },
@@ -60,7 +69,12 @@
             
             $.ajax({ type: 'post', url: form.attr('action'), data: data })
                 .done(site.handleWith(function(response){
-                    metric.configure(htmlToReplaceWith);
+                    metric.configure(response);
+                    if (site.hasValidationErrors()){
+                        site.showWarning('The configuration was not all valid. 
 Please check all the metrics below.');
+                    } else {
+                        site.showSuccess('Configuration Saved');
+                    }
                 }))
                 .fail(site.failure);
         },
diff --git a/wikimetrics/static/js/site.js b/wikimetrics/static/js/site.js
index 97ca2c1..2a29e6b 100644
--- a/wikimetrics/static/js/site.js
+++ b/wikimetrics/static/js/site.js
@@ -32,6 +32,7 @@
         site.showMessage(message, 'success');
     },
     showMessage: function (message, category){
+        $('.site-messages').children().remove();
         if (!site.messageTemplate){
             site.messageTemplate = $('.messageTemplate').html();
         }
@@ -48,5 +49,10 @@
     
     failure: function (error){
         site.showError(error);
+        console.log(error);
+    },
+    
+    hasValidationErrors: function(){
+        return $('li.text-error').length > 0;
     },
 };

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

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

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

Reply via email to