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.auth.decorators import user_passes_test
10 from django.conf import settings
11 from django.core.exceptions import ObjectDoesNotExist
12 from django.core.mail import EmailMessage
13 from django.http import HttpResponse, Http404
14 from django.shortcuts import render, render_to_response, get_object_or_404
15 from django.template import RequestContext
16 from django.template.loader import get_template
18 from .models import SequencingRun, DataFile, FlowCell, Lane, Sequencer
19 from .admin import LaneOptions
20 from .experiments import estimateFlowcellTimeRemaining, roundToDays, \
21 getUsersForFlowcell, \
23 from samples.changelist import HTSChangeList
24 from samples.models import HTSUser
28 all_runs = SequencingRun.objects.order_by('-run_start_time')
29 return render(request, 'experiments/index.html',{'data_run_list': all_runs})
31 def detail(request, run_folder):
32 html_str = '<h2>Exp Track Details Page</h2>'
33 html_str += 'Run Folder: '+run_folder
34 r = get_object_or_404(SequencingRun,run_folder=run_folder)
35 return render(request, 'experiments/detail.html',{'run_f': r})
37 def makeFCSheet(request,fcid):
38 # get Flowcell by input fcid
42 rec = FlowCell.objects.get(flowcell_id=fcid)
43 except ObjectDoesNotExist:
45 lanes = ['1','2','3','4','5','6','7','8']
46 return render(request, 'experiments/flowcellSheet.html',{'fc': rec})
49 @user_passes_test(lambda u: u.is_staff)
50 def startedEmail(request, pk):
52 Create the we have started processing your samples email
54 fc = get_object_or_404(FlowCell, id=pk)
56 send = request.GET.get('send',False)
57 if send in ('1', 'on', 'True', 'true', True):
62 bcc_managers = request.GET.get('bcc', False)
63 if bcc_managers in ('on', '1', 'True', 'true'):
68 email_lane = makeEmailLaneMap(fc)
69 flowcell_users = getUsersForFlowcell(fc)
70 estimate = estimateFlowcellTimeRemaining(fc)
71 estimate_low, estimate_high = roundToDays(estimate)
72 email_verify = get_template('experiments/email_preview.html')
73 email_template = get_template('experiments/started_email.txt')
74 sender = settings.NOTIFICATION_SENDER
80 for user in flowcell_users:
82 if user.email is None or len(user.email) == 0:
83 warnings.append((user.admin_url(), user.username))
86 for user_email in email_lane.keys():
89 context = RequestContext(request,
91 u'lanes': email_lane[user_email],
92 u'runfolder': 'blank',
93 u'finish_low': estimate_low,
94 u'finish_high': estimate_high,
95 u'now': datetime.now(),
99 subject = "Flowcell %s" % ( fc.flowcell_id )
100 body = email_template.render(context)
103 email = EmailMessage(subject, body, sender, to=[user_email])
106 for manager in settings.MANAGERS:
108 notified.add(manager)
109 for user in settings.NOTIFICATION_BCC:
112 email.bcc = list(notified)
115 emails.append((user_email, subject, body, sending))
117 verify_context = RequestContext(
123 'site_managers': settings.MANAGERS,
124 'title': fc.flowcell_id,
125 'warnings': warnings,
127 return HttpResponse(email_verify.render(verify_context))
129 def finishedEmail(request, pk):
132 return HttpResponse("I've got nothing.")
135 def flowcell_detail(request, flowcell_id, lane_number=None):
136 fc = get_object_or_404(FlowCell, flowcell_id__startswith=flowcell_id)
137 fc.update_sequencing_runs()
139 if lane_number is not None:
140 lanes = fc.lane_set.filter(lane_number=lane_number)
142 lanes = fc.lane_set.all()
144 context = RequestContext(request,
148 return render(request, 'experiments/flowcell_detail.html', context)
150 def flowcell_lane_detail(request, lane_pk):
151 lane = get_object_or_404(Lane, id=lane_pk)
152 lane.flowcell.update_sequencing_runs()
155 lane.flowcell.update_sequencing_runs()
156 for run in lane.flowcell.sequencingrun_set.all():
157 files = run.lane_files().get(lane.lane_number, None)
158 sequencingruns.append((run,
162 context = RequestContext(request,
163 {'lib': lane.library,
165 'flowcell': lane.flowcell,
166 'filtered_sequencingruns': sequencingruns})
168 return render(request, 'experiments/flowcell_lane_detail.html', context)
170 def read_result_file(self, key):
171 """Return the contents of filename if everything is approved
173 data_file = get_object_or_404(DataFile, random_key = key)
175 content_type = 'application/octet-stream'
176 if data_file.file_type.mimetype is not None:
177 content_type = data_file.file_type.mimetype
179 if os.path.exists(data_file.pathname):
180 return HttpResponse(open(data_file.pathname,'rb'),
181 content_type=content_type)
186 def sequencer(request, sequencer_id):
187 sequencer = get_object_or_404(Sequencer, id=sequencer_id)
188 context = RequestContext(request,
189 {'sequencer': sequencer})
190 return render(request, 'experiments/sequencer.html', context)
193 def lanes_for(request, username=None):
195 Generate a report of recent activity for a user
198 if username is not None:
200 user = HTSUser.objects.get(username=username)
201 query.update({'library__affiliations__users__id': user.id})
202 except HTSUser.DoesNotExist as e:
203 raise Http404('User not found')
205 fcl = HTSChangeList(request, Lane,
206 list_filter=['library__affiliations',
207 'library__library_species'],
208 search_fields=['flowcell__flowcell_id', 'library__id', 'library__library_name'],
210 model_admin=LaneOptions(Lane, None),
214 context = {'lanes': fcl, 'title': 'Lane Index'}
216 return render(request, 'samples/lanes_for.html', context)