Repository: translate/pootle

Issue:
Priority sorting has random file order where it should be alphabetical
In a server with vfolders, when clicking on an incomplete count, it automatically sets the priority sorting to the editor view. The sorting, in turn, exposes units in a very illogical way: sorted by priority but scattered across files. This results in totally confusing surrounding units.

See an example in the following screenshot — note all units in there have _1.0_ priority.
<img width="581" alt="weird_priority" src="https://cloud.githubusercontent.com/assets/16768/9954920/2d575ada-5ded-11e5-9e3d-551ffd93abcf.png">



Discussion:
Just to be clear this is what we want:

1) Sort on priority
2) Then sort on directory and files in alphabetical order.

This can be seen live in http://mozilla.locamotion.org/eu/firefox/translate/dom/#filter=all&sort=priority&unit=8708523 where `/eu/firefox/dom/chrome/netError.dtd.po` appears twice.


Resolving patch:
diff --git a/pootle/apps/pootle_store/views.py b/pootle/apps/pootle_store/views.py
--- a/pootle/apps/pootle_store/views.py
+++ b/pootle/apps/pootle_store/views.py
@@ -168,7 +168,8 @@ def get_step_query(request, units_queryset):
             sort_by = ALLOWED_SORTS[sort_on].get(sort_by_param, None)
             if sort_by is not None:
                 if sort_on in SIMPLY_SORTED:
-                    match_queryset = match_queryset.order_by(sort_by)
+                    match_queryset = match_queryset.order_by(
+                        sort_by, "store__pootle_path", "index")
                 else:
                     # Omit leading `-` sign
                     if sort_by[0] == '-':
@@ -183,7 +184,7 @@ def get_step_query(request, units_queryset):
                     # (unless PostreSQL is used and `distinct(field_name)`)
                     match_queryset = match_queryset \
                         .annotate(sort_by_field=Max(max_field)) \
-                        .order_by(sort_order)
+                        .order_by(sort_order, "store__pootle_path", "index")
 
             units_queryset = match_queryset
 
@@ -377,7 +378,7 @@ def get_units(request):
 
     if is_initial_request:
         sort_by_field = None
-        if len(step_queryset.query.order_by) == 1:
+        if len(step_queryset.query.order_by) > 2:
             sort_by_field = step_queryset.query.order_by[0]
 
         sort_on = None
@@ -387,25 +388,10 @@ def get_units(request):
                 break
 
         if sort_by_field is None or sort_on == 'units':
-            # Since `extra()` has been used before, it's necessary to
-            # explicitly request the `store__pootle_path` field. This is a
-            # subtetly in Django's ORM.
-            uid_list = [u['id'] for u
-                        in step_queryset.values('id', 'store__pootle_path')]
+            uid_list = list(step_queryset.values_list('id', flat=True))
         else:
-            # Not using `values_list()` here because it doesn't know about all
-            # existing relations when `extra()` has been used before in the
-            # queryset. This affects annotated names such as those ending in
-            # `__max`, where Django thinks we're trying to lookup a field on a
-            # relationship field. That's why `sort_by_field` alias for `__max`
-            # is used here. This alias must be queried in
-            # `values('sort_by_field', 'id')` with `id` otherwise
-            # Django looks for `sort_by_field` field in the initial table.
-            # https://code.djangoproject.com/ticket/19434
             uid_list = [u['id'] for u
-                        in step_queryset.values('id', 'sort_by_field',
-                                                'store__pootle_path')]
-
+                        in step_queryset.values('id', 'sort_by_field')]
         if len(uids) == 1:
             try:
                 uid = uids[0]
@@ -413,6 +399,7 @@ def get_units(request):
                 begin = max(index - chunk_size, 0)
                 end = min(index + chunk_size + 1, len(uid_list))
                 uids = uid_list[begin:end]
+                units = step_queryset[begin:end]
             except ValueError:
                 raise Http404  # `uid` not found in `uid_list`
         else:


Test patch: