Output page_params as a single JSON object

This is a lot cleaner, and also cuts about 50-70 ms off of page load time in
local testing (with lots of users), presumably because there's less work to be
done by the slow Django template engine.

(imported from commit 257b700238ee5d9a4ae00a53011ed5bce018124c)
This commit is contained in:
Keegan McAllister 2013-03-25 18:49:38 -04:00
parent 6d9aca5f1a
commit 7309d57067
2 changed files with 24 additions and 44 deletions

View File

@ -51,38 +51,13 @@
{% compressed_js 'app_debug' %}
{% endif %}
{# Insert parameters, which have been encoded with JSONEncoderForHTML. #}
<script type="text/javascript">
{% autoescape off %}
var page_params = {
{# Not escaped, because it's guaranteed by the model to be an integer. #}
initial_pointer: {{ user_profile.pointer }},
poll_timeout: {{ poll_timeout }},
fullname: "{{ user_profile.full_name|escapejs }}",
email: "{{ user_profile.user.email|escapejs }}",
domain: "{{ user_profile.realm.domain|escapejs }}",
have_initial_messages: {{ have_initial_messages|escapejs }},
desktop_notifications_enabled: {{ desktop_notifications_enabled|escapejs }},
enter_sends: {{ enter_sends|escapejs }},
debug_mode: {% if debug %} true {% else %} false {% endif %},
needs_tutorial: {{ needs_tutorial|escapejs }},
{# We use JSONEncoderForHTML to generate "streams". #}
stream_list: {{ streams }},
people_list: [
{% for person in people %}
{ 'email': "{{ person.email|escapejs }}",
'full_name': "{{ person.full_name|escapejs }}" },
{% endfor %}
]
};
var page_params = {{ page_params }};
{% endautoescape %}
</script>
{% endblock %}
{% block content %}
<div class="container-fluid">

View File

@ -387,7 +387,8 @@ def home(request):
num_messages = UserMessage.objects.filter(user_profile=user_profile).count()
# Brand new users get the tutorial
# Brand new users get the tutorial.
# Compute this here, before we set user_profile.pointer below.
needs_tutorial = settings.TUTORIAL_ENABLED and user_profile.pointer == -1
if user_profile.pointer == -1 and num_messages > 0:
@ -414,9 +415,23 @@ def home(request):
for profile in
UserProfile.objects.select_related().filter(realm=user_profile.realm)]
streams = simplejson.encoder.JSONEncoderForHTML().encode(gather_subscriptions(user_profile))
js_bool = lambda x: 'true' if x else 'false'
# Pass parameters to the client-side JavaScript code.
# These end up in a global JavaScript Object named 'page_params'.
page_params = simplejson.encoder.JSONEncoderForHTML().encode(dict(
debug_mode = settings.DEBUG,
poll_timeout = settings.POLL_TIMEOUT,
have_initial_messages = num_messages > 0,
stream_list = gather_subscriptions(user_profile),
people_list = people,
initial_pointer = user_profile.pointer,
fullname = user_profile.full_name,
email = user_profile.user.email,
domain = user_profile.realm.domain,
enter_sends = user_profile.enter_sends,
needs_tutorial = needs_tutorial,
desktop_notifications_enabled =
user_profile.enable_desktop_notifications,
))
try:
isnt_mit(user_profile.user.email)
@ -426,21 +441,11 @@ def home(request):
return render_to_response('zephyr/index.html',
{'user_profile': user_profile,
'page_params' : page_params,
'email_hash' : gravatar_hash(user_profile.user.email),
'people' : people,
'streams' : streams,
'poll_timeout': settings.POLL_TIMEOUT,
'debug' : settings.DEBUG,
'have_initial_messages':
js_bool(num_messages > 0),
'desktop_notifications_enabled':
js_bool(user_profile.enable_desktop_notifications),
'enter_sends':
js_bool(user_profile.enter_sends),
'show_debug':
settings.DEBUG and ('show_debug' in request.GET),
'show_invites': show_invites,
'needs_tutorial': js_bool(needs_tutorial)
'show_invites': show_invites
},
context_instance=RequestContext(request))