Adds a json api 'lanes_for' feature
authorDiane Trout <diane@caltech.edu>
Thu, 28 Jan 2010 23:59:57 +0000 (23:59 +0000)
committerDiane Trout <diane@caltech.edu>
Thu, 28 Jan 2010 23:59:57 +0000 (23:59 +0000)
htsworkflow/frontend/experiments/experiments.py
htsworkflow/frontend/experiments/fixtures/test_flowcells.json
htsworkflow/frontend/experiments/tests.py
htsworkflow/frontend/experiments/urls.py

index 7f492a6fc85e174edefb55303e25fa403345404e..3e17e04d873b39721c2d6296b772666cc9df2b87 100755 (executable)
@@ -13,10 +13,10 @@ from django.core.exceptions import ObjectDoesNotExist
 from django.core.mail import send_mail, mail_admins
 from django.http import HttpResponse, Http404
 
-from htsworkflow.frontend import settings
-from htsworkflow.frontend.experiments.models import FlowCell, DataRun
-from htsworkflow.frontend.samples.models import Library
 from htsworkflow.frontend.auth import require_api_key
+from htsworkflow.frontend import settings
+from htsworkflow.frontend.experiments.models import FlowCell, DataRun, Lane
+from htsworkflow.frontend.samples.models import Library, HTSUser
 
 def flowcell_information(flowcell_id):
     """
@@ -80,7 +80,43 @@ def flowcell_json(request, fc_id):
     
     fc_json = json.dumps(fc_dict)
     return HttpResponse(fc_json, mimetype = 'application/json')
+
+def lanes_for(username=None):
+    """
+    Given a user id try to return recent lanes as a list of dictionaries
+    """
+    query = {}
+    if username is not None:
+        user = HTSUser.objects.get(username=username)        
+        query.update({'library__affiliations__users__id': user.id})
+        
+    lanes = Lane.objects.filter(**query).order_by('-flowcell__run_date')
+    
+    result = []
+    for l in lanes:
+        result.append({ 'flowcell': l.flowcell.flowcell_id,
+                        'run_date': l.flowcell.run_date.isoformat(),
+                        'lane_number': l.lane_number,
+                        'library': l.library.id,
+                        'comment': l.comment})
+    return result
+
+def lanes_for_json(request, username):
+    """
+    Format lanes for a user
+    """
+    require_api_key(request)
+
+    try:
+        result = lanes_for(username)
+    except ObjectDoesNotExist, e:
+        raise Http404
+    
+    #convert query set to python structure
     
+    result_json = json.dumps(result)
+    return HttpResponse(result_json, mimetype='application/json')
+                 
 def updStatus(request):
     output=''
     user = 'none'
index 41c59f5ccf4f4f5c8ee23d6e6c3ea4b669083246..8ac03803d17b5f6abbeed3727e8fdcc345f9ecef 100644 (file)
@@ -1,44 +1,4 @@
 [
-    {"pk": 1, "model": "samples.affiliation",
-     "fields": {
-         "users": [],
-         "contact": "group 1",
-         "name": "affiliation 1",
-         "email": "pk1@example.com"
-         }
-     },
-    {"pk": 2, "model": "samples.affiliation",
-     "fields": {
-         "users": [],
-         "contact": "group 2",
-         "name": "affiliation 2",
-         "email": "pk2@example.com"
-         }
-     },
-    {"pk": 3, "model": "samples.affiliation",
-     "fields": {
-         "users": [],
-         "contact": "group 3",
-         "name": "affiliation 3",
-         "email": "pk3@example.com"
-         }
-     },
-    {"pk": 4, "model": "samples.affiliation",
-     "fields": {
-         "users": [],
-         "contact": "group 4",
-         "name": "affiliation 4",
-         "email": "pk1@example.com"
-         }
-     },
-    {"pk": 5, "model": "samples.affiliation",
-     "fields": {
-         "users": [],
-         "contact": "group 5",
-         "name": "affiliation 5",
-         "email": "pk5@example.com"
-         }
-     },
     {"pk": 5, "model": "auth.user", 
    "fields": {
        "username": "test",
        "date_joined": "2009-01-01 00:01:01"
        }
    },
-  {"pk": 6, "model": "auth.user", 
+   {"pk": 5, "model": "samples.htsuser",
+    "fields" : {}
+   },
+   {"pk": 6, "model": "auth.user", 
    "fields": {
        "username": "admintest",
        "first_name": "",
        "date_joined": "2009-01-01 00:01:01"
        }
    },
-  {"pk": 7, "model": "auth.user", 
+   {"pk": 6, "model": "samples.htsuser",
+    "fields" : {}
+   },
+   {"pk": 7, "model": "auth.user", 
    "fields": {
        "username": "supertest",
        "first_name": "",
        "date_joined": "2009-01-01 00:01:01"
        }
    },
+   {"pk": 7, "model": "samples.htsuser",
+    "fields" : {}
+   },
+   {"pk": 1, "model": "samples.affiliation",
+     "fields": {
+         "users": [5],
+         "contact": "group 1",
+         "name": "affiliation 1",
+         "email": "pk1@example.com"
+         }
+     },
+    {"pk": 2, "model": "samples.affiliation",
+     "fields": {
+         "users": [6],
+         "contact": "group 2",
+         "name": "affiliation 2",
+         "email": "pk2@example.com"
+         }
+     },
+    {"pk": 3, "model": "samples.affiliation",
+     "fields": {
+         "users": [],
+         "contact": "group 3",
+         "name": "affiliation 3",
+         "email": "pk3@example.com"
+         }
+     },
+    {"pk": 4, "model": "samples.affiliation",
+     "fields": {
+         "users": [],
+         "contact": "group 4",
+         "name": "affiliation 4",
+         "email": "pk1@example.com"
+         }
+     },
+    {"pk": 5, "model": "samples.affiliation",
+     "fields": {
+         "users": [],
+         "contact": "group 5",
+         "name": "affiliation 5",
+         "email": "pk5@example.com"
+         }
+     },
  {"pk": 1, "model": "experiments.clusterstation", "fields": {"name": "old"}}, 
  {"pk": 2, "model": "experiments.clusterstation", "fields": {"name": "loaner"}}, 
  {"pk": 3, "model": "experiments.clusterstation", "fields": {"name": "new"}}, 
index 6a3278ac9037a57acf7ebc95c9fc7e6c70eb412e..9088b46144f3f7066e1e36482ada58346be63502 100644 (file)
@@ -7,6 +7,7 @@ except ImportError, e:
 import sys
 
 from django.core import mail
+from django.core.exceptions import ObjectDoesNotExist
 from django.test import TestCase
 from htsworkflow.frontend.experiments import models
 from htsworkflow.frontend.experiments import experiments
@@ -124,6 +125,45 @@ class ExperimentsTestCases(TestCase):
             self.failUnlessEqual(library_id, expected_ids[i])
             self.failUnlessEqual(input_field['value'], library_id)
 
+    def test_lanes_for(self):
+        """
+        Check the code that packs the django objects into simple types.
+        """
+        user = 'test'
+        lanes = experiments.lanes_for(user)
+        self.failUnlessEqual(len(lanes), 5)
+
+        response = self.client.get('/experiments/lanes_for/%s/json' % (user,), apidata)
+        lanes_json = json.loads(response.content)
+        self.failUnlessEqual(len(lanes), len(lanes_json))
+        for i in range(len(lanes)):
+            self.failUnlessEqual(lanes[i]['comment'], lanes_json[i]['comment'])
+            self.failUnlessEqual(lanes[i]['lane_number'], lanes_json[i]['lane_number'])
+            self.failUnlessEqual(lanes[i]['flowcell'], lanes_json[i]['flowcell'])
+            self.failUnlessEqual(lanes[i]['run_date'], lanes_json[i]['run_date'])
+            
+    def test_lanes_for_no_lanes(self):
+        """
+        Do we get something meaningful back when the user isn't attached to anything?
+        """
+        user = 'supertest'
+        lanes = experiments.lanes_for(user)
+        self.failUnlessEqual(len(lanes), 0)
+
+        response = self.client.get('/experiments/lanes_for/%s/json' % (user,), apidata)
+        lanes_json = json.loads(response.content)
+
+    def test_lanes_for_no_user(self):
+        """
+        Do we get something meaningful back when its the wrong user
+        """
+        user = 'not a real user'
+        self.failUnlessRaises(ObjectDoesNotExist, experiments.lanes_for, user)
+
+        response = self.client.get('/experiments/lanes_for/%s/json' % (user,), apidata)
+        self.failUnlessEqual(response.status_code, 404)
+
+
 class TestEmailNotify(TestCase):
     fixtures = ['test_flowcells.json']
 
index bc41e04ee3e7390f8abbb3fb7b3803babd99b2ca..5119e9d3c14006b741586f400bae4fef5f6a6b35 100755 (executable)
@@ -5,6 +5,7 @@ urlpatterns = patterns('',
     #(r'^liblist$', 'htsworkflow.frontend.experiments.views.test_Libs'),
     #(r'^(?P<run_folder>.+)/$', 'gaworkflow.frontend.experiments.views.detail'),
     (r'^config/(?P<fc_id>.+)/json$', 'htsworkflow.frontend.experiments.experiments.flowcell_json'),
+    (r'^lanes_for/(?P<username>.+)/json$', 'htsworkflow.frontend.experiments.experiments.lanes_for_json'),
     (r'^fcsheet/(?P<fcid>.+)/$', 'htsworkflow.frontend.experiments.views.makeFCSheet'),
     (r'^updStatus$', 'htsworkflow.frontend.experiments.experiments.updStatus'),
     (r'^getConfile$', 'htsworkflow.frontend.experiments.experiments.getConfile'),