diff --git a/beat/web/experiments/api.py b/beat/web/experiments/api.py index 882b24e32112a3da6c3e64c51111ef2736e926c3..ea7d6d27553963aa1671e94e5e840319a35e49ce 100755 --- a/beat/web/experiments/api.py +++ b/beat/web/experiments/api.py @@ -29,6 +29,7 @@ import re import uuid import simplejson +import functools from django.conf import settings from django.shortcuts import get_object_or_404 @@ -104,6 +105,15 @@ class ListCreateExperimentsView(ListCreateContributionView): def _getStatusLabel(status): return [s for s in Experiment.STATUS if s[0] == status][0][1] + def _cmp(a, b): + """ + cmp is not available anymore in Python 3. This method is implemetend + as recommended in the documentation for this kind of use case. + Based on: + https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons + """ + return (a > b) - (a < b) + def _custom_compare(exp1, exp2): PENDING = _getStatusLabel(Experiment.PENDING) @@ -113,7 +123,7 @@ class ListCreateExperimentsView(ListCreateContributionView): if exp2['status'] != PENDING: return -1 elif exp1['creation_date'] != exp2['creation_date']: - return cmp(exp2['creation_date'], exp1['creation_date']) + return _cmp(exp1['creation_date'], exp2['creation_date']) else: return 1 elif exp2['status'] == PENDING: @@ -123,13 +133,13 @@ class ListCreateExperimentsView(ListCreateContributionView): tier3_states = [_getStatusLabel(Experiment.DONE), _getStatusLabel(Experiment.FAILED)] if (exp1['status'] in tier3_states) and (exp2['status'] in tier3_states): - return cmp(exp2['end_date'], exp1['end_date']) + return _cmp(exp2['end_date'], exp1['end_date']) elif (exp1['status'] in tier3_states) and (exp2['status'] not in tier3_states): return -1 elif (exp1['status'] not in tier3_states) and (exp2['status'] in tier3_states): return 1 - return cmp(exp1['start_date'], exp2['start_date']) + return _cmp(exp1['start_date'], exp2['start_date']) # Retrieve the experiments fields_to_return_original = self.get_serializer_fields(request) @@ -140,8 +150,10 @@ class ListCreateExperimentsView(ListCreateContributionView): experiments = self.model.objects.from_author_and_public(request.user, author_name).select_related() serializer = self.get_serializer(experiments, many=True, fields=fields_to_return) - result = serializer.data - result.sort(_custom_compare) + if six.PY2: + result = sorted(serializer.data, cmp=_custom_compare) + else: + result = sorted(serializer.data, key=functools.cmp_to_key(_custom_compare)) processed_result = [] for experiment in result: