5

I want to import images in mongoDB along with any dictionary. The dictionary should provide image tags, of which types, numbers and names I can't know at the moment I define the schema. I'm trying to add a dictionary in eve without success:

curl -F"attr={\"a\":1}"  -F "img_id=2asdasdasd" -F "img_data=@c:\path\
1.png;type=image/png" http://127.0.0.1:5000/images
{"_status": "ERR", "_issues": {"attr": "must be of dict type"}, "_error": {"message": "Insertion failure: 1 document(s)
contain(s) error(s)", "code": 422}}

My schema definition looks like that:

'schema': {
    #Fixed attributes
        'original_name': {
            'type': 'string',
            'minlength': 4,
            'maxlength': 1000,
        },
        'img_id': {
            'type': 'string',
            'minlength': 4,
            'maxlength': 150,
            'required': True,

            'unique': True,
        },
        'img_data': {
            'type': 'media'
        },
    #Additional attributes
        'attr': {
            'type': 'dict'

        }
    }

Is it possible at all? Should the schema for dicts be fixed?

EDIT I wanted to add image first and the dictionary after it, but get an error in PATCH request:

C:\Windows\SysWOW64>curl -X PATCH -i  -H "Content-Type: application/json" -d "{\
"img_id\":\"asdasdasd\", \"attr\": {\"a\": 1}}" http://localhost:5000/images/asd
asdasd
HTTP/1.0 405 METHOD NOT ALLOWED
Content-Type: application/json
Content-Length: 106
Server: Eve/0.7.4 Werkzeug/0.9.4 Python/2.7.3
Date: Wed, 28 Jun 2017 22:55:54 GMT

{"_status": "ERR", "_error": {"message": "The method is not allowed for the requested URL.", "code": 405}}
Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • I've checked how postman does a working similar request, and sending curl with `--data-binary` works. I know that your Content-type should be adjusted to multipart since you are sending images together: `curl 'http://localhost:5000/images/' -H 'Content-Type: application/json' --data-binary '{"attr": {"a": 1}}'`. – gcw Jun 28 '17 at 17:43
  • @gcw : Yes, providing the data by -d worked (your json should contain img_id as it has required attribute) but I cant submit an image. I've tried to send the image first, and than add the meta intormation using -X PATCH, but it failed also. – Valentin H Jun 29 '17 at 08:06
  • what was the error in the PATCH request? – gcw Jun 29 '17 at 11:29
  • @gcw: I've added the patch error in the question at the end – Valentin H Jun 30 '17 at 20:05
  • I see. The PATCH error is because you need to allow patching for `images` items in your domain configuration. See how to set `item_methods` on eve's documentation: http://python-eve.org/config.html#resource-item-endpoints – gcw Jul 01 '17 at 17:07
  • @gcw: Do you mean this setting? `ITEM_METHODS = ['GET', 'PATCH', 'DELETE']` Looks like PATCH is allowed. – Valentin H Jul 01 '17 at 23:19
  • Yes, that's the setting. I see that you are using `img_id` on the PATCH URL, do you have an additional lookup set for `images`? You need to use eve's auto-generated `_id` field as standard for item reference. Does it work if you try to GET this same URL `http://localhost:5000/images/asdasdasd`? – gcw Jul 03 '17 at 11:31

1 Answers1

0

I have posted an issue on Github for the same situation. However I have came with a workaround.

Override the dict validator:

class JsonValidator(Validator):

    def _validate_type_dict(self, field, value):
        if type(value) is dict:
            pass
        try:
            json.loads(value)
        except:
            self._error(value, "Invalid JSON")

app = Eve(validator=JsonValidator)

Next, add an insert hook:

def multi_request_json_parser(documents):
    for item in documents:
        if 'my_json_field' in item.keys():
            item['my_json_field'] = json.loads(item['my_json_field'])

app.on_insert_myendpoint += multi_request_json_parser

The dict validator must be overidden, because else the insert hook will not be called due to an validation error.

Mac Lotsen
  • 53
  • 1
  • 10