Ejemplo n.º 1
0
def getWorkflowCompositeID(id, dn=None):
    """
    Method to retrieve a workflow composite id in the field 'alias'.
    """
    api_version, root_url, root = get_api_version(request.url)

    r = {}
    if request.method == 'GET':
        #Add logic to parse id if comma separated
        if id:
            ids = id.strip().split(',')
            for id in ids:
                rs = rdb.getWorkflowCompositeID(id, dn=dn)
                if rs:
                    r[id] = rs
                else:
                    r[id] = {
                        'uid': '0',
                        'error': 'invalid response',
                        'len': len(rs),
                        'resp': rs
                    }

            if len(ids) == 1:  #return just single record if one uid
                r = rs

    return Response(json.dumps(r, cls=MPOSetEncoder),
                    mimetype='application/json')
Ejemplo n.º 2
0
def getWorkflowCompositeID(id,dn=None):
    """
    Method to retrieve a workflow composite id in the field 'alias'.
    """
    api_version,root_url,root=get_api_version(request.url)

    r={}
    if request.method == 'GET':
        #Add logic to parse id if comma separated
        if id:
            ids=id.strip().split(',')
            for id in ids:
                rs = rdb.getWorkflowCompositeID(id,dn=dn)
                if rs:
                    r[id]=rs
                else:
                    r[id]={'uid':'0','error':'invalid response','len':len(rs),'resp':rs}

            if len(ids)==1: #return just single record if one uid
                r=rs

    return Response(json.dumps(r,cls=MPOSetEncoder), mimetype='application/json')
Ejemplo n.º 3
0
def collectionElement(id=None, oid=None, dn=None):
    """
    /collection/:id/element       - GET a list of objects in a collection
                                   - POST to add to the collection
    /collection/:id/element/:oid - GET details of a single object in a collection.
                                    Should resolve oid to full record from relevant table.
    /collection/:id/element?detail=full[sparse] - GET collection information with full
               details [or default sparse as /collection/<id>]
    """
    api_version, root_url, root = get_api_version(request.url)
    if request.method == 'POST':

        payload = json.loads(request.data)

        #make sure the element hasn't been added to the collection already
        #elems must be a list of uids that are not already in this collection, if it exists.
        elems = payload.get('elements')
        if elems:
            if not isinstance(elems, list):
                elements = [elems]
            else:
                elements = elems
        else:
            elements = []

        #remove elements already in the collection
        for e in elements[:]:
            r = rdb.getRecord('collection_elements', {
                'uid': e,
                'parent_uid': id
            },
                              dn=dn)
            if len(r) != 0:
                elements.remove(
                    e)  #maybe add to response message this was done
        payload['elements'] = elements

        #add elements one and a time and create list of returned records
        r = []
        for e in elements:
            rr = rdb.addRecord('collection_elements',
                               json.dumps({
                                   'uid': e,
                                   'parent_uid': id
                               }),
                               dn=dn)
            r.append(rr)
            morer = rdb.getRecord('collection_elements', {'uid': rr['uid']},
                                  dn=dn)
            publishEvent('mpo_collection_elements', onlyone(morer))
    elif request.method == 'DELETE':
        if oid:
            r = rdb.deleteCollectionElement({
                'parent_uid': id,
                'uid': oid
            },
                                            dn=None)
        else:
            r = []
    elif request.method == 'GET':
        if oid:
            r = rdb.getRecord('collection_elements', {
                'parent_uid': id,
                'uid': oid
            },
                              dn=dn)
        else:
            r = rdb.getRecord('collection_elements', {'parent_uid': id}, dn=dn)

        #Find original item and add it to record.
        for record in r:
            print('collection element r', record)
            r_uid = record['uid']
            #getRecordTable returns a python dict
            record['type'] = rdb.getRecordTable(r_uid, dn=dn)

            #set default field values
            detail = {
                'related': 'not sure',
                'link-related': root_url,
                'related': 'cousins',
                'name': 'what is this?',
                'description': 'empty',
                'time': 'nowhen'
            }
            #Translation for specific types
            if record['type'] == 'workflow':
                detail = rdb.getWorkflow({'uid': r_uid}, dn=dn)[0]
                print('element workflow', detail)
                detail['related'] = rdb.getWorkflowCompositeID(
                    r_uid, dn=dn).get('alias')
                links = {}
                links['link1'] = root_url + '/workflow/' + r_uid
                links['link2'] = root_url + '/workflow?alias=' + detail[
                    'related']
                detail['link-related'] = links
            elif record['type'] == 'dataobject':
                detail = rdb.getRecord('dataobject', {'uid': r_uid}, dn=dn)[0]
                detail['related'] = detail.get('uri')
                detail['link-related'] = root_url + '/dataobject/' + r_uid
            elif record['type'] == 'dataobject_instance':
                do_uid = (rdb.getRecord('dataobject_instance',
                                        {'uid': record['uid']},
                                        dn=dn)[0]).get('do_uid')
                detail = rdb.getRecord('dataobject', {'uid': do_uid}, dn=dn)[0]
                detail['related'] = detail.get('uri')
                detail['link-related'] = root_url + '/dataobject/' + do_uid
            elif record['type'] == 'collection':
                thisdetail = rdb.getRecord('collection', {'uid': r_uid},
                                           dn=dn)[0]
                detail['related'] = None
                detail[
                    'link-related'] = root_url + '/collection/' + r_uid + '/element'
                detail['description'] = thisdetail.get('description')
                detail['name'] = thisdetail.get('name')
                detail['time'] = thisdetail.get('time')

            record['name'] = detail['name']
            record['description'] = detail['description']
            record['time'] = detail['time']
            record['related'] = detail['related']
            record['link-related'] = detail['link-related']

    istatus = 200
    #    if len(r) == 0:
    #    #istatus = 404
    #    r=[{'mesg':'No records found', 'number_of_records':0, 'status':404}]

    return Response(json.dumps(r, cls=MPOSetEncoder),
                    mimetype='application/json',
                    status=istatus)
Ejemplo n.º 4
0
def collectionElement(id=None, oid=None, dn=None):
    """
    /collection/:id/element       - GET a list of objects in a collection
                                   - POST to add to the collection
    /collection/:id/element/:oid - GET details of a single object in a collection.
                                    Should resolve oid to full record from relevant table.
    /collection/:id/element?detail=full[sparse] - GET collection information with full
               details [or default sparse as /collection/<id>]
    """
    api_version,root_url,root=get_api_version(request.url)
    if request.method == 'POST':

        payload = json.loads(request.data)

        #make sure the element hasn't been added to the collection already
        #elems must be a list of uids that are not already in this collection, if it exists.
        elems = payload.get('elements')
        if elems:
            if not isinstance(elems,list):
                elements=[elems]
            else:
                elements=elems
        else:
            elements=[]

        #remove elements already in the collection
        for e in elements[:]:
            r = rdb.getRecord('collection_elements',{'uid':e,'parent_uid':id}, dn=dn)
            if len(r)!=0: elements.remove(e) #maybe add to response message this was done
        payload['elements'] = elements

        #add elements one and a time and create list of returned records
        r=[]
        for e in elements:
            rr=rdb.addRecord('collection_elements',json.dumps({'uid':e,'parent_uid':id}),dn=dn)
            r.append(rr)
            morer = rdb.getRecord('collection_elements',{'uid':rr['uid']},dn=dn)
            publishEvent('mpo_collection_elements',onlyone(morer))
    elif request.method == 'DELETE':
        if oid:
            r = rdb.deleteCollectionElement({'parent_uid':id,'uid':oid}, dn=None)
        else:
            r = []
    elif request.method == 'GET':
        if oid:
            r = rdb.getRecord('collection_elements',{'parent_uid':id,'uid':oid}, dn=dn)
        else:
            r = rdb.getRecord('collection_elements',{'parent_uid':id}, dn=dn)

        #Find original item and add it to record.
        for record in r:
            print ('collection element r',record)
            r_uid=record['uid']
            #getRecordTable returns a python dict
            record['type']=rdb.getRecordTable( r_uid, dn=dn )

            #set default field values
            detail={'related':'not sure','link-related':root_url,'related':'cousins',
                    'name':'what is this?','description':'empty','time':'nowhen'}
            #Translation for specific types
            if record['type']=='workflow':
                detail=rdb.getWorkflow({'uid':r_uid},dn=dn)[0]
                print('element workflow',detail)
                detail['related']=rdb.getWorkflowCompositeID(r_uid,dn=dn).get('alias')
                links={}
                links['link1']=root_url+'/workflow/'+r_uid
                links['link2']=root_url+'/workflow?alias='+detail['related']
                detail['link-related']=links
            elif record['type']=='dataobject':
                detail=rdb.getRecord('dataobject',{'uid':r_uid},dn=dn)[0]
                detail['related']=detail.get('uri')
                detail['link-related']=root_url+'/dataobject/'+r_uid
            elif record['type']=='dataobject_instance':
                do_uid=(rdb.getRecord('dataobject_instance',{'uid':record['uid']},dn=dn)[0]).get('do_uid')
                detail=rdb.getRecord('dataobject',{'uid':do_uid},dn=dn)[0]
                detail['related']=detail.get('uri')
                detail['link-related']=root_url+'/dataobject/'+do_uid
            elif record['type']=='collection':
                thisdetail=rdb.getRecord('collection',{'uid':r_uid},dn=dn)[0]
                detail['related']=None
                detail['link-related']=root_url+'/collection/'+r_uid+'/element'
                detail['description']=thisdetail.get('description')
                detail['name']=thisdetail.get('name')
                detail['time']=thisdetail.get('time')

            record['name']=detail['name']
            record['description']=detail['description']
            record['time']=detail['time']
            record['related']=detail['related']
            record['link-related']=detail['link-related']

    istatus=200
    #    if len(r) == 0:
    #    #istatus = 404
    #    r=[{'mesg':'No records found', 'number_of_records':0, 'status':404}]

    return Response(json.dumps(r,cls=MPOSetEncoder),mimetype='application/json',status=istatus)