# some core functions of the exp tracker module
-from django.http import HttpResponse
-from datetime import datetime
-from string import *
+from datetime import datetime, timedelta
import os
import re
+
+from django.http import HttpResponse
from htsworkflow.frontend import settings
from htsworkflow.frontend.experiments.models import FlowCell, DataRun
from htsworkflow.frontend.samples.models import Library
fcid = 'none'
runfolder = 'unknown'
ClIP = request.META['REMOTE_ADDR']
- granted = False
- if request.has_key('user'):
- user = request['user']
-
- #Check access permission
- if (user == 'rami' and settings.ALLOWED_IPS.has_key(ClIP)): granted = True
- if not granted: return HttpResponse("access denied.")
+ if hasattr(request, 'user'):
+ user = request.user
+ #Check access permission
+ if not (user.is_superuser and settings.ALLOWED_IPS.has_key(ClIP)):
+ return HttpResponse("%s access denied from %s." % (user, ClIP))
# ~~~~~~Parameters for the job ~~~~
- if request.has_key('fcid'):
- fcid = request['fcid']
+ if request.REQUEST.has_key('fcid'):
+ fcid = request.REQUEST['fcid']
else:
return HttpResponse('missing fcid')
- if request.has_key('runf'):
- runfolder = request['runf']
+ if request.REQUEST.has_key('runf'):
+ runfolder = request.REQUEST['runf']
else:
return HttpResponse('missing runf')
- if request.has_key('updst'):
- UpdatedStatus = request['updst']
+ if request.REQUEST.has_key('updst'):
+ UpdatedStatus = request.REQUEST['updst']
else:
return HttpResponse('missing status')
#if there's a message update that too
mytimestamp = datetime.now().__str__()
mytimestamp = re.sub(pattern=":[^:]*$",repl="",string=mytimestamp)
- if request.has_key('msg'):
- rec.run_note += ", "+request['msg']+" ("+mytimestamp+")"
+ if request.REQUEST.has_key('msg'):
+ rec.run_note += ", "+request.REQUEST['msg']+" ("+mytimestamp+")"
else :
if UpdatedStatus == '1':
rec.run_note = "Started ("+mytimestamp+")"
else: outputfile = 'Missing input: flowcell id'
return HttpResponse(outputfile, mimetype='text/plain')
+
+def estimateFlowcellDuration(flowcell):
+ """
+ Attempt to estimate how long it will take to run a flowcell
+
+ """
+ # (3600 seconds * 1.5 hours per cycle )
+ sequencing_seconds_per_cycle= 3600 * 1.5
+ # 800 is a rough guess
+ pipeline_seconds_per_cycle = 800
+
+ cycles = flowcell.read_length
+ if flowcell.paired_end:
+ cycles *= 2
+ sequencing_time = timedelta(0, cycles * sequencing_seconds_per_cycle)
+ analysis_time = timedelta(0, cycles * pipeline_seconds_per_cycle)
+ estimate_mid = sequencing_time + analysis_time
+ # floor estimate_mid
+ estimate_low = timedelta(estimate_mid.days, 0)
+ # floor estimate_mid and add a day
+ estimate_high = timedelta(estimate_mid.days+1, 0)
+
+ return (estimate_low, estimate_high)
+
+
+def makeUserLaneMap(flowcell):
+ """
+ Given a flowcell return a mapping of users interested in
+ the libraries on those lanes.
+ """
+ users = {}
+
+ for lane in flowcell.lane_set.all():
+ for affiliation in lane.library.affiliations.all():
+ for user in affiliation.users.all():
+ users.setdefault(user,[]).append(lane)
+
+ return users
+
+def getUsersForFlowcell(flowcell):
+ users = set()
+
+ for lane in flowcell.lane_set.all():
+ for affiliation in lane.library.affiliations.all():
+ for user in affiliation.users.all():
+ users.add(user)
+
+ return users
+
+def makeUserLibraryMap(libraries):
+ """
+ Given an interable set of libraries return a mapping or
+ users interested in those libraries.
+ """
+ users = {}
+
+ for library in libraries:
+ for affiliation in library.affiliations.all():
+ for user in affiliation.users.all():
+ users.setdefault(user,[]).append(library)
+
+ return users
+
+def makeAffiliationLaneMap(flowcell):
+ affs = {}
+
+ for lane in flowcell.lane_set.all():
+ for affiliation in lane.library.affiliations.all():
+ affs.setdefault(affiliation,[]).append(lane)
+
+ return affs
+
+def makeEmailLaneMap(flowcell):
+ """
+ Create a list of email addresses and the lanes associated with those users.
+
+ The email addresses can come from both the "users" table and the "affiliations" table.
+ """
+ emails = {}
+ for lane in flowcell.lane_set.all():
+ for affiliation in lane.library.affiliations.all():
+ if affiliation.email is not None and len(affiliation.email) > 0:
+ emails.setdefault(affiliation.email,set()).add(lane)
+ for user in affiliation.users.all():
+ if user.email is not None and len(user.email) > 0:
+ emails.setdefault(user.email,set()).add(lane)
+
+ return emails