Strange deform/colander behaviour "string indices must be integers" - pyramid

So I have the following working code, that I have been using for the past few months
class UserSchema(colander.MappingSchema):
dob = colander.SchemaNode(
colander.Date(),
title='Date of birth:')
if 'submit' in request.POST:
controls = request.POST.items()
try:
appstruct = myform.validate(controls)
except ValidationFailure, e:
return {'form':e.render(), 'values': False}
Now this is a basic date picker, as shown here:
http://deform2demo.repoze.org/dateinput/
But all of a sudden, when using this widget I get the error:
Traceback (most recent call last):
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.9-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler
result = handler(request)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 82, in tm_tween
reraise(*exc_info)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 63, in tm_tween
response = handler(request)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid/router.py", line 161, in handle_request
response = view_callable(context, request)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid/config/views.py", line 347, in rendered_view
result = view(context, request)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/pyramid/config/views.py", line 493, in _requestonly_view
response = view(request)
File "/home/luke/pyramids/getwork2day.co.uk/getwork2day/views/signup.py", line 244, in user_signup
appstruct = myform.validate(controls)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/deform-2.0a2-py2.7.egg/deform/field.py", line 636, in validate
return self.validate_pstruct(pstruct)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/deform-2.0a2-py2.7.egg/deform/field.py", line 661, in validate_pstruct
cstruct = self.deserialize(pstruct)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/deform-2.0a2-py2.7.egg/deform/field.py", line 512, in deserialize
return self.widget.deserialize(self, pstruct)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/deform-2.0a2-py2.7.egg/deform/widget.py", line 1274, in deserialize
result[name] = subfield.deserialize(subval)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/deform-2.0a2-py2.7.egg/deform/field.py", line 512, in deserialize
return self.widget.deserialize(self, pstruct)
File "/home/luke/virts/pyramid-1.4.5/lib/python2.7/site-packages/deform-2.0a2-py2.7.egg/deform/widget.py", line 551, in deserialize
date = pstruct['date'].strip()
TypeError: string indices must be integers

At least dump current form controls to logging and post it here. Try to be more verbose & clear with your code examples and do not mix up colander schema code and pyramid view code.
if 'submit' in request.POST:
controls = request.POST.items()
try:
appstruct = myform.validate(controls)
except ValidationFailure, e:
log.debug('form validation fails for %r' % controls)
return {'form':e.render(), 'values': False}

Related

cannot unpack non-iterable bool object(when trying to filter result for user registered in auth>User module)

I’m creating a django based web app and I have used Auth > User model (without any customization except password hiding) and for user detail I have created another model ChangeShift. and model based form.
Case 1: When I am using alluser = ShiftChange.objects.all() I'm able to see all the data available in ShiftChange model.
Case 2: Now I want that when user is logging, he should be able to see data related to his name and I'm using this:
def retrieve_view(request):
# alluser = ShiftChange.objects.all()
alluser = ShiftChange.objects.filter(ShiftChange.ldap_id == request.user.username)
return render(request, 'apple/shift2.html', {'alluser': alluser})
For this code I am getting error:
cannot unpack non-iterable bool object
Error Traceback:
[21/Sep/2020 11:13:18] "GET /static/css/auth.css HTTP/1.1" 404 1662
Internal Server Error: /alldata/
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/shaileshyadaav/djangoshaileshdurga/mapsIT/apple/views.py", line 53, in retrieve_view
alluser = ShiftChange.objects.filter(ShiftChange.ldap_id == request.user.username)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 962, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 969, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1358, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1255, in build_filter
arg, value = filter_expr
TypeError: cannot unpack non-iterable bool object
The line:
alluser = ShiftChange.objects.filter(
ShiftChange.ldap_id == request.user.username)
is the error. The part ShiftChange.ldap_id == request.user.username is an expression that evaluates to True or False, and then your code becomes equivalent to
alluser = ShiftChange.objects.filter(True)
or
alluser = ShiftChange.objects.filter(False)
and that is probably not what you want.
Try using a single = sign and use a valid left side kwarg (because ShiftChange.ldap_id with the dot in the middle is not valid here).
So your code may end up similar to this:
alluser = ShiftChange.objects.filter(ldap_id=request.user.username)

How do I find the specific document insert_many() fails on?

if (constant.gc in file.sheet_names):
coll = db[constant.gc]
print("Adding to " + constant.gc + " database")
df = file.parse(constant.gc)
df = clean(df)
data_dict = df.to_dict('r')
try:
result = coll.insert_many(data_dict)
nr_inserts = len(result.inserted_ids)
print(str(nr_inserts) + "Cases added to database")
except pymongo.errors.BulkWriteError as bwe:
nr_inserts = bwe.details["nInserted"]
print(nr_inserts)
I keep getting a NaTType error and I can't find which row of the dataframe has the blank date. Unfortunately, it's off of a 39k row Excel file. So, just looking through isn't going to help. I tried an except that theoretically could tell me how many were successfully inserted until the error, and therefore give me a hint on where to look, but it hasn't printed.
The error looks like this:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "dataimport.py", line 71, in importFromExcel
result = coll.insert_many(data_dict)
File "C:\Python38\lib\site-packages\pymongo\collection.py", line 758, in insert_many
blk.execute(write_concern, session=session)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 511, in execute
return self.execute_command(generator, write_concern, session)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 345, in execute_command
client._retry_with_session(
File "C:\Python38\lib\site-packages\pymongo\mongo_client.py", line 1384, in _retry_with_session
return func(session, sock_info, retryable)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 339, in retryable_bulk
self._execute_command(
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 295, in _execute_command
result, to_send = bwc.execute(ops, client)
File "C:\Python38\lib\site-packages\pymongo\message.py", line 898, in execute
request_id, msg, to_send = self._batch_command(docs)
File "C:\Python38\lib\site-packages\pymongo\message.py", line 890, in _batch_command
request_id, msg, to_send = _do_bulk_write_command(
File "C:\Python38\lib\site-packages\pymongo\message.py", line 1382, in _do_bulk_write_command
return _do_batched_op_msg(
File "C:\Python38\lib\site-packages\pymongo\message.py", line 1307, in _do_batched_op_msg
return _batched_op_msg(
File "pandas\_libs\tslibs\nattype.pyx", line 64, in pandas._libs.tslibs.nattype._make_error_func.f
ValueError: NaTType does not support utcoffset
At a guess, ValueError and BulkWriteError are not the same, so nInserted never prints. Does anyone have an idea of how to get the number of successful inserts before the failure?
I doubt that any inserts are performed as the error is likely occurring before the data is passed to mongodb to insert.
In any case, if you want to hunt down which row in the dataframe has the NaT value, try: (subsitute 'date' for your column containing the date)
null_df = df[pd.isnull(df['date'])]
print(null_df)
To remove null dated items use:
df = df[pd.notnull(df['date'])]

How do I search for a Django model by a primary key that doesn't match its type without throwing an error?

I'm using Django 3 and Python 3.7. I have a model (MySql 8 backed table) that has integer primary keys. I have code that searches for such models like so
state = State.objects.get(pk=locality['state'])
The issue is if "locality['state']" contains an empty string, I get the below error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1768, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/tests/test_serializers.py", line 132, in test_coop_create_with_incomplete_data
assert not serializer.is_valid(), serializer.errors
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 234, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 433, in run_validation
value = self.to_internal_value(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 490, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/fields.py", line 565, in run_validation
value = self.to_internal_value(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/relations.py", line 519, in to_internal_value
return [
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/relations.py", line 520, in <listcomp>
self.child_relation.to_internal_value(item)
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/serializers.py", line 26, in to_internal_value
state = State.objects.get(pk=locality['state'])
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 404, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1337, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1362, in _add_q
child_clause, needed_inner = self.build_filter(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1298, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1155, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/lookups.py", line 72, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1770, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got ''.
Is there a more "Django" way to search for an object without an error being thrown if the object doesn't exist? I could do this
state = None if str(type(locality['state'])) != "<class 'int'>" else State.objects.get(pk=locality['state'])
but this seems unnecessarily wordy and not how Django was intended to be used.
I would choose Ask forgiveness not permission strategy
try:
state = State.objects.get(pk=int(locality['state']))
except ValueError:
state = None
You could use a logical AND to validate the dict value before using it to look up the data.
state = locality['state'] and State.objects.get(pk=locality['state'])

Pygsheets update_cells() not working

I am trying to update a range of 3 cells horizontally using pygsheets but I am having some issues
The Error:
Traceback (most recent call last):
File "C:\Users\Art\Desktop\Python\Plain\General_testing.py", line 12, in <module>
wks.update_cells('I{0}:K{0}'.format(rows),[output['Name'], output['Age'], output['State']])
File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\pygsheets\worksheet.py", line 431, in update_cells
self.client.sh_update_range(self.spreadsheet.id, body, self.spreadsheet.batch_mode, parse=parse)
File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\pygsheets\client.py", line 352, in sh_update_range
self._execute_request(spreadsheet_id, final_request, batch)
File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\pygsheets\client.py", line 418, in _execute_request
response = request.execute()
File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1P2FvtuOLWZGgVIp-D9ROeBqexXQt1eNv8tSxZdwGrlw/values/Sheet1%21I19%3AK19?valueInputOption=USER_ENTERED&alt=json returned "Invalid value at 'data.values[0]' (type.googleapis.com/google.protobuf.ListValue), "Joe"
Invalid value at 'data.values[1]' (type.googleapis.com/google.protobuf.ListValue), "19"
Invalid value at 'data.values[2]' (type.googleapis.com/google.protobuf.ListValue), "NY"">
Simplified version of my code that produces the same error:
import pygsheets
gc = pygsheets.authorize(service_file='secret.json')
sh = gc.open('TestSheet')
wks = sh.sheet1
row = 1
output = {}
output['Name'] = 'Joe'
output['Age'] = '19'
output['State'] = 'NY'
wks.update_cells('I{0}:K{0}'.format(row),[output['Name'], output['Age'], output['State']])
The update cells take a 2D matrix (list of list) as input for values not a list
wks.update_cells('I{0}:K{0}'.format(row),[[output['Name'], output['Age'], output['State']] ] )
see the docs here

Tornado OAuth2 Error in get_argument() (Facebook change)

I have a problem that I can't work out. My code worked for localhost however how I have a proper domain setup i'm getting some strange problems trying to login with facebook. I have since moved to python 3.6
I have the following tornado setup code:
handlers = [
(r"/facebookAuth",FBAuth),
# other handlers...
]
# Settings dict for Application
settings = {
# static handler
# Set specific HTTP404 errors to Error404 Class
"default_handler_class": Error404,
"cookie_secret":"xxx",
"facebook_redirect_uri":"https://www.example.com/facebookAuth",
"facebook_secret":"xxx",
"facebook_app_id":"xxx",
}
class FBAuth(BaseHandler,tornado.auth.FacebookGraphMixin):
async def get(self):
if self.get_argument("code", False):
print("not code")
user = await self.get_authenticated_user(redirect_uri=self.settings["facebook_redirect_uri"],
client_id=self.settings["facebook_app_id"],
client_secret=self.settings["facebook_secret"],
code=self.get_argument("code"))
print("******")
print(user)
firstName=user["first_name"]
lastName=user["last_name"]
# set cookie and start up code
else:
print("code")
await self.authorize_redirect(redirect_uri=self.settings["facebook_redirect_uri"],
client_id=self.settings["facebook_app_id"],
scope=["email","public_profile"])
I can't work out the result of the code. It shows:
code
not code
and crashes with the following:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/web.py", line 1474, in _execute
result = yield result
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/gen.py", line 1045, in run
value = future.result()
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/concurrent.py", line 237, in result
raise_exc_info(self._exc_info)
File "<string>", line 3, in raise_exc_info
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/gen.py", line 1051, in run
yielded = self.gen.throw(*exc_info)
File "<string>", line 6, in _wrap_awaitable
File "/home/cs/charliesays/authHandlers.py", line 13, in get
code=self.get_argument("code"))
File "<string>", line 3, in __await__
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/gen.py", line 1045, in run
value = future.result()
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/concurrent.py", line 237, in result
raise_exc_info(self._exc_info)
File "<string>", line 3, in raise_exc_info
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/stack_context.py", line 314, in wrapped
ret = fn(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/tornado-4.5.dev1-py3.6-linux-x86_64.egg/tornado/auth.py", line 983, in _on_access_token
"access_token": args["access_token"][-1],
KeyError: 'access_token'
It seems there is a problem with the:
code=self.get_argument("code") in the call to get_authenticated_user()

Resources