Skip to content

Commit a7f04ee

Browse files
author
Tom Hoddes
committed
get_value_from_dict_using_formdata_key util. there might be some django way to do this already but I couldn't find it
1 parent 3f2dbad commit a7f04ee

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

sendgrid/utils/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@
3535

3636
# return emailMessage
3737

38+
def get_value_from_dict_using_formdata_key(key,dictionary):
39+
"""
40+
Example:
41+
key="newsletter[newsletter_id]"
42+
dict={"newsletter":{"newsletter_id": 123}}
43+
44+
returns 123
45+
"""
46+
#check if we need to go deeper
47+
if '[' in key and ']' in key:
48+
#get top level key e.g. topKey[nextKey][anotherKey] => topKey
49+
topKey = key.split('[')[0]
50+
#remove top level key, e.g. topKey[nextKey][anotherKey] => nextKey[anotherKey]
51+
topKeyRemoved = key[len(topKey)+1:].replace(']','',1)
52+
#recurse
53+
return get_value_from_dict_using_formdata_key(topKeyRemoved,dictionary[topKey])
54+
else:
55+
return dictionary[key]
56+
3857
def in_test_environment():
3958
"""
4059
Returns True if in a test environment, False otherwise.

0 commit comments

Comments
 (0)