Repository: translate/pootle

Issue:
Coalesce in get_units is creating bad sql
Im seeing errors in get_units, regarding bad sql

this is the dump of the sql in question, seems like theres a bracket missing - not sure how that has happened, but im figuring something to do with COALESCE

``` sql
SELECT DISTINCT `pootle_store_unit`.`id`, `pootle_store_store`.`pootle_path`,                         ) FROM `pootle_store_unit` INNER JOIN `pootle_store_store` ON ( `pootle_store_unit`.`store_id` = `pootle_store_store`.`id` ) INNER JOIN `pootle_app_translationproject` ON ( `pootle_store_store`.`translation_project_id` = `pootle_app_translationproject`.`id` ) INNER JOIN `pootle_app_project` ON ( `pootle_app_translationproject`.`project_id` = `pootle_app_project`.`id` ) INNER JOIN `pootle_app_language` ON ( `pootle_app_translationproject`.`language_id` = `pootle_app_language`.`id` ) INNER JOIN `pootle_store_qualitycheck` ON ( `pootle_store_unit`.`id` = `pootle_store_qualitycheck`.`unit_id` ) WHERE (`pootle_store_unit`.`state` > -100 AND `pootle_app_project`.`disabled` = False AND `pootle_app_language`.`code` = ka AND `pootle_app_project`.`code` = firefox AND `pootle_store_store`.`pootle_path` LIKE BINARY /ka/firefox/toolkit/% AND `pootle_store_qualitycheck`.`category` = 100 AND `pootle_store_qualitycheck`.`false_positive` = False) ORDER BY (
                            SELECT
                              COALESCE(MAX(virtualfolder_virtualfolder.priority), 1)
                            FROM virtualfolder_virtualfolder
                            INNER JOIN virtualfolder_virtualfolder_units
                            ON virtualfolder_virtualfolder.id =
                              virtualfolder_virtualfolder_units.virtualfolder_id
                            WHERE virtualfolder_virtualfolder_units.unit_id =
                              pootle_store_unit.id
                        ) DESC LIMIT 21

```

This will be fixed in the move to a search backend, but im thinking that as we no longer need to do this (vfolder.priority is denormalized) we should fix first



Discussion:

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
@@ -288,26 +288,6 @@ def get_step_query(request, units_queryset):
             if sort_by is not None:
                 if sort_on in SIMPLY_SORTED:
                     if sort_by == 'priority':
-                        # TODO: Replace the following extra() with Coalesce
-                        # https://docs.djangoproject.com/en/1.8/ref/models/database-functions/#coalesce
-                        # once we drop support for Django<1.8.x:
-                        # .annotate(
-                        #     sort_by_field=Coalesce(
-                        #         Max("vfolders__priority"),
-                        #         Value(1)
-                        #     )
-                        # ).order_by("-sort_by_field")
-                        match_queryset = match_queryset.extra(select={'sort_by_field': """
-                            SELECT
-                              COALESCE(MAX(virtualfolder_virtualfolder.priority), 1)
-                            FROM virtualfolder_virtualfolder
-                            INNER JOIN virtualfolder_virtualfolder_units
-                            ON virtualfolder_virtualfolder.id =
-                              virtualfolder_virtualfolder_units.virtualfolder_id
-                            WHERE virtualfolder_virtualfolder_units.unit_id =
-                              pootle_store_unit.id
-                        """}).extra(order_by=['-sort_by_field'])
-                    else:
                         match_queryset = match_queryset.order_by(sort_by)
                 else:
                     # Omit leading `-` sign


Test patch: