Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patching list of dicts w/ data_relation #60

Closed
jkatz50 opened this issue Jun 3, 2013 · 7 comments
Closed

patching list of dicts w/ data_relation #60

jkatz50 opened this issue Jun 3, 2013 · 7 comments
Labels
Milestone

Comments

@jkatz50
Copy link

jkatz50 commented Jun 3, 2013

I have a schema where I've defined a list of dictionary objects, however whenever I try to post to the resource with the list defined with a dictionary in it, it returns a status "ERR" and issue ["'field'"].

When I post without the list defined or the list empty, it will post fine, but then when I try to patch the list field with a dict, it gives me a 400 error.

my schema looks something like this...:

'schema': {
    'name': {
        'type': 'string',
        'required': True,
    },
    'perma_name': {
        'type': 'string',
    },
    'active_list': {
        'type': 'list',
        'schema': {
            'type': 'dict',
            'schema': {
                'p_id': {
                    'type': 'objectid',
                    'required': True,
                    'data_relation': {
                        'collection': 'projects'
                    }
                },
                'proj_name': {
                    'type': 'string',
                },
                'perma_name': {
                    'type': 'string',
                },
                'raised': {
                    'type': 'integer',
                },
                'goal': {
                    'type': 'integer',
                },
                'description': {
                    'type': 'string',
                }
            }
        }
    }
}
@nicolaiarocci
Copy link
Member

Hello,

can you please provide me with an example of the request that causes you trouble?

@jkatz50
Copy link
Author

jkatz50 commented Jun 3, 2013

Here's my 2 schemas and some sample code

projects = {
    'item_title': 'project',
    'additional_lookup': {
        'url': '[\w]+',
        'field': 'perma_name'
    },
    'schema': {
        'name': {
            'type': 'string',
            'required': True,
        },
        'perma_name': {
            'type': 'string',
            'empty': False,
            'required': True,
            'unique': True,
        },
        'camp_id': {
            'type': 'objectid',
            'required': True,
            'data_relation': {
                'collection': 'campaigns'
            }
        },
        'raised': {
            'type': 'integer',
            'required': True,
            'min': 0,
        },
        'goal': {
            'type': 'integer',
            'required': True,
            'min': 0,
        },
        'description': {
            'type': 'string',
        }
    }
}

campaigns = {
    'item_title': 'campaign',
    'additional_lookup': {
        'url': '[\w]+',
        'field': 'perma_name'
    },
    'schema': {
        'name': {
            'type': 'string',
            'required': True,
        },
        'perma_name': {
            'type': 'string',
            'empty': False,
            'required': True,
            'unique': True,
        },
        'active_list': {
            'type': 'list',
            'items': {
                'p_id': {
                    'type': 'objectid',
                    'required': True,
                    'data_relation': {
                        'collection': 'projects'
                    }
                },
                'proj_name': {
                    'type': 'string',
                    'empty': False,
                    'required': True,
                },
                'perma_name': {
                    'type': 'string',
                    'required': True
                },
                'raised': {
                    'type': 'integer',
                    'required': True,
                    'min': 0,
                },
                'goal': {
                    'type': 'integer',
                    'required': True,
                    'min': 0,
                },
                'description': {
                    'type': 'string',
                    'required': True,
                    'maxlength': 255,
                }
            }
        }
    }
}


import requests
import json

camp = {'name': 'test',
             'perma_name': 'test',
             'active_list': []
}

data = {'item1': camp}

post_camp = requests.post('http://127.0.0.1:5001/campaigns/',data=json.dumps(data), headers={'content-type': 'application/json'})

camp_res = post_camp.json()
proj = { 'name': 'test_proj', 'perma_name': 'test_proj', 'camp_id': camp_res['item1']['_id'], 'raised': 0, 'goal': 50000, 'description': 'whooo'}

post_proj = requests.post('http://127.0.0.1:5001/projects/',data=json.dumps({'item1':proj}), headers={'content-type': 'application/json'})
proj_res = post_proj.json()
camp_proj = {'p_id': proj_res['item1']['_id'], 'proj_name': proj['name'], 'perma_name': proj['perma_name'], 'raised': 0, 'goal': 50000, 'description': 'whooo'}

camp_patch = {'active_list': [camp_proj]}
patch_link = 'http://' + camp_res['item1']['_links']['self']['href']
etag = camp_res['item1']['etag']
changes = {'item1': camp_patch}

patch_req = requests.post(patch_link, data=json.dumps(changes),headers={'content-type':'application/json','x-http-method-override':'patch','if-match':etag})
patch_req.content
#this will return a 400 error

new_camp = {'name': 'test2', 'perma_name': 'test2', 'active_list' = [camp_proj]}
new_camp_post = requests.post('http://127.0.0.1:5001/campaigns/', data=json.dumps({'item1':new_camp}), headers={'content-type': 'application/json'})
new_camp_post.json()
#will return with {'item1': {'status': 'ERR', 'issues': ["'field'"]}}

@jkatz50
Copy link
Author

jkatz50 commented Jun 3, 2013

Upon further inspection, I think it might be the fact that in my campaigns active list array, I am trying to define a data relation on projects? I believe if I take the data_relation out, it posts ok...

^-- just confirmed that it works without the data_relation for both...

@nicolaiarocci
Copy link
Member

Try setting the data_relation field value explicitly. There might be a bug in the code that sets the default value (I'm just looking at the code, can't test it atm).

@jkatz50
Copy link
Author

jkatz50 commented Jun 3, 2013

Awesome! I believe that worked

@nicolaiarocci
Copy link
Member

Excellent. Will fix in 0.0.7.

@jkatz50
Copy link
Author

jkatz50 commented Jun 3, 2013

Great framework btw - I'm loving the rapid development I can do with this and the speed with which it is evolving.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants