def createResult(result_type, request_id=None, sample_id=None, result_obj=None, timestamp=None, as_mongo_obj=False, **kwargs): """ result_type: string, Type object, or dbref, required request_id: int, Request object, or dbref (optional) sample_id: int, Sample object, or dbref (optional, unecessary if request_id is specified) result_obj: dict to attach timestamp: """ if not isinstance(result_type, Result) and not isinstance(result_type, bson.DBRef): result_type = type_from_name(result_type, as_mongo_obj=True) if request_id is not None: if not isinstance(request_id, Request) and not isinstance(request_id, bson.DBRef): request_id = getRequest(request_id, as_mongo_obj=True) kwargs['request_id'] = request_id if sample_id is not None: if isinstance(sample_id, Sample) or isinstance(sample_id, bson.DBRef): sample_id = sample_id.sample_id kwargs['sample_id'] = sample_id kwargs['result_type'] = result_type kwargs['timestamp'] = timestamp kwargs['result_obj'] = result_obj r = Result(**kwargs) r.save() if as_mongo_obj: return r return r.to_mongo()
def getResult(result_id, as_mongo_obj=False): """ result_id: required, int or bson.DBRef """ if isinstance(result_id, bson.DBRef): result = Result.objects(__raw__={'_id': result_id.id}) return _try0_maybe_mongo(result, 'result', 'result _id', result_id.id, None, as_mongo_obj=as_mongo_obj) else: result_id = int(result_id) # do we need this cast? result = Result.objects(__raw__={'result_id': result_id}) return _try0_maybe_mongo(result, 'result', 'result_id', result_id, None, as_mongo_obj=as_mongo_obj)
def getResultsforRequest(request_id): """ Takes an integer request_id and returns a list of matching results or []. """ reslist = [] # convert int ('request_id') to ObjectID ('_id') if isinstance(request_id, int): request_id = Request.objects(__raw__={'request_id': request_id}).only('id')[0].id for result in Result.objects(request_id=request_id): reslist.append(result.to_mongo()) return reslist
def deleteRequest(reqObj): """ reqObj should be a dictionary with a 'request_id' field and optionally a 'sample_id' field. If the request to be deleted is the last entry in a sample's requestList, the list attribute is removed entirely, not just set to an empty list! The request_id attribute for any results which references the deleted request are also entirely removed, not just set to None! This seems to be the way wither mongo, pymongo, or mongoengine works :( """ r_id = reqObj['request_id'] # delete it from any sample first try: sample = getSampleByID(reqObj['sample_id'], as_mongo_obj=True) # maybe there's a slicker way to get the req with a query and remove it? for req in sample.requestList: if req.request_id == r_id: print("found the request to delete") sample.requestList.remove(req) sample.save() break except KeyError: pass # not all requests are linked to samples # then any results that refer to it req = Request.objects(__raw__={'request_id': r_id}).only('id')[0].id for res in Result.objects(request_id=req): res.request_id = None res.save() # then finally directly in Requests r = getRequest(r_id, as_mongo_obj=True) if r: r.delete()
def getResultsforBL(id=None, name=None, number=None): """ Retrieve results using either BL id, name, or number (tried in that order) Returns a generator of results """ if id is None: if name is None: key = 'number' val = number else: key = 'name' val = name query = {key: val} b = Beamline.objects(__raw__=query) id = _try0_dict_key(b, 'beamline', key, val, None, 'beamline_id') if id is None: yield None raise StopIteration for r in Result.objects(__raw__={'beamline_id': id}): yield r