Ejemplo n.º 1
0
 def _convert_logical_conditions(self, condition, resource, request):
     """
     Method that recursive converts condition tree to the django models Q objects.
     """
     if condition.is_composed and condition.operator_slug == LOGICAL_OPERATORS.NOT:
         return ~Q(self._convert_logical_conditions(condition.condition_right, resource, request))
     elif condition.is_composed and condition.operator_slug == LOGICAL_OPERATORS.AND:
         return Q(
             self._convert_logical_conditions(condition.condition_left, resource, request),
             self._convert_logical_conditions(condition.condition_right, resource, request)
         )
     elif condition.is_composed and condition.operator_slug == LOGICAL_OPERATORS.OR:
         return Q(
             self._convert_logical_conditions(condition.condition_left, resource, request) |
             self._convert_logical_conditions(condition.condition_right, resource, request)
         )
     else:
         try:
             return self.get_filter(condition.identifiers, resource, request).get_q(
                 condition.value, condition.operator_slug, request
             )
         except FilterIdentifierError:
             raise RESTException(
                 mark_safe(ugettext('Invalid identifier of condition "{}"').format(condition.source))
             )
         except FilterValueError as ex:
             raise RESTException(
                 mark_safe(ugettext('Invalid value of condition "{}". {}').format(condition.source, ex))
             )
         except OperatorFilterError:
             raise RESTException(
                 mark_safe(ugettext('Invalid operator of condition "{}"').format(condition.source))
             )
Ejemplo n.º 2
0
 def order(self, resource, qs, request):
     try:
         parsed_order_terms = self.parser.parse(request)
         return qs.order_by(
             *self._convert_order_terms(parsed_order_terms, resource, request)
         ) if parsed_order_terms else qs
     except OrderParserError as ex:
         raise RESTException(ex)
Ejemplo n.º 3
0
 def get_requested_fields(self, resource, request):
     try:
         parsed_requested_rfs = self.parser.parse(request)
         if parsed_requested_rfs is None:
             return None
         else:
             return parsed_requested_rfs
     except RESTException as ex:
         raise RESTException(ex)
Ejemplo n.º 4
0
 def filter(self, resource, qs, request):
     try:
         parsed_conditions = self.parser.parse(request)
         return (qs.filter(pk__in=qs.filter(
             self._convert_logical_conditions(parsed_conditions, resource,
                                              request)).values('pk'))
                 if parsed_conditions else qs)
     except FilterParserError as ex:
         raise RESTException(ex)
Ejemplo n.º 5
0
 def sort(self, resource, qs, request):
     try:
         parsed_order_terms = self.parser.parse(request)
         sorters = self._get_sorters(parsed_order_terms or (), resource,
                                     request)
         qs = self._update_queryset(qs, sorters)
         return qs.order_by(
             *self._convert_order_terms(sorters)) if sorters else qs
     except OrderParserError as ex:
         raise RESTException(ex)
Ejemplo n.º 6
0
 def filter(self, resource, qs, request):
     try:
         parsed_conditions = self.parser.parse(request)
         if parsed_conditions:
             q = self._convert_logical_conditions(parsed_conditions, resource, request)
             if self._is_required_distinct(qs, q):
                 return qs.filter(pk__in=qs.filter(q).values('pk'))
             else:
                 return qs.filter(q)
         else:
             return qs
     except FilterParserError as ex:
         raise RESTException(ex)
Ejemplo n.º 7
0
    def post(self):
        if not self.request.data:
            raise RESTException(ugettext('Missing data'))
        form = self.get_form_class()(**self.get_form_kwargs())

        errors = form.is_invalid()
        if errors:
            self._unsucessful_login(self.request)
            return RESTErrorResponse(errors)

        self._sucessful_login(self.request)
        self._login(form.get_user(), not form.is_permanent(), form)
        return {'token': self.request.token.key, 'user': form.get_user()}
Ejemplo n.º 8
0
 def _convert_order_terms(self, parsed_order_terms, resource, request):
     """
     Method that converts order terms to the django query order strings.
     """
     ordering_strings = []
     for ordering_term in parsed_order_terms:
         try:
             ordering_strings.append('{}{}'.format(
                 '-' if ordering_term.direction == DIRECTION.DESC else '',
                 self.get_order_string(ordering_term.identifiers, resource, request)
             ))
         except OrderIdentifierError:
             raise RESTException(
                 mark_safe(ugettext('Invalid identifier of ordering "{}"').format(ordering_term.source))
             )
     return ordering_strings
Ejemplo n.º 9
0
 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