Follow the flowcell link from the library page.
[htsworkflow.git] / htsworkflow / frontend / experiments / views.py
index 92f6cbd9227bcffa2eb465177f4d226c3ccc30ba..e1204135424dca941ca1d7a0044337b719b0ca67 100755 (executable)
@@ -1,18 +1,25 @@
 # Create your views here.
+from datetime import datetime
+import os
+
 #from django.template import Context, loader
 #shortcut to the above modules
 from django.contrib.auth.decorators import user_passes_test
+from django.conf import settings
 from django.core.exceptions import ObjectDoesNotExist
 from django.core.mail import EmailMessage, mail_managers
 from django.http import HttpResponse
 from django.shortcuts import render_to_response, get_object_or_404
-from django.template import Context
+from django.template import RequestContext
 from django.template.loader import get_template
 
-from htsworkflow.frontend.experiments.models import *
+from htsworkflow.frontend.experiments.models import DataRun, DataFile, FlowCell
 from htsworkflow.frontend.experiments.experiments import \
      estimateFlowcellDuration, \
-     makeUserLaneMap
+     estimateFlowcellTimeRemaining, \
+     roundToDays, \
+     getUsersForFlowcell, \
+     makeEmailLaneMap
 
 def index(request):
     all_runs = DataRun.objects.order_by('-run_start_time')
@@ -61,57 +68,58 @@ def startedEmail(request, pk):
     else:
         bcc_managers = False
 
-    user_lane = makeUserLaneMap(fc)
-    estimate_low, estimate_high = estimateFlowcellDuration(fc)
+    email_lane = makeEmailLaneMap(fc)
+    flowcell_users = getUsersForFlowcell(fc)
+    estimate = estimateFlowcellTimeRemaining(fc)
+    estimate_low, estimate_high = roundToDays(estimate)
     email_verify = get_template('experiments/email_preview.html')
-    email_template = get_template('experiments/started_email.html')
+    email_template = get_template('experiments/started_email.txt')
     sender = settings.NOTIFICATION_SENDER
 
     warnings = []
     emails = []
+
+    emailless_users = []
+    for user in flowcell_users:
+        # provide warning
+        if user.email is None or len(user.email) == 0:
+            warnings.append((user.admin_url(), user.username))
+    user=None
     
-    for user in user_lane.keys():
+    for user_email in email_lane.keys():
         sending = ""
         # build body
-        context = Context({u'flowcell': fc,
-                   u'lanes': user_lane[user],
-                   u'runfolder': 'blank',
-                   u'finish_low': estimate_low,
-                   u'finish_high': estimate_high,
-                   u'user_admin': user.admin_url(),
-                  })
+        context = RequestContext(request,
+                                 {u'flowcell': fc,
+                                  u'lanes': email_lane[user_email],
+                                  u'runfolder': 'blank',
+                                  u'finish_low': estimate_low,
+                                  u'finish_high': estimate_high,
+                                  u'now': datetime.now(),        
+                                  })
 
         # build view
-        subject = "Flowcell  %s" % ( fc.flowcell_id )
+        subject = "Flowcell %s" % ( fc.flowcell_id )
         body = email_template.render(context)
 
-        # provide warning
-        has_email = True
-        if user.email is None or len(user.email) == 0:
-            warnings.append((user.admin_url(), user.username))
-            has_email = False
-            
         if send:
-            if has_email:
-                email = EmailMessage(subject, body, sender, to=[user.email])
-                if bcc_managers:
-                    print 'bcc_managers', bcc_managers
-                    email.bcc = settings.MANAGERS
-                print email.to, email.bcc
-                email.send()
-                sending = "sent"
-            else:
-                print settings.MANAGERS
-                mail_managers("Couldn't send to "+user.username, body)
-                sending = "bounced to managers"
-
-        emails.append((user.email, subject, body, sending))
-
-    verify_context = Context({
-        'send': send,
-        'warnings': warnings,
-        'emails': emails,
-        'from': sender,
+            email = EmailMessage(subject, body, sender, to=[user_email])
+            if bcc_managers:
+                email.bcc = settings.MANAGERS
+            email.bcc.append(settings.NOTIFICATION_BCC)
+            email.send()
+
+        emails.append((user_email, subject, body, sending))
+
+    verify_context = RequestContext(
+        request,
+        { 'emails': emails,
+          'flowcell': fc,
+          'from': sender,
+          'send': send,
+          'site_managers': settings.MANAGERS,
+          'title': fc.flowcell_id,
+          'warnings': warnings,
         })
     return HttpResponse(email_verify.render(verify_context))
     
@@ -119,3 +127,45 @@ def finishedEmail(request, pk):
     """
     """
     return HttpResponse("I've got nothing.")
+
+
+def flowcell_detail(request, flowcell_id):
+    fc = get_object_or_404(FlowCell, flowcell_id__startswith=flowcell_id)
+    fc.update_data_runs()
+
+    context = RequestContext(request,
+                             {'flowcell': fc})
+    
+    return render_to_response('experiments/flowcell_detail.html',
+                              context)
+
+def flowcell_lane_detail(request, flowcell_id, lane_number):
+    fc = get_object_or_404(FlowCell, flowcell_id__startswith=flowcell_id)
+    lane = get_object_or_404(fc.lane_set, lane_number=lane_number)
+    
+    fc.update_data_runs()
+
+    context = RequestContext(request,
+                             {'lib': lane.library,
+                              'lane': lane,
+                              'flowcell': fc})
+    
+    return render_to_response('experiments/flowcell_lane_detail.html',
+                              context)
+
+def read_result_file(self, key):
+    """Return the contents of filename if everything is approved
+    """
+    data_file = get_object_or_404(DataFile, random_key = key)
+    
+    mimetype = 'application/octet-stream'
+    if data_file.file_type.mimetype is not None:
+        mimetype = data_file.file_type.mimetype
+
+    if os.path.exists(data_file.pathname):
+        return HttpResponse(open(data_file.pathname,'r'),
+                            mimetype=mimetype)
+
+    raise Http404
+      
+