Beispiel #1
0
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)
Beispiel #2
0
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
Beispiel #3
0
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()
Beispiel #4
0
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