1 from __future__ import absolute_import, print_function, unicode_literals
3 # Create your views here.
4 from datetime import datetime
7 #from django.template import Context, loader
8 #shortcut to the above modules
9 from django.contrib.admin.sites import site as default_admin_site
10 from django.contrib.auth.decorators import user_passes_test
11 from django.conf import settings
12 from django.core.exceptions import ObjectDoesNotExist
13 from django.core.mail import EmailMessage
14 from django.http import HttpResponse, Http404
15 from django.shortcuts import render, render_to_response, get_object_or_404
16 from django.template import RequestContext
17 from django.template.loader import get_template
19 from .models import SequencingRun, DataFile, FlowCell, Lane, Sequencer
20 from .admin import LaneOptions
21 from .experiments import estimateFlowcellTimeRemaining, roundToDays, \
22 getUsersForFlowcell, \
24 from samples.changelist import HTSChangeList
25 from samples.models import HTSUser
29 all_runs = SequencingRun.objects.order_by('-run_start_time')
30 return render(request, 'experiments/index.html',{'data_run_list': all_runs})
32 def detail(request, run_folder):
33 html_str = '<h2>Exp Track Details Page</h2>'
34 html_str += 'Run Folder: '+run_folder
35 r = get_object_or_404(SequencingRun,run_folder=run_folder)
36 return render(request, 'experiments/detail.html',{'run_f': r})
38 def makeFCSheet(request, fcid):
39 # get Flowcell by input fcid
43 rec = FlowCell.objects.get(flowcell_id=fcid)
44 except ObjectDoesNotExist:
46 lanes = ['1','2','3','4','5','6','7','8']
47 return render(request, 'experiments/flowcellSheet.html',{'fc': rec})
50 @user_passes_test(lambda u: u.is_staff)
51 def startedEmail(request, pk):
53 Create the we have started processing your samples email
55 fc = get_object_or_404(FlowCell, id=pk)
57 send = request.GET.get('send',False)
58 if send in ('1', 'on', 'True', 'true', True):
63 bcc_managers = request.GET.get('bcc', False)
64 if bcc_managers in ('on', '1', 'True', 'true'):
69 email_lane = makeEmailLaneMap(fc)
70 flowcell_users = getUsersForFlowcell(fc)
71 estimate = estimateFlowcellTimeRemaining(fc)
72 estimate_low, estimate_high = roundToDays(estimate)
73 email_verify = get_template('experiments/email_preview.html')
74 email_template = get_template('experiments/started_email.txt')
75 sender = settings.NOTIFICATION_SENDER
81 for user in flowcell_users:
83 if user.email is None or len(user.email) == 0:
84 warnings.append((user.admin_url(), user.username))
87 for user_email in email_lane.keys():
90 context = RequestContext(request,
92 u'lanes': email_lane[user_email],
93 u'runfolder': 'blank',
94 u'finish_low': estimate_low,
95 u'finish_high': estimate_high,
96 u'now': datetime.now(),
100 subject = "Flowcell %s" % ( fc.flowcell_id )
101 body = email_template.render(context)
104 email = EmailMessage(subject, body, sender, to=[user_email])
107 for manager in settings.MANAGERS:
109 notified.add(manager)
110 for user in settings.NOTIFICATION_BCC:
113 email.bcc = list(notified)
116 emails.append((user_email, subject, body, sending))
118 verify_context = RequestContext(
124 'site_managers': settings.MANAGERS,
125 'title': fc.flowcell_id,
126 'warnings': warnings,
128 return HttpResponse(email_verify.render(verify_context))
130 def finishedEmail(request, pk):
133 return HttpResponse("I've got nothing.")
136 def flowcell_detail(request, flowcell_id, lane_number=None):
137 fc = get_object_or_404(FlowCell, flowcell_id__startswith=flowcell_id)
138 fc.update_sequencing_runs()
140 if lane_number is not None:
141 lanes = fc.lane_set.filter(lane_number=lane_number)
143 lanes = fc.lane_set.all()
145 context = {'flowcell': fc, 'lanes': lanes}
147 return render(request, 'experiments/flowcell_detail.html', context)
149 def flowcell_lane_detail(request, lane_pk):
150 lane = get_object_or_404(Lane, id=lane_pk)
151 lane.flowcell.update_sequencing_runs()
154 lane.flowcell.update_sequencing_runs()
155 for run in lane.flowcell.sequencingrun_set.all():
156 files = run.lane_files().get(lane.lane_number, None)
157 sequencingruns.append((run,
161 context = {'lib': lane.library,
163 'flowcell': lane.flowcell,
164 'filtered_sequencingruns': sequencingruns}
166 return render(request, 'experiments/flowcell_lane_detail.html', context)
168 def read_result_file(self, key):
169 """Return the contents of filename if everything is approved
171 data_file = get_object_or_404(DataFile, random_key = key)
173 content_type = 'application/octet-stream'
174 if data_file.file_type.mimetype is not None:
175 content_type = data_file.file_type.mimetype
177 if os.path.exists(data_file.pathname):
178 return HttpResponse(open(data_file.pathname,'rb'),
179 content_type=content_type)
184 def sequencer(request, sequencer_id):
185 sequencer = get_object_or_404(Sequencer, id=sequencer_id)
186 context = {'sequencer': sequencer}
187 return render(request, 'experiments/sequencer.html', context)
190 def lanes_for(request, username=None):
192 Generate a report of recent activity for a user
195 if username is not None:
197 user = HTSUser.objects.get(username=username)
198 query.update({'library__affiliations__users__id': user.id})
199 except HTSUser.DoesNotExist as e:
200 raise Http404('User not found')
202 fcl = HTSChangeList(request, Lane,
203 list_filter=['library__affiliations',
204 'library__library_species'],
205 search_fields=['flowcell__flowcell_id',
207 'library__library_name'],
209 model_admin=LaneOptions(Lane, default_admin_site),
213 context = {'lanes': fcl, 'title': 'Lane Index'}
215 return render(request, 'samples/lanes_for.html', context)