From f8d9bab74f8bf34bf69d08b28e406e1b2b73586d Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Tue, 27 Sep 2011 14:53:35 +0000
Subject: [PATCH] Add HEAD method to /Reference download

git-svn-id: https://humgenprojects.lumc.nl/svn/mutalyzer/branches/refactor-mutalyzer-branch@369 eb6bd6ab-9ccd-42b9-aceb-e2899b4a52f1
---
 mutalyzer/website.py  | 31 +++++++++++++++++++++++++++++++
 tests/test_website.py | 30 +++++++++++++++++++++++++-----
 2 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/mutalyzer/website.py b/mutalyzer/website.py
index fc0cce2d..03e946a0 100644
--- a/mutalyzer/website.py
+++ b/mutalyzer/website.py
@@ -270,6 +270,37 @@ class Reference:
         web.header('Content-Type', 'text/plain')
         web.header('Content-Disposition', 'attachment; filename="%s"' % file)
         return handle.read()
+
+    def HEAD(self, file):
+        """
+        Do the same as in the GET case, but don't actually bunzip and send the
+        file, just check if it exists.
+
+        @arg file: Filename to download from cache.
+        @type file: string
+
+        This is used by LOVD to quickly check if a reference file is in the
+        cache. If it isn't, it will resubmit it.
+        Of course a more proper solution here would be to have some webservice
+        method which checks if the GenBank file is in the cache *or* can be
+        reconstructed from the information in the database. Because if the
+        latter is the case, Mutalyzer will add it to the cache on the fly.
+        """
+        file_path = os.path.join(config.Retriever.cache, '%s.bz2' % file)
+        if not os.path.isfile(file_path):
+            # The following is a hack to return a 404 not found status with
+            # empty body (as is checked by our unit test framework, WebTest).
+            # Just passing nothing, or the empty string, causes web.py to
+            # insert some default 'not found' message.
+            class TrueEmptyString(object):
+                def __str__(self):
+                    return ''
+                def __nonzero__( self):
+                    return True
+            raise web.notfound(message=TrueEmptyString())
+        web.header('Content-Type', 'text/plain')
+        web.header('Content-Disposition', 'attachment; filename="%s"' % file)
+        return ''
 #Reference
 
 
diff --git a/tests/test_website.py b/tests/test_website.py
index a6692495..1121cadd 100644
--- a/tests/test_website.py
+++ b/tests/test_website.py
@@ -626,11 +626,6 @@ facilisi."""
     def test_reference(self):
         """
         Test if reference files are cached.
-
-        @todo: This test doesn't work, since for every request a new
-            temporary cache directory is created by the webserver instance
-            and thus the cached file from request i cannot be re-used in
-            request i+1.
         """
         r = self.app.get('/check')
         form = r.forms[0]
@@ -645,3 +640,28 @@ facilisi."""
         assert_equal(r.content_type, 'text/plain')
         assert_equal(r.content_length, 26427)
         r.mustcontain('ggaaaaagtc tctcaaaaaa cctgctttat')
+
+    def test_reference_head(self):
+        """
+        Test if reference files are cached, by issuing a HEAD request.
+
+        Note: The WebTest module also checks that the response to a HEAD
+            request is empty, as it should be.
+        """
+        r = self.app.get('/check')
+        form = r.forms[0]
+        form['mutationName'] = 'AB026906.1:c.274G>T'
+        r = form.submit()
+        r.mustcontain('0 Errors',
+                      '1 Warning',
+                      'Raw variant 1: substitution at 7872',
+                      '<a href="#bottom" class="hornav">go to bottom</a>',
+                      '<input value="AB026906.1:c.274G&gt;T" type="text" name="mutationName" style="width:100%">')
+        r = self.app.head('/Reference/AB026906.1.gb')
+        assert_equal(r.content_type, 'text/plain')
+
+    def test_reference_head_none(self):
+        """
+        Test if non-existing reference files gives a 404 on a HEAD request.
+        """
+        r = self.app.head('/Reference/AB026906.78.gb', status=404)
-- 
GitLab