Example #1
0
def item(id, dn=None):
    api_version,root_url,root=get_api_version(request.url)

    if id:
        r = rdb.getRecordTable( id, dn=dn )
    else:
        payload={"url":request.url, "body":request.data, "hint":"Must provide an UID", "uid":-1}
        raise InvalidAPIUsage(message='Unsupported route specified',status_code=400,
                              payload=payload)

    return Response(json.dumps({'table':r,'uid':id}), mimetype='application/json')
Example #2
0
def item(id, dn=None):
    api_version, root_url, root = get_api_version(request.url)

    if id:
        r = rdb.getRecordTable(id, dn=dn)
    else:
        payload = {
            "url": request.url,
            "body": request.data,
            "hint": "Must provide an UID",
            "uid": -1
        }
        raise InvalidAPIUsage(message='Unsupported route specified',
                              status_code=400,
                              payload=payload)

    return Response(json.dumps({
        'table': r,
        'uid': id
    }),
                    mimetype='application/json')
Example #3
0
def comment(id=None, dn=None):
    api_version, root_url, root = get_api_version(request.url)

    if request.method == 'POST':
        req = json.loads(request.data)
        req['ptype'] = rdb.getRecordTable(req['parent_uid'], dn=dn)
        r = rdb.addRecord('comment', json.dumps(req), dn=dn)
        id = r['uid']
        if apidebug: print('APIDEBUG comment route:: dn = ', dn)
        try:  #JCW just being careful here on first implementation
            morer = rdb.getRecord('comment', {'uid': id}, dn=dn)
        except Exception as e:
            import sys, os
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print('get comment exception', exc_type, fname, exc_tb.tb_lineno)

        publishEvent('mpo_comment', onlyone(morer))

    elif request.method == 'GET':
        #Add logic to parse id if comma separated
        if id:
            ids = id.strip().split(',')
            r = {}
            for id in ids:
                rs = rdb.getRecord('comment', {'uid': id}, dn=dn)
                if len(rs) == 1:
                    r[id] = rs[0]  #unpack single element list
                else:
                    r[id] = [
                    ]  #{'uid':'0','msg':'invalid response','len':len(rs),'resp':rs}
            if len(ids) == 1:  #return just single record if one uid
                r = rs
            else:
                r = r
        else:
            r = rdb.getRecord('comment', request.args, dn=dn)

    return Response(json.dumps(r, cls=MPOSetEncoder),
                    mimetype='application/json')
Example #4
0
def comment(id=None, dn=None):
    api_version,root_url,root=get_api_version(request.url)

    if request.method == 'POST':
        req = json.loads(request.data)
        req['ptype']=rdb.getRecordTable(req['parent_uid'], dn=dn)
        r = rdb.addRecord('comment',json.dumps(req),dn=dn)
        id = r['uid']
        if apidebug: print('APIDEBUG comment route:: dn = ',dn)
        try:  #JCW just being careful here on first implementation
            morer = rdb.getRecord('comment',{'uid':id},dn=dn)
        except Exception as e:
            import sys,os
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print('get comment exception',exc_type, fname, exc_tb.tb_lineno)

        publishEvent('mpo_comment',onlyone(morer))

    elif request.method == 'GET':
        #Add logic to parse id if comma separated
        if id:
            ids=id.strip().split(',')
            r={}
            for id in ids:
                rs = rdb.getRecord('comment',{'uid':id},dn=dn)
                if len(rs)==1:
                    r[id]=rs[0] #unpack single element list
                else:
                    r[id]=[]#{'uid':'0','msg':'invalid response','len':len(rs),'resp':rs}
            if len(ids)==1: #return just single record if one uid
                r=rs
            else:
                r=r
        else:
            r = rdb.getRecord('comment',request.args,dn=dn)

    return Response(json.dumps(r,cls=MPOSetEncoder), mimetype='application/json')
Example #5
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)
Example #6
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)