Erosen has uploaded a new change for review.

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


Change subject: makes NamespaceEdits namespace parameter a custom wtf Field 
subclass, and adds testing code to deal with instantiating wtforms outside of 
the request context.  current commit still fails tests which use a custom field 
type with celery, as app context is n
......................................................................

makes NamespaceEdits namespace parameter a custom wtf Field subclass, and adds 
testing code to deal with instantiating wtforms outside of the request context. 
 current commit still fails tests which use a custom field type with celery, as 
app context is not accessible in celery tasks

Change-Id: I854cb1c69d0e28d63793e762753d272368fd3153
---
M tests/fixtures.py
M tests/test_metrics/test_namespace_edits.py
M wikimetrics/configurables.py
M wikimetrics/metrics/namespace_edits.py
M wikimetrics/models/metric_job.py
5 files changed, 64 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics 
refs/changes/79/71579/1

diff --git a/tests/fixtures.py b/tests/fixtures.py
index e328a84..682a927 100644
--- a/tests/fixtures.py
+++ b/tests/fixtures.py
@@ -156,7 +156,12 @@
 
 
 class QueueTest(unittest.TestCase):
-    pass
+    
+    def setUp(self):
+        pass
+    
+    def tearDown(self):
+        pass
 
 
 class QueueDatabaseTest(QueueTest, DatabaseTest):
diff --git a/tests/test_metrics/test_namespace_edits.py 
b/tests/test_metrics/test_namespace_edits.py
index 3e2f6c1..77beb31 100644
--- a/tests/test_metrics/test_namespace_edits.py
+++ b/tests/test_metrics/test_namespace_edits.py
@@ -1,14 +1,22 @@
 from nose.tools import assert_true
 from tests.fixtures import DatabaseTest, QueueDatabaseTest
 
+from wikimetrics import app
 from wikimetrics.metrics import NamespaceEdits
 from wikimetrics.models import Cohort, MetricJob
 
 
 class NamespaceEditsDatabaseTest(DatabaseTest):
+    
     def setUp(self):
         super(NamespaceEditsDatabaseTest, self).setUp()
+        self.ctx = app.test_request_context()
+        self.ctx.push()
     
+    def tearDown(self):
+        self.ctx.pop()
+        super(NamespaceEditsDatabaseTest, self).tearDown()
+   
     def test_finds_edits(self):
         cohort = self.session.query(Cohort).filter_by(name='test').one()
         
@@ -29,8 +37,17 @@
         assert_true(results[3] == 0, 'Andrew had not 0 edits')
 
 
-class NamesapceEditsFullTest(QueueDatabaseTest):
+class NamespaceEditsFullTest(QueueDatabaseTest):
     
+    def setUp(self):
+        super(NamespaceEditsFullTest, self).setUp()
+        self.ctx = app.test_request_context()
+        #self.ctx.push()
+    
+    def tearDown(self):
+        #self.ctx.pop()
+        super(NamespaceEditsFullTest, self).tearDown()
+        
     def test_namespace_edits(self):
         cohort = self.session.query(Cohort).filter_by(name='test').one()
         
@@ -46,7 +63,7 @@
         cohort = self.session.query(Cohort).filter_by(name='test').one()
         
         namespaces = [3]
-        metric = NamespaceEdits(namespaces)
+        metric = NamespaceEdits(namespaces=namespaces)
         job = MetricJob(metric, list(cohort), 'enwiki')
         results = job.run.delay(job).get()
         
@@ -57,7 +74,7 @@
         cohort = self.session.query(Cohort).filter_by(name='test').one()
         
         namespaces = []
-        metric = NamespaceEdits(namespaces)
+        metric = NamespaceEdits(namespaces=namespaces)
         job = MetricJob(metric, list(cohort), 'enwiki')
         results = job.run.delay(job).get()
         
diff --git a/wikimetrics/configurables.py b/wikimetrics/configurables.py
index 9b57bb3..da54c31 100644
--- a/wikimetrics/configurables.py
+++ b/wikimetrics/configurables.py
@@ -66,9 +66,19 @@
     from celery import Celery
     
     # create and configure celery app
+ 
     global queue
     queue = Celery('wikimetrics', include=['wikimetrics'])
     config_object = create_object_from_config_file(args.celery_config)
     queue.config_from_object(config_object)
     if args.override_config:
         queue.config_from_object(args.override_config)
+    
+    # makes Flask app context available to celery tasks
+    TaskBase = queue.Task
+    class ContextTask(TaskBase):
+        abstract = True
+        def __call__(self, *args, **kwargs):
+            with app.app_context():
+                return TaskBase.__call__(self, *args, **kwargs)
+    queue.Task = ContextTask
diff --git a/wikimetrics/metrics/namespace_edits.py 
b/wikimetrics/metrics/namespace_edits.py
index 60b9ecf..f5b9a45 100644
--- a/wikimetrics/metrics/namespace_edits.py
+++ b/wikimetrics/metrics/namespace_edits.py
@@ -9,6 +9,29 @@
     'NamespaceEdits',
 ]
 
+class CommaSeparatedIntegerListField(wtf.Field):
+    
+    
+    print 'parsing CommaSeparatedIntegerListField.__iter__()'
+    def __iter__(self):
+        return iter(self.data)
+    
+    widget = wtf.TextInput()
+    
+    def _value(self):
+        """ overrides wtforms representation which is sends to server """
+        if self.data:
+            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(',')]
+        else:
+            self.data = [] 
+
 
 class NamespaceEdits(Metric):
     """
@@ -33,7 +56,8 @@
     label       = 'Edits'
     description = 'Compute the number of edits in a specific namespace of a 
mediawiki project'
     
-    namespaces = wtf.IntegerField(default=0, description='0, 2, 4, etc.')
+    #namespaces = wtf.IntegerField(default=[1], description='0, 2, 4, etc.')
+    namespaces = CommaSeparatedIntegerListField(default = [0], description='0, 
2, 4, etc.')
     
     def __call__(self, user_ids, session):
         """
diff --git a/wikimetrics/models/metric_job.py b/wikimetrics/models/metric_job.py
index a854f55..02621be 100644
--- a/wikimetrics/models/metric_job.py
+++ b/wikimetrics/models/metric_job.py
@@ -1,5 +1,5 @@
 import job
-from wikimetrics.configurables import queue, db
+from wikimetrics.configurables import queue, db, app
 
 __all__ = [
     'MetricJob',
@@ -21,4 +21,5 @@
     @queue.task
     def run(self):
         session = db.get_mw_session(self.project)
-        return self.metric(self.user_ids, session)
+        with app.test_request_context():
+            return self.metric(self.user_ids, session)

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

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

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

Reply via email to