From: Diane Trout Date: Wed, 23 Sep 2009 19:09:58 +0000 (+0000) Subject: Return species information as part of the flowcell json information. X-Git-Tag: 0.3.2~19 X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=htsworkflow.git;a=commitdiff_plain;h=45bea53077b4e46618320beee76682d441e0a1c7 Return species information as part of the flowcell json information. Additionally instead of using django authentication use an apikey for authenticating access to the json data. Currently the apikey is just a value stored in the settings.py file (DEFAULT_API_KEY), but in the future could be linked to users. --- diff --git a/htsworkflow/frontend/auth.py b/htsworkflow/frontend/auth.py new file mode 100644 index 0000000..4df771b --- /dev/null +++ b/htsworkflow/frontend/auth.py @@ -0,0 +1,22 @@ +""" +Define some alternate authentication methods +""" +from django.core.exceptions import PermissionDenied + +from htsworkflow.frontend import settings + +apidata = {'apiid': u'0', 'apikey': settings.DEFAULT_API_KEY} + +def require_api_key(request): + # make sure we have the api component + if not (request.REQUEST.has_key('apiid') or request.REQUEST.has_key('apikey')): + raise PermissionDenied + + # make sure the id and key are right + if request.REQUEST['apiid'] == apidata['apiid'] and \ + request.REQUEST['apikey'] == apidata['apikey']: + return True + else: + raise PermissionDenied + + diff --git a/htsworkflow/frontend/experiments/experiments.py b/htsworkflow/frontend/experiments/experiments.py index f9144b0..331bdde 100755 --- a/htsworkflow/frontend/experiments/experiments.py +++ b/htsworkflow/frontend/experiments/experiments.py @@ -16,6 +16,7 @@ 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 def flowcell_information(flowcell_id): """ @@ -35,6 +36,7 @@ def flowcell_information(flowcell_id): 'lane_number': int(lane.lane_number), 'library_name': lane.library.library_name, 'library_id': lane.library.library_id, + 'library_species': lane.library.library_species.scientific_name, 'pM': float(lane.pM), } info = { @@ -56,11 +58,12 @@ def flowcell_information(flowcell_id): return info -@login_required def flowcell_json(request, fc_id): """ Return a JSON blob containing enough information to generate a config file. """ + require_api_key(request) + fc_dict = flowcell_information(fc_id) if fc_dict is None: diff --git a/htsworkflow/frontend/experiments/fixtures/test_flowcells.json b/htsworkflow/frontend/experiments/fixtures/test_flowcells.json index 0987679..1ce5250 100644 --- a/htsworkflow/frontend/experiments/fixtures/test_flowcells.json +++ b/htsworkflow/frontend/experiments/fixtures/test_flowcells.json @@ -92,7 +92,7 @@ "library_name": "Paired End Pfl #3 MP 7/24/9 a", "creation_date": "2009-08-06", "cell_line": 1, - "library_species": 22, + "library_species": 2, "library_type": 2, "made_by": "Lorian", "affiliations": [ diff --git a/htsworkflow/frontend/experiments/tests.py b/htsworkflow/frontend/experiments/tests.py index 6f4bc3d..a4a5315 100644 --- a/htsworkflow/frontend/experiments/tests.py +++ b/htsworkflow/frontend/experiments/tests.py @@ -6,6 +6,7 @@ except ImportError, e: from django.test import TestCase from htsworkflow.frontend.experiments import models from htsworkflow.frontend.experiments import experiments +from htsworkflow.frontend.auth import apidata class ExperimentsTestCases(TestCase): fixtures = ['test_flowcells.json'] @@ -36,9 +37,10 @@ class ExperimentsTestCases(TestCase): self.failUnlessEqual(lane_dict['library_name'], lane.library.library_name) self.failUnlessEqual(lane_dict['library_id'], lane.library.library_id) self.failUnlessAlmostEqual(lane_dict['pM'], float(lane.pM)) + self.failUnlessEqual(lane_dict['library_species'], + lane.library.library_species.scientific_name) - self.client.login(username='test', password='BJOKL5kAj6aFZ6A5') - response = self.client.get('/experiments/config/%s/json' % (fc_id,)) + response = self.client.get('/experiments/config/%s/json' % (fc_id,), apidata) # strptime isoformat string = '%Y-%m-%dT%H:%M:%S' fc_json = json.loads(response.content) self.failUnlessEqual(fc_json['flowcell_id'], fc_id) @@ -57,35 +59,38 @@ class ExperimentsTestCases(TestCase): self.failUnlessEqual(lane_dict['library_name'], lane.library.library_name) self.failUnlessEqual(lane_dict['library_id'], lane.library.library_id) self.failUnlessAlmostEqual(lane_dict['pM'], float(lane.pM)) + self.failUnlessEqual(lane_dict['library_species'], + lane.library.library_species.scientific_name) def test_invalid_flowcell(self): """ Make sure we get a 404 if we request an invalid flowcell ID """ - self.client.login(username='test', password='BJOKL5kAj6aFZ6A5') - response = self.client.get('/experiments/config/nottheone/json') + response = self.client.get('/experiments/config/nottheone/json', apidata) self.failUnlessEqual(response.status_code, 404) - def test_not_logged_in(self): + def test_no_key(self): """ Require logging in to retrieve meta data """ response = self.client.get(u'/experiments/config/303TUAAXX/json') - self.failUnlessEqual(response.status_code, 302) + self.failUnlessEqual(response.status_code, 403) def test_library_id(self): """ Library IDs should be flexible, so make sure we can retrive a non-numeric ID """ - self.client.login(username='test', password='BJOKL5kAj6aFZ6A5') - response = self.client.get('/experiments/config/303TUAAXX/json') + response = self.client.get('/experiments/config/303TUAAXX/json', apidata) self.failUnlessEqual(response.status_code, 200) flowcell = json.loads(response.content) self.failUnlessEqual(flowcell['lane_set']['3']['library_id'], 'SL039') - response = self.client.get('/samples/library/SL039/json') + response = self.client.get('/samples/library/SL039/json', apidata) self.failUnlessEqual(response.status_code, 200) library_sl039 = json.loads(response.content) self.failUnlessEqual(library_sl039['library_id'], 'SL039') + +#class RetriveConfigTestCases(TestCase): +# fixtures = ['test_flowcells.json'] diff --git a/htsworkflow/frontend/samples/fixtures/initial_data.json b/htsworkflow/frontend/samples/fixtures/initial_data.json index dbdcc7a..27eeccf 100644 --- a/htsworkflow/frontend/samples/fixtures/initial_data.json +++ b/htsworkflow/frontend/samples/fixtures/initial_data.json @@ -86,14 +86,6 @@ "name": "Multiplexed" } }, - { - "model": "samples.Species", - "pk": 1, - "fields": { - "scientific_name": "Mus musculus", - "common_name": "mouse" - } - }, { "model": "samples.Species", "pk": 2, @@ -110,6 +102,13 @@ "common_name": "" } }, + { + "model": "samples.Species", + "pk": 6, + "fields": { + "scientific_name": "Arabidopsis thaliana" + } + }, { "model": "samples.Species", "pk": 8, @@ -117,6 +116,20 @@ "scientific_name": "Homo sapiens", "common_name": "" } + }, + { + "model": "samples.Species", + "pk": 9, + "fields": { + "scientific_name": "Mus musculus", + "common_name": "mouse" + } + }, + { + "model": "samples.Species", + "pk": 10, + "fields": { + "scientific_name": "Strongylocentrotus purpuratus" + } } - ] diff --git a/htsworkflow/frontend/samples/fixtures/test_samples.json b/htsworkflow/frontend/samples/fixtures/test_samples.json index dba08b6..ae99872 100644 --- a/htsworkflow/frontend/samples/fixtures/test_samples.json +++ b/htsworkflow/frontend/samples/fixtures/test_samples.json @@ -53,7 +53,7 @@ "library_name": "Paired End Pfl #3 MP 7/24/9 a", "creation_date": "2009-08-06", "cell_line": 1, - "library_species": 1, + "library_species": 9, "library_type": 2, "made_by": "Lorian", "affiliations": [ @@ -115,7 +115,7 @@ "library_name": "Paired End Pfl #3 MP 7/24/9", "creation_date": "2009-08-05", "cell_line": 1, - "library_species": 1, + "library_species": 8, "library_type": 2, "made_by": "Lorian", "affiliations": [ diff --git a/htsworkflow/frontend/samples/tests.py b/htsworkflow/frontend/samples/tests.py index 595727e..d85a466 100644 --- a/htsworkflow/frontend/samples/tests.py +++ b/htsworkflow/frontend/samples/tests.py @@ -18,6 +18,8 @@ from htsworkflow.frontend.samples.views import \ library_dict, \ library_json +from htsworkflow.frontend.auth import apidata + # The django test runner flushes the database between test suites not cases, # so to be more compatible with running via nose we flush the database tables # of interest before creating our sample data. @@ -123,9 +125,8 @@ class SampleWebTestCase(TestCase): for lib in Library.objects.all(): lib_dict = library_dict(lib.library_id) - self.client.login(username='test', password='BJOKL5kAj6aFZ6A5') url = '/samples/library/%s/json' % (lib.library_id,) - lib_response = self.client.get(url) + lib_response = self.client.get(url, apidata) self.failUnlessEqual(lib_response.status_code, 200) lib_json = json.loads(lib_response.content) @@ -164,17 +165,15 @@ class SampleWebTestCase(TestCase): """ Make sure we get a 404 if we request an invalid library id """ - self.client.login(username='test', password='BJOKL5kAj6aFZ6A5') - response = self.client.get('/samples/library/nottheone/json') + response = self.client.get('/samples/library/nottheone/json', apidata) self.failUnlessEqual(response.status_code, 404) - def test_library_not_logged_in(self): + def test_library_no_key(self): """ Make sure we get a 302 if we're not logged in """ response = self.client.get('/samples/library/10981/json') - self.failUnlessEqual(response.status_code, 302) - self.client.login(username='test', password='BJOKL5kAj6aFZ6A5') - response = self.client.get('/samples/library/10981/json') + self.failUnlessEqual(response.status_code, 403) + response = self.client.get('/samples/library/10981/json', apidata) self.failUnlessEqual(response.status_code, 200) diff --git a/htsworkflow/frontend/samples/views.py b/htsworkflow/frontend/samples/views.py index f42c8bc..b0a61f4 100644 --- a/htsworkflow/frontend/samples/views.py +++ b/htsworkflow/frontend/samples/views.py @@ -7,6 +7,7 @@ try: except ImportError, e: import simplejson as json +from htsworkflow.frontend.auth import require_api_key from htsworkflow.frontend.experiments.models import FlowCell from htsworkflow.frontend.samples.changelist import ChangeList from htsworkflow.frontend.samples.models import Library @@ -491,11 +492,11 @@ def library_dict(library_id): info['library_type'] = lib.library_type.name return info -@login_required def library_json(request, library_id): """ Return a json formatted library dictionary """ + require_api_key(request) # what validation should we do on library_id? lib = library_dict(library_id) diff --git a/htsworkflow/frontend/settings.py b/htsworkflow/frontend/settings.py index 91eccf9..a141283 100644 --- a/htsworkflow/frontend/settings.py +++ b/htsworkflow/frontend/settings.py @@ -141,6 +141,9 @@ ADMIN_MEDIA_PREFIX = '/media/' # Make this unique, and don't share it with anybody. SECRET_KEY = '(ekv^=gf(j9f(x25@a7r+8)hqlz%&_1!tw^75l%^041#vi=@4n' +# some of our urls need an api key +DEFAULT_API_KEY = 'n7HsXGHIi0vp9j5u4TIRJyqAlXYc4wrH' + # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.load_template_source',