Initial port to python3
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
index 28a3e1ea73e7794e1b5988bc189484b17537d9b8..5ddad93c02fdb99c6366ee4bc21a73f95681d24e 100644 (file)
@@ -1,19 +1,21 @@
 # Create your views here.
-import StringIO
+import io
 import logging
 import os
 import sys
 
 try:
     import json
-except ImportError, e:
+except ImportError as e:
     import simplejson as json
 
-from django.contrib.csrf.middleware import csrf_exempt
+from django.views.decorators.csrf import csrf_exempt
 from htsworkflow.frontend.auth import require_api_key
 from htsworkflow.frontend.experiments.models import FlowCell, Lane, LANE_STATUS_MAP
-from htsworkflow.frontend.samples.changelist import ChangeList
+from htsworkflow.frontend.experiments.admin import LaneOptions
+from htsworkflow.frontend.samples.changelist import HTSChangeList
 from htsworkflow.frontend.samples.models import Antibody, Library, Species, HTSUser
+from htsworkflow.frontend.samples.admin import LibraryOptions
 from htsworkflow.frontend.samples.results import get_flowcell_result_dict
 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
 from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
@@ -82,26 +84,29 @@ def create_library_context(cl):
            summary['amplified_from'] = ''
        lanes_run = count_lanes(lib.lane_set)
        # suppress zeros
-       for row in xrange(len(lanes_run)):
-           for col in xrange(len(lanes_run[row])):
+       for row in range(len(lanes_run)):
+           for col in range(len(lanes_run[row])):
                if lanes_run[row][col] == 0:
                    lanes_run[row][col] = ''
        summary['lanes_run'] = lanes_run
        summary['is_archived'] = lib.is_archived()
        records.append(summary)
-    cl.result_count = unicode(cl.paginator._count)
+    cl.result_count = str(cl.paginator._count)
     return {'library_list': records }
 
+
 def library(request, todo_only=False):
     queryset = Library.objects.filter(hidden__exact=0)
+    filters = {'hidden__exact': 0}
     if todo_only:
-        queryset = queryset.filter(lane=None)
+        filters[lane] = None
     # build changelist
-    fcl = ChangeList(request, Library,
+    fcl = HTSChangeList(request, Library,
         list_filter=['affiliations', 'library_species'],
         search_fields=['id', 'library_name', 'amplified_from_sample__id'],
         list_per_page=200,
-        queryset=queryset
+        model_admin=LibraryOptions(Library, None),
+        extra_filters=filters
     )
 
     context = { 'cl': fcl, 'title': 'Library Index', 'todo_only': todo_only}
@@ -110,9 +115,11 @@ def library(request, todo_only=False):
     c = RequestContext(request, context)
     return HttpResponse( t.render(c) )
 
+
 def library_not_run(request):
     return library(request, todo_only=True)
 
+
 def library_to_flowcells(request, lib_id):
     """
     Display information about all the flowcells a library has been run on.
@@ -161,10 +168,11 @@ def lanes_for(request, username=None):
     if username is not None:
         user = HTSUser.objects.get(username=username)
         query.update({'library__affiliations__users__id':user.id})
-    fcl = ChangeList(request, Lane,
+    fcl = HTSChangeList(request, Lane,
         list_filter=[],
         search_fields=['flowcell__flowcell_id', 'library__id', 'library__library_name'],
         list_per_page=200,
+        model_admin=LaneOptions,
         queryset=Lane.objects.filter(**query)
     )
 
@@ -306,7 +314,7 @@ def _summary_stats(flowcell_id, lane_id, library_id):
             read = key.read-1 if key.read is not None else 0
             try:
                 eland_summary.clusters = gerald_summary[read][key.lane].cluster
-            except IndexError as e:
+            except (IndexError, KeyError) as e:
                 eland_summary.clustes = None
             eland_summary.cycle_width = cycle_width
             if hasattr(eland_summary, 'genome_map'):
@@ -371,7 +379,7 @@ def _make_eland_results(flowcell_id, lane_number, interesting_flowcells):
         storage_ids = ', '.join([ '<a href="/inventory/%s/">%s</a>' % (s,s) for s in storage_id_list ])
 
     results = []
-    for cycle in cur_fc.keys():
+    for cycle in list(cur_fc.keys()):
         result_path = cur_fc[cycle]['eland_results'].get(lanes[0], None)
         result_link = make_result_link(fc_id, cycle, lanes[0], result_path)
         results.append({'flowcell_id': fc_id,
@@ -451,7 +459,7 @@ def library_dict(library_id):
     """
     try:
         lib = Library.objects.get(id = library_id)
-    except Library.DoesNotExist, e:
+    except Library.DoesNotExist as e:
         return None
 
     #lane_info = lane_information(lib.lane_set)
@@ -459,6 +467,7 @@ def library_dict(library_id):
     for lane in lib.lane_set.all():
         lane_info.append( {'flowcell':lane.flowcell.flowcell_id,
                            'lane_number': lane.lane_number,
+                           'lane_id': lane.id,
                            'paired_end': lane.flowcell.paired_end,
                            'read_length': lane.flowcell.read_length,
                            'status_code': lane.status,
@@ -549,5 +558,3 @@ def user_profile(request):
     context.update(SAMPLES_CONTEXT_DEFAULTS)
     return render_to_response('registration/profile.html', context,
                               context_instance=RequestContext(request))
-
-