# Path: pyston/order/sorters.py
# class BaseSorter:
# 
#     def __init__(self, identifiers, direction):
#         self.identifiers = identifiers
#         self.direction = direction
# 
#     def update_queryset(self, qs):
#         return qs
# 
#     def get_order_term(self):
#         raise NotImplementedError
# 
# Path: pyston/order/managers.py
# class BaseParserModelOrderManager(BaseModelOrderManager):
# 
#     parsers = [DefaultOrderParser()]
# 
#     def _get_sorters(self, parsed_order_terms, resource, request):
#         """
#         Converts order terms to sorter classes
#         """
#         sorters = []
#         for ordering_term in parsed_order_terms:
#             try:
#                 sorters.append(self.get_sorter(ordering_term.identifiers, ordering_term.direction, resource, request))
#             except OrderIdentifierError:
#                 raise RestException(
#                     mark_safe(ugettext('Invalid identifier of ordering "{}"').format(ordering_term.source))
#                 )
#         return sorters
# 
#     def _convert_order_terms(self, sorters):
#         """
#         Converts sorters to the django query order strings.
#         """
#         return [sorter.get_order_term() for sorter in sorters]
# 
#     def _update_queryset(self, qs, sorters):
#         """
#         Update queryset for extra sorter class (it is used for annotations before ordering)
#         """
#         for sorter in sorters:
#             qs = sorter.update_queryset(qs)
#         return qs
# 
#     def _sort_queryset(self, qs, terms):
#         raise NotImplementedError
# 
#     def _sort_with_parser(self, parser, resource, qs, request):
#         try:
#             parsed_order_terms = parser.parse(request)
#             sorters = self._get_sorters(parsed_order_terms or (), resource, request)
#             qs = self._update_queryset(qs, sorters)
#             return self._sort_queryset(qs, self._convert_order_terms(sorters)) if sorters else qs
#         except OrderParserError as ex:
#             raise RestException(ex)
# 
#     def sort(self, resource, qs, request):
#         for parser in self.parsers:
#             qs = self._sort_with_parser(parser, resource, qs, request)
#         return qs
# 
# Path: pyston/order/utils.py
# DIRECTION = Enum(
#     'ASC',
#     'DESC',
# )


from pyston.order.sorters import BaseSorter
from pyston.order.managers import BaseParserModelOrderManager
from pyston.order.utils import DIRECTION


class DynamoSorter(BaseSorter):

    def get_order_term(self):
        return self.direction == DIRECTION.ASC


class DynamoOrderManager(BaseParserModelOrderManager):

    def _sort_queryset(self, qs, terms):
        for term in terms:
            qs = qs.set_scan_index_forward(term)
        return qs

    def _get_sorter_from_model(self, identifiers_prefix, identifiers, direction, model, resource, request,
                               order_fields_rfs):
        if len(identifiers) == 1:
            current_identifier = self._get_real_field_name(resource, identifiers[0])

# Path: pyston/order/sorters.py
# class BaseSorter:
# 
#     def __init__(self, identifiers, direction):
#         self.identifiers = identifiers
#         self.direction = direction
# 
#     def update_queryset(self, qs):
#         return qs
# 
#     def get_order_term(self):
#         raise NotImplementedError
# 
# Path: pyston/order/managers.py
# class BaseParserModelOrderManager(BaseModelOrderManager):
# 
#     parsers = [DefaultOrderParser()]
# 
#     def _get_sorters(self, parsed_order_terms, resource, request):
#         """
#         Converts order terms to sorter classes
#         """
#         sorters = []
#         for ordering_term in parsed_order_terms:
#             try:
#                 sorters.append(self.get_sorter(ordering_term.identifiers, ordering_term.direction, resource, request))
#             except OrderIdentifierError:
#                 raise RestException(
#                     mark_safe(ugettext('Invalid identifier of ordering "{}"').format(ordering_term.source))
#                 )
#         return sorters
# 
#     def _convert_order_terms(self, sorters):
#         """
#         Converts sorters to the django query order strings.
#         """
#         return [sorter.get_order_term() for sorter in sorters]
# 
#     def _update_queryset(self, qs, sorters):
#         """
#         Update queryset for extra sorter class (it is used for annotations before ordering)
#         """
#         for sorter in sorters:
#             qs = sorter.update_queryset(qs)
#         return qs
# 
#     def _sort_queryset(self, qs, terms):
#         raise NotImplementedError
# 
#     def _sort_with_parser(self, parser, resource, qs, request):
#         try:
#             parsed_order_terms = parser.parse(request)
#             sorters = self._get_sorters(parsed_order_terms or (), resource, request)
#             qs = self._update_queryset(qs, sorters)
#             return self._sort_queryset(qs, self._convert_order_terms(sorters)) if sorters else qs
#         except OrderParserError as ex:
#             raise RestException(ex)
# 
#     def sort(self, resource, qs, request):
#         for parser in self.parsers:
#             qs = self._sort_with_parser(parser, resource, qs, request)
#         return qs
# 
# Path: pyston/order/utils.py
# DIRECTION = Enum(
#     'ASC',
#     'DESC',
# )


# Path: pyston/contrib/dynamo/order.py
from pyston.order.sorters import BaseSorter
from pyston.order.managers import BaseParserModelOrderManager
from pyston.order.utils import DIRECTION



class DynamoSorter(BaseSorter):

    def get_order_term(self):
        return self.direction == DIRECTION.ASC


class DynamoOrderManager(BaseParserModelOrderManager):

    def _sort_queryset(self, qs, terms):
        for term in terms:
            qs = qs.set_scan_index_forward(term)
        return qs

    def _get_sorter_from_model(self, identifiers_prefix, identifiers, direction, model, resource, request,
                               order_fields_rfs):
        if len(identifiers) == 1:
            current_identifier = self._get_real_field_name(resource, identifiers[0])

            if current_identifier == resource.get_range_key():