Commits

David Jean Louis committed 9719a36

avoid a useless ajax GET request for retrieving dashboard preferences

  • Participants
  • Parent commits c55fbbc

Comments (0)

Files changed (4)

admin_tools/dashboard/templates/admin_tools/dashboard/dashboard.html

 <script type="text/javascript">
 // <![CDATA[
 jQuery(function($) {
-    jQuery.getJSON(
-        '{% url admin-tools-dashboard-get-preferences %}',
-        function(data) {
-            $('#{{ dashboard.get_id }}').dashboard({
-                'columns': {{ dashboard.columns }},
-                'load_preferences_function': function(options) {
-                    return data;
-                },
-                'save_preferences_function': function(options, preferences) {
-                    jQuery.post(
-                        '{% url admin-tools-dashboard-set-preferences %}',
-                        { data: JSON.stringify(preferences) }
-                    );
-                }
-            });
-         }
-    );
+    $('#{{ dashboard.get_id }}').dashboard({
+        'columns': {{ dashboard.columns }},
+        'load_preferences_function': function(options) {
+            return {% autoescape off %}{{ dashboard_preferences }}{% endautoescape %};
+        },
+        'save_preferences_function': function(options, preferences) {
+            jQuery.post(
+                '{% url admin-tools-dashboard-set-preferences %}',
+                { data: JSON.stringify(preferences) }
+            );
+        }
+    });
 });
 //]]>
 </script>

admin_tools/dashboard/templatetags/admin_tools_dashboard_tags.py

 from django import template
 from admin_tools.utils import get_media_url
 from admin_tools.dashboard.utils import get_dashboard
+from admin_tools.dashboard.models import DashboardPreferences
 
 register = template.Library()
 tag_func = register.inclusion_tag('admin_tools/dashboard/dummy.html', takes_context=True)
         dashboard = get_dashboard(context, location)
 
     dashboard.init_with_context(context)
+
+    try:
+        preferences = DashboardPreferences.objects.get(user=context['request'].user).data
+    except DashboardPreferences.DoesNotExist:
+        preferences = '{}'
+
     context.update({
         'template': dashboard.template,
         'dashboard': dashboard,
+        'dashboard_preferences': preferences,
         'split_at': math.ceil(float(len(dashboard.children))/float(dashboard.columns)),
         'media_url': get_media_url(),
         'has_disabled_modules': len([m for m in dashboard.children \

admin_tools/dashboard/urls.py

 from django.conf.urls.defaults import patterns, url
 
 urlpatterns = patterns('admin_tools.dashboard.views',
-    url(r'^get_preferences/$', 'get_preferences', name='admin-tools-dashboard-get-preferences'),
     url(r'^set_preferences/$', 'set_preferences', name='admin-tools-dashboard-set-preferences'),
 )

admin_tools/dashboard/views.py

     return direct_to_template(request, 'admin_tools/dashboard/preferences_form.html', {
         'form': form,   
     })
-
-
-@login_required
-@csrf_exempt
-def get_preferences(request):
-    """
-    Returns the dashboard preferences for the current user in json format.
-    If no preferences are found, the return value is an empty json object.
-    """
-    try:
-        preferences = DashboardPreferences.objects.get(user=request.user)
-        data = preferences.data
-    except DashboardPreferences.DoesNotExist:
-        data = '{}'
-    return HttpResponse(data, mimetype='application/json')
-