Skip to content

Commit

Permalink
Don't sort keys by default in tojson when rendering profile forms
Browse files Browse the repository at this point in the history
jinja2's tojson sorts keys in dicts by default. This was useful
in the time when python's dicts were not ordered. However, now that
dicts are ordered in python, this screws it up. Since profiles are
dicts, ordering *does* matter - they should be displayed to the user
in the order that the admin sets them. This allows template writers
to use `|tojson` on the profile_list (to be read by JS)
without worrying about ordering getting mangled. Template writers
can still sort keys by explicitly using `|dictsort` in their
template
  • Loading branch information
yuvipanda committed Sep 19, 2023
1 parent 9663b7e commit 07d4692
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2936,6 +2936,18 @@ def _render_options_form(self, profile_list):
)

env = Environment(loader=loader)

# jinja2's tojson sorts keys in dicts by default. This was useful
# in the time when python's dicts were not ordered. However, now that
# dicts are ordered in python, this screws it up. Since profiles are
# dicts, ordering *does* matter - they should be displayed to the user
# in the order that the admin sets them. This allows template writers
# to use `|tojson` on the profile_list (to be read by JS)
# without worrying about ordering getting mangled. Template writers
# can still sort keys by explicitly using `|dictsort` in their
# template
env.policies['json.dumps_kwargs'] = {'sort_keys': False}

if self.profile_form_template != "":
profile_form_template = env.from_string(self.profile_form_template)
else:
Expand Down

0 comments on commit 07d4692

Please sign in to comment.