Adding an untested python eve dictionary along with an image

I want to import images into mongoDB along with any dictionary. The dictionary should contain image tags, of which types, numbers and names that I cannot know, at the moment I am defining a scheme. I try to add a dictionary on 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 is as follows:

 '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 be fixed for dicts?

EDIT I wanted to add an image and a dictionary after it first, but get an error message in the 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}} 
+5
source share
1 answer

I posted a Github question for the same situation. However, I came up with a workaround.

Override validator check:

 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) 

Then add the 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 overridden, because otherwise the insert hook will not be called due to a validation error.

0
source

Source: https://habr.com/ru/post/1269276/


All Articles