# test the new object.  SHOULD HANDLE ERRORS GRACEFULLY        
            try:
                jsonschema.validate(new_object,object_schema)
            # did not validate
            except Exception as e:
                print('Validation of ' + object_id + ' failed.')
                print(e)

            # did validate
            else:
                # inform the user of the success
                print('Validation of ' + object_id + ' succeeded.')

                # post the new object(s).  SHOULD HANDLE ERRORS GRACEFULLY
                response = new_ENCODE(object_collection,new_object,keys)


        # if object is found, check for differences and patch it if needed.
        else:

            # compare new object to old one, remove identical fields.  Also, remove fields not present in schema. SHOULD INFORM OF THIS OPERATION, BUT NOT NEEDED WHEN SINGLE PATCH CODE EXISTS.
            for key in new_object.keys():
                if new_object.get(key) == old_object.get(key):
                    new_object.pop(key)
                elif not old_object.get(key):
                    new_object.pop(key)

            # if there are any different fields, patch them.  SHOULD ALLOW FOR USER TO VIEW/APPROVE DIFFERENCES
            if new_object:
                
Exemplo n.º 2
0
#                response = new_ENCODE(object_collection,new_object)

        # if object is not found, verify and post it
        if (old_object.get(u'title') == u'Not Found') | (old_object.get(u'title') == u'Home'):

            # clean object of unpatchable or nonexistent properties.  SHOULD INFORM USER OF ANYTHING THAT DOESN"T GET POSTED.
            new_object = CleanJSON(new_object,object_schema,'POST')
    
            new_object = FlatJSON(new_object,keys)
            print(new_object)

            # test the new object       
            if ValidJSON(object_type,object_id,new_object,keys):
                # post the new object(s).  SHOULD HANDLE ERRORS GRACEFULLY
                new_object = CleanJSON(new_object,object_schema,'POST')
                response = new_ENCODE(object_type,new_object,keys)
                
                object_check = GetENCODE(str(response[u'@graph'][0][u'@id']),keys)
                print(object_check[u'@id'], object_check[u'uuid'])

        # if object is found, check for differences and patch it if needed/valid.
        elif put_status:
            # clean object of unpatchable or nonexistent properties.  SHOULD INFORM USER OF ANYTHING THAT DOESN"T GET PUT.
            new_object = CleanJSON(new_object,object_schema,'POST')

            new_object = FlatJSON(new_object,keys)

            print('Running a put.')
            print(new_object)
            response = replace_ENCODE(object_id,new_object,keys)
 
            # test the new object.  SHOULD HANDLE ERRORS GRACEFULLY
            try:
                jsonschema.validate(new_object, object_schema)
            # did not validate
            except Exception as e:
                print('Validation of ' + object_id + ' failed.')
                print(e)

            # did validate
            else:
                # inform the user of the success
                print('Validation of ' + object_id + ' succeeded.')

                # post the new object(s).  SHOULD HANDLE ERRORS GRACEFULLY
                response = new_ENCODE(object_collection, new_object, keys)

        # if object is found, check for differences and patch it if needed.
        else:

            # compare new object to old one, remove identical fields.  Also, remove fields not present in schema. SHOULD INFORM OF THIS OPERATION, BUT NOT NEEDED WHEN SINGLE PATCH CODE EXISTS.
            for key in new_object.keys():
                if new_object.get(key) == old_object.get(key):
                    new_object.pop(key)
                elif not old_object.get(key):
                    new_object.pop(key)

            # if there are any different fields, patch them.  SHOULD ALLOW FOR USER TO VIEW/APPROVE DIFFERENCES
            if new_object:

                # inform user of the updates