def createRequest(request_type, request_obj=None, timestamp=None, as_mongo_obj=False, **kwargs): """ request_type: required, name (string) of request type, dbref to it's db entry, or a Type object request_obj: optional, stored as is, could be a dict of collection parameters, or whatever timestamp: datetime.datetime.now() if not provided priority: optional, integer priority level anything else (priority, sample_id) can either be embedded in the request_object or passed in as keyword args to get saved at the top level. """ if isinstance(request_type, unicode) or isinstance(request_type, str): request_type = type_from_name(request_type, as_mongo_obj=True) print('rt:[{0}]'.format(request_type)) # elif not isinstance(request_type, Request): # raise ValueError('wrong type {0}'.format(request_type.__class__)) kwargs['request_type'] = request_type kwargs['timestamp'] = timestamp kwargs['request_obj'] = request_obj r = Request(**kwargs) r.save() if as_mongo_obj: return r return r.to_mongo()
def getQueueFast(): requests = Request.objects(sample_id__exists=True) # return [request.to_mongo() for request in requests] # generator seems slightly faster even when wrapped by list() for request in requests: yield request.to_mongo()
def getRequest(reqID, as_mongo_obj=False): # need to get this from searching the dewar I guess reqID = int(reqID) """ request_id: required, integer id """ r = Request.objects(__raw__={'request_id': reqID}) return _try0_maybe_mongo(r, 'request', 'request_id', reqID, None, as_mongo_obj=as_mongo_obj)
def find_request(): ret_list = [] headers = ['request_name', 'request_type', 'fields'] #return [r.to_mongo() for r in Request.objects()] for req in Request.objects(): r = req.to_mongo() r['request_type'] = req.request_type.name ret_list.append(r) return (headers, ret_list)
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 updateRequest(request_dict): """ This is not recommended once results are recorded for a request! Using a new request instead would keep the apparent history complete and intuitive. Although it won't hurt anything if you've also recorded the request params used inside the results and query against that, making requests basically ephemerally useful objects. """ if not Request.objects(__raw__={'request_id': request_dict['request_id']}).update( set__request_obj=request_dict['request_obj']): addRequesttoSample(**request_dict)
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()
# Nah... [0] is faster and catch Exception... try: items = Container.objects(__raw__={'containerName': primaryDewarName}).only('item_list')[0].item_list except IndexError, AttributeError: raise ValueError('could not find container: "{0}"!'.format(primaryDewarName)) items = set(items) items.discard(None) # skip empty positions sample_list = [] for samp in Container.objects(container_id__in=items).only('item_list'): sil = set(samp.item_list) sil.discard(None) sample_list += sil for request in Request.objects(sample_id__in=sample_list): yield request.to_mongo() # ## for item_id in items.item_list: ## if item_id is not None: # for item_id in items: # try: # puck_items = Container.objects(__raw__={'container_id': item_id}).only('item_list')[0].item_list # except IndexError, AttributeError: # raise ValueError('could not find container id: "{0}"!'.format(item_id)) # # puck_items = set(puck_items) # puck_items.discard(None) # skip empty positions # ## for sample_id in puck.item_list: