Krukov / amocrm_api

Python amocrm API wrapper
MIT License
104 stars 55 forks source link

Баг с полем file #118

Open andrewttpp opened 8 months ago

andrewttpp commented 8 months ago

При обновлении сделки, в данные для отправки в амо подтягивается обязательное поле file, которое указано в самих полях сделок данно воронки, но не указано программно. В самом поле есть параметр 'is_deleted' из-за которого тригерится амо и отдает 400 ошибку- {'field_id': 1889201, 'field_name': 'Скриншот заказа', 'field_code': None, 'field_type': 'file', 'values': [{'value': {'file_uuid': '6da97e19-5fff-46b9-9209-24443fa66285', 'version_uuid': '475abc88-9081-44d7-a933-be52e30c236f', 'file_name': 'vozvrat-strojmaterialov.jpg', 'file_size': 606942, 'is_deleted': False}}]}

вот логи:

Internal Server Error: /tg_bot/change_status_operation
Traceback (most recent call last):
  File "/home/miniapp/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/miniapp/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/miniapp/venv/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/miniapp/venv/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/miniapp/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/miniapp/venv/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/miniapp/venv/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/miniapp/venv/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/miniapp/venv/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/miniapp/venv/lib/python3.10/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/home/miniapp/balance/views.py", line 257, in change_operation_status_from_tg_bot
    BalanceOperations.change_status_operation(int(operation_id), int(is_completed))
  File "/home/miniapp/balance/models.py", line 224, in change_status_operation
    lead.save()
  File "/home/miniapp/venv/lib/python3.10/site-packages/amocrm/v2/model.py", line 49, in save
    self.update()
  File "/home/miniapp/venv/lib/python3.10/site-packages/amocrm/v2/model.py", line 63, in update
    self._manager.update(self.id, self._get_updated_data())
  File "/home/miniapp/venv/lib/python3.10/site-packages/amocrm/v2/manager.py", line 19, in update
    return self._interaction.update(object_id=object_id, data=data or kwargs)
  File "/home/miniapp/venv/lib/python3.10/site-packages/amocrm/v2/interaction.py", line 132, in update
    raise exceptions.ValidationError(response)
amocrm.v2.exceptions.ValidationError: {'validation-errors': [{'request_id': '0', 'errors': [{'code': 'FieldNotExpected', 'path': 'custom_fields_values.11.values.0.value.is_deleted', 'detail': 'This field was not expected.'}]}], 'title': 'Bad Request', 'type': 'https://httpstatus.es/400', 'status': 400, 'detail': 'Request validation failed'}

помогло только удаление этого поля из data при отправке обновленной сделки. такой костыль в amocrm/v2/interaction.py

 def update(self, object_id, data):
        path = "{}/{}".format(self._get_path(), object_id)
        if 'custom_fields_values' in data:
            new_data = []
            for item in data['custom_fields_values']:
                if 'field_type' in item and item['field_type'] != 'file':
                    new_data.append(item)
            data['custom_fields_values'] = new_data
        response, status = self.request("patch", path, data=data)
        if status == 400:
            raise exceptions.ValidationError(response)
        return response