def createSample(sample_name, sample_type, **kwargs): """ sample_name: string, name for the new sample, required sample_type: required, either: - string, name for existing type in Types collection, or - mongo type object for existing type in Types collection kwargs: passed to constructor """ kwargs['sampleName'] = sample_name kwargs['requestList'] = [] kwargs['resultList'] = [] # initialize request count to zero if not kwargs.has_key('request_count'): kwargs['request_count'] = 0 if isinstance(sample_type, unicode) or isinstance(sample_type, str): kwargs['sample_type'] = type_from_name(sample_type, as_mongo_obj=True) # try: # kwargs['sample_type'] = Types.objects(__raw__={'name': type_name})[0] # except IndexError: # raise ValueError('no sample type found matching "{0}"'.format(type_name)) s = Sample(**kwargs) s.save() return s.sample_id
def getSampleNamebyID(sample_id): """ sample_id: required, integer """ s = Sample.objects(__raw__={'sample_id': sample_id}) return _try0_dict_key(s, 'sample', 'sample_id', sample_id, -99, dict_key='sampleName')
def getSampleByID(sample_id, as_mongo_obj=False): """ sample_id: required, integer """ s = Sample.objects(__raw__={'sample_id': sample_id}) return _try0_maybe_mongo(s, 'sample', 'sample_id', sample_id, None, as_mongo_obj=as_mongo_obj)
def getSampleRequestCount(sample_id): """ get the 'request_count' attribute of the specified sample """ s = Sample.objects(__raw__={'sample_id': sample_id}) return _try0_dict_key(s, 'sample', 'sample_id', sample_id, None, dict_key='request_count')
def find_sample_request(): req_list = [] for samp in Sample.objects(): for req in samp.requestList: req_list.append(req.to_mongo()) return req_list
def getResultsforSample(sample_id): """ Takes a sample_id and returns it's resultsList or []. """ sample = Sample.objects(__raw__={'sample_id': sample_id} ).only('resultList') return [r.to_mongo() for r in _try0_maybe_mongo(sample, 'sample', 'sample_id', sample_id, None, as_mongo_obj=True).resultList]
def incrementSampleRequestCount(sample_id): """ increment the 'request_count' attribute of the specified sample by 1 """ # potential for race here? s = Sample.objects(__raw__={'sample_id': sample_id})[0] s.update(inc__request_count=1) s.reload() return s.request_count
def find_sample(): ret_list = [] headers = ['sample_name', 'sample_type'] #return [s.to_mongo() for s in Sample.objects()] for samp in Sample.objects(): s = samp.to_mongo() s['sample_type'] = samp.sample_type.name ret_list.append(s) return (headers, ret_list)
def addResulttoSample(result_type, sample_id, result_obj=None, timestamp=None, as_mongo_obj=False, **kwargs): """ like addResulttoRequest, but without a request """ r = createResult(result_type, sample_id=sample_id, result_obj=result_obj, timestamp=timestamp, as_mongo_obj=True, **kwargs) try: sample = Sample.objects(sample_id=sample_id)[0] except IndexError: return None sample.resultList.append(r) sample.save() return r.to_mongo()
def deleteResult(result_id, get_result=False): """ Takes a result_id, deletes, and optionally returns the matching result or None. When should we ever be doing this? result_id: required, int """ result_id = int(result_id) # delete it from any samples first sample_qs = Sample.objects(__raw__={'resultList.result_id': result_id}) sample = _try0_maybe_mongo(sample_qs, 'result', 'result_id', result_id, None, as_mongo_obj=True) for res in sample.resultList: if res.result_id == result_id: tmp = res sample.resultList.remove(res) sample.save() # then directly in Results r = getResult(result_id, as_mongo_obj=True) r.delete()
def addResultforRequest(result_type, request_id, result_obj=None, timestamp=None, as_mongo_obj=False, **kwargs): """ like createResult, but also adds it to the resultList of result['sample_id'] """ r = createResult(result_type, request_id, result_obj=result_obj, timestamp=timestamp, as_mongo_obj=True, **kwargs) request = r.request_id #sample_qs = Sample.objects(__raw__={'requestList.request_id': request_id}) #sample = _try0_maybe_mongo(sample_qs, 'request', 'request_id', request_id, None, # as_mongo_obj=True) try: sample = Sample.objects(requestList__in=[request])[0] except IndexError: return None sample.resultList.append(r) sample.save() return r.to_mongo()
def getSampleIDbyName(sample_name): s = Sample.objects(__raw__={'sampleName': sample_name}) return _try0_dict_key(s, 'sample', 'sampleName', sample_name, -99, dict_key='sample_id')