Update utils.py to improve build_paq_cmd()

Update `utils.build_paq_cmd()` to convert all elements in the args list to their javascript counter-parts. This will recursively convert the keys and values of a dictionary until there are no more nested dictionaries and will also convert all the elements of a list as well.

Also made it so things like booleans, lists, and  dictionaries are not surrounded in quotation marks so they're treated like a normal javascript object. If that behavior needs changed. Please let me know
This commit is contained in:
SilverStrings (Matt) 2021-07-12 18:03:02 -04:00 committed by GitHub
parent 3761e7308a
commit 85c651f6f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
"""
Utility function for django-analytical.
"""
from copy import deepcopy
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
@ -174,13 +174,49 @@ def build_paq_cmd(cmd, args=[]):
:Returns:
A complete '_paq.push([])' command in string form
"""
def __to_js_arg(arg):
"""
Turn the argument into a js variable.
True -> true
False -> false
"""
# Recursively handle dictionaries
if isinstance(arg, dict):
arg_cpy = deepcopy(arg)
for k, v in arg_cpy.items():
arg.pop(k)
arg[__to_js_arg(k)] = __to_js_arg(v)
return arg
# Handle bools
elif isinstance(arg, bool):
if arg:
arg = "true"
else:
arg = "false"
# Handle lists
elif isinstance(arg, list):
for elem_idx in range(len(arg)):
arg[elem_idx] = __to_js_arg(arg[elem_idx])
return arg
paq = "_paq.push(['%s', " % (cmd)
if len(args) > 0:
for arg_idx in range(len(args)):
current_arg = __to_js_arg(args[arg_idx])
no_quotes = type(current_arg) in [bool, dict, list]
if arg_idx == len(args)-1:
paq += "'%s'])" % (args[arg_idx])
if no_quotes:
segment = "%s])" % (current_arg)
else:
segment = "'%s'])" % (current_arg)
else:
paq += "'%s', " % (args[arg_idx])
if no_quotes:
segment = "%s, "% (current_arg)
else:
segment = "'%s', " % (current_arg)
paq += segment
else:
paq += "])"
return paq