Index out of range while using PUT method on flask_mongoengine API - flask

My code for the PUT method,
#app.route('/update_doc/<id>', methods=["PUT"])
def update_method(id):
body = request.get_json()
updated_document = db_collection.objects.get(id=id).update(**body)
#document.modify(**body)
return 'success', 200
I am getting the following error,
Traceback (most recent call last):
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/home/configcar_it/Documents/k8s/ec2_mongodb/ec2_mongodb_validator.py", line 119, in update_method
store = Store_collection.objects.get(id=id).update(**body)
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/document.py", line 610, in update
return self._qs.filter(**self._object_key).update_one(**kwargs)
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/queryset/base.py", line 631, in update_one
return self.update(
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/queryset/base.py", line 555, in update
update = transform.update(queryset._document, **update)
File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/queryset/transform.py", line 301, in update
field = cleaned_fields[-1]
IndexError: list index out of range
The remaining Post, Delete, Get methods are working.
I am giving the input through the postman.
Delete method
#app.route('/stores/<id>', methods=['DELETE'])
def delete_doc(id):
stores = Store_collection.objects.get(id=id).delete()
return 'successfully deleted.', 200

After browsing through the internet, I found out that I specified the field name as "type" in my MongoDB database. After understanding this, I changed "type" --> "types". Now it's working properly.
I understood this by reading the following GitHub issue --> https://github.com/MongoEngine/mongoengine/issues/1194.
Also, the same issue is similar to the word "size". Hope this might be helpful for someone.

Related

Diagnosing an Internal Server Error in GAE

Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~dkc-app/20180221t133719.407817416956265792/manage/admin.py", line 13, in get
applicants, applications = query.get_all_overview()
File "/base/data/home/apps/s~dkc-app/20180221t133719.407817416956265792/manage/query.py", line 52, in get_all_overview
applications = ndb.get_multi(application_keys)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3931, in get_multi
for future in get_multi_async(keys, **ctx_options)]
File "/base/alloc/tmpfs/dynamic_runtimes/python27/a7637d5531ec9deb_unzipped/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3916, in get_multi_async
return [key.get_async(**ctx_options) for key in keys]
AttributeError: 'NoneType' object has no attribute 'get_async'
This suddenly started happening a few weeks ago and I have not been able to figure out what happened. My best guess is that updating gcloud components caused it since nothing else changed.
try changing:
applications = ndb.get_multi(application_keys)
to
applications = ndb.get_multi([key for key in application_keys if key])
it looks like one of your keys is None

OneLogin SAML2 module throws an error `lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1`

I am trying to implement a SAML2 SSO functionality using the OneLogin SAML2 module for this. There is much info in the readme and also in the demo.
I have implemented the most of it already and I am testing my ACS endpoint using Samling tool.
I am able to receive the SAML response, but I am getting the mentioned above error at this point in my implementation.
The XML, which I receive looks fine and the first symbol is <. I do not understand, where the problem lies. Please help.
Here is the complete Traceback:
Internal Server Error: /auth/sso/saml2/
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/django/core/handlers/base.py", line 140, in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/usr/local/lib/python3.4/site-packages/debug_toolbar/middleware.py", line 78, in process_view
response = panel.process_view(request, view_func, view_args, view_kwargs)
File "/usr/local/lib/python3.4/site-packages/debug_toolbar/panels/profiling.py", line 151, in process_view
return self.profiler.runcall(view_func, *args, **view_kwargs)
File "/usr/local/lib/python3.4/cProfile.py", line 109, in runcall
return func(*args, **kw)
File "/code/authtoken/views.py", line 63, in sso_handler
resp = do_saml2(request)
File "/code/authtoken/sso/saml2/saml2.py", line 83, in do_saml2
auth.process_response()
File "/usr/local/lib/python3.4/site-packages/onelogin/saml2/auth.py", line 99, in process_response
response = OneLogin_Saml2_Response(self.__settings, self.__request_data['post_data']['SAMLResponse'])
File "/usr/local/lib/python3.4/site-packages/onelogin/saml2/response.py", line 39, in __init__
self.document = OneLogin_Saml2_XML.to_etree(self.response)
File "/usr/local/lib/python3.4/site-packages/onelogin/saml2/xml_utils.py", line 66, in to_etree
return OneLogin_Saml2_XML._parse_etree(xml)
File "/usr/local/lib/python3.4/site-packages/defusedxml/lxml.py", line 143, in fromstring
rootelement = _etree.fromstring(text, parser, base_url=base_url)
File "src/lxml/lxml.etree.pyx", line 3228, in lxml.etree.fromstring (src/lxml/lxml.etree.c:79609)
File "src/lxml/parser.pxi", line 1848, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:119128)
File "src/lxml/parser.pxi", line 1736, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:117808)
File "src/lxml/parser.pxi", line 1102, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:112052)
File "src/lxml/parser.pxi", line 595, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:105896)
File "src/lxml/parser.pxi", line 706, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:107604)
File "src/lxml/parser.pxi", line 635, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:106458)
File "<string>", line 1
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1
[2017/07/10 14:07:58] HTTP POST /auth/sso/saml2/ 500 [0.28, 172.17.0.1:53032]
I found it. The response xml must be base64 encoded. Then it is processed correctly.

python-selenium on aws lambda throws error when switch_back_to.default_content and try to select element

python-selenium using phantomjs used to open an iFrame inside the page, then after switching back to the default content(driver.switch_to.default_content()) does not work.
The following is a pseudo code snippet to explain the scenario.
frame = WebDriverWait(driver, wait_time).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.class_name")))
......
driver.switch_to.default_content()
print('Switched back to default content')
# Error occurs when line below is interpreted
driver.find_element_by_css_selector("div[title^='Test %s']" %(array_of_content[1]))
Following error occurs:-
Traceback (most recent call last):
File "/var/task/main.py", line 217, in test_method
driver.find_element_by_css_selector("div[title^='Test %s']" %(array_of_content[1]))
File "/var/task/selenium/webdriver/support/wait.py", line 71, in until
value = method(self._driver)
File "/var/task/selenium/webdriver/support/expected_conditions.py", line 213, in __call__
element = visibility_of_element_located(self.locator)(driver)
File "/var/task/selenium/webdriver/support/expected_conditions.py", line 78, in __call__
return _element_if_visible(_find_element(driver, self.locator))
File "/var/task/selenium/webdriver/support/expected_conditions.py", line 307, in _find_element
return driver.find_element(*by)
File "/var/task/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/var/task/selenium/webdriver/remote/webdriver.py", line 234, in execute
response = self.command_executor.execute(driver_command, params)
File "/var/task/selenium/webdriver/remote/remote_connection.py", line 401, in execute
return self._request(command_info[0], url, body=data)
File "/var/task/selenium/webdriver/remote/remote_connection.py", line 471, in _request
resp = opener.open(request, timeout=self._timeout)
File "/usr/lib64/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib64/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib64/python2.7/urllib2.py", line 1229, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib64/python2.7/urllib2.py", line 1202, in do_open
r = h.getresponse(buffering=True)
File "/var/task/raven/breadcrumbs.py", line 328, in getresponse
rv = real_getresponse(self, *args, **kwargs)
File "/usr/lib64/python2.7/httplib.py", line 1132, in getresponse
response.begin()
File "/usr/lib64/python2.7/httplib.py", line 453, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python2.7/httplib.py", line 417, in _read_status
raise BadStatusLine(line)
BadStatusLine: ''
Any ideas, what is happening.
As I am able to see the same error, even if I write driver.save_screenshot('test.png').
But it is not throwing any error on local machines, also tried to use AMI instance to replicate if something is there in concern Amazon Linux.

Flask error handler not able to handle Assertion Error

I am using Flask-RESTful for building REST api and webargs for parsing.
While defining resource, I want argument to be present, so I wrote required=True
For example:
class Name(Resource):
"""Retrieve ids corresponding to given names
Input entries: String
"""
args = {
'entries' : fields.Str(required=True),
}
#use_kwargs(args)
def get(self, entries):
# HTTP method GET
result = object.find_id(entries)
return jsonify(result)
now,
While performing unittesting for API, explicitly not specifying entries, it returns Assertion Error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1363, in handle_user_exception
assert exc_value is e
AssertionError
When I checked, exc_value is
TypeError("ValidationError({'entries': [u'Missing data for required field.']}, status_code=422, headers={}) is not JSON serializable",)
and e is
<UnprocessableEntity '422: Unprocessable Entity'>
Now,
I've these ways to handle this exception
#app.errorhandler(500)
#app.errorhandler(ValidationError)
#app.errorhandler(TypeError)
#app.errorhandler(UnprocessableEntity)
#app.errorhandler(422)
#app.errorhandler(Exception)
I don't know why even this is not working
#app.errorhandler(AssertionError)
Refered to this:
link
but wasn't able to solve
I am using
Flask(0.10.1)
Flask-Restful(0.3.5)
webargs(1.2.0)
I have hit this issue only today (even though I have been using Flask-RESTplus for almost a year now), and it came as a weird discovery. I see this issue only with Python 2.7 while Python 3.3, 3.4, 3.5 work fine.
Thus, I think it should be considered as a compatibility bug in either webargs or Flask, I'm not sure yet.
UPDATE: After some digging into the issue, I ended up in webargs and filed an issue with my troubleshooting there: https://github.com/sloria/webargs/issues/122
UPDATE 2: Here is my PR fixing this issue: https://github.com/sloria/webargs/pull/123

Logging fail in Appengine

I'm trying to do some importing to a DB in App Store 1.9.26, in python 2.7.9, and to do so,I would like to get some logging so I can inspect some variables and have a look on whjt is happening
import logging
[...]
updateDueDate = '2016.3.15'
fmt ='%Y.%m.%d'
fechalinea = datetime.datetime.strptime(updateDueDate,fmt)
fecha_aviso = avisoDB.modificacion.due_date.strftime(fmt)
logging.error = ('Date Call= %s, Date DB= %s' %(fecha_aviso, fechalinea))
Which should be pretty straightforward, but its not working. I think I mess up my python instalation or something that has nothing to do with my code, since the output I get from the devserver is:
TypeError("'str' object is not callable",)
TypeError("'str' object is not callable",)
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
req.respond()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
self.server.gateway(self).respond()
INFO 2015-11-22 16:44:48,554 module.py:809] default: "POST /admin/filemanager HTTP/1.1" 500 -
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 272, in __call__
return app(environ, start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
response_body = iter(application(environ, wrapped_start_response))
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 154, in __call__
response = self.handle_normal_request(environ)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 184, in handle_normal_request
self._PYTHON_LIB_DIR)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/runtime.py", line 152, in HandleRequest
error)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 329, in HandleRequest
return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 279, in Handle
logging.exception('')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 1609, in exception
error(msg, *args, **kwargs)
TypeError: 'str' object is not callable
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
req.respond()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
self.server.gateway(self).respond()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 272, in __call__
return app(environ, start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
response_body = iter(application(environ, wrapped_start_response))
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 154, in __call__
response = self.handle_normal_request(environ)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 184, in handle_normal_request
self._PYTHON_LIB_DIR)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/runtime.py", line 152, in HandleRequest
error)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 329, in HandleRequest
return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 279, in Handle
logging.exception('')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 1609, in exception
error(msg, *args, **kwargs)
TypeError: 'str' object is not callable
INFO 2015-11-22 16:44:48,647 module.py:809] default: "GET /favicon.ico HTTP/1.1" 200 15086
Tried to upgrade python and GAE launcher, google for this problem, but with all the different options I get the same errors, and I'm pretty sure it has nothing to do with my code, but... any one has any idea on what is happening?
Thanks
logging.error = assigns the right-hand-side (in your case, a string) to attribute error of module logging, replacing its previous value (which was a function).
When any Python code (yours or in the SDK) later calls logging.error (which you've set to a string), of course it will produce TypeError("'str' object is not callable",)!
I think I mess up my python instalation
Yes you did, by assigning a string to logging.error.
or something that has nothing to do with my code
It has everything to do with your code -- it's your code that does the mistaken assigning!