def test_correct_number_of_requests_overlapping_windows(self):
     r_dict = self.req.as_dict()
     r_dict['cadence'] = {
         'start': datetime.datetime(2016, 9, 1, tzinfo=timezone.utc),
         'end': datetime.datetime(2016, 9, 2, tzinfo=timezone.utc),
         'period': 1.0,
         'jitter': 2.0
     }
     requests = expand_cadence_request(r_dict)
     self.assertEqual(len(requests), 5)
 def test_correct_number_of_requests_large_cadence(self):
     r_dict = self.req.as_dict()
     r_dict['cadence'] = {
         'start': datetime.datetime(2016, 9, 1, tzinfo=timezone.utc),
         'end': datetime.datetime(2016, 10, 1, tzinfo=timezone.utc),
         'period': 24.0,
         'jitter': 12.0
     }
     requests = expand_cadence_request(r_dict)
     self.assertEqual(len(requests), 26)
    def cadence(self, request):
        """ Given a well-formed RequestGroup containing a single Request, return a new RequestGroup containing many requests
        generated by the cadence function.
        """
        request_serializer = self.get_request_serializer(
            data=request.data, context={'request': request})
        expanded_requests = []
        if request_serializer.is_valid():
            cadence_request = request_serializer.validated_data.get(
                'requests')[0]
            if isinstance(cadence_request,
                          dict) and cadence_request.get('cadence'):
                cadence_request_serializer = import_string(
                    settings.SERIALIZERS['requestgroups']['CadenceRequest'])(
                        data=cadence_request)
                if cadence_request_serializer.is_valid():
                    expanded_requests.extend(
                        expand_cadence_request(
                            cadence_request_serializer.validated_data,
                            request.user.is_staff))
                else:
                    return Response(cadence_request_serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)

            # if we couldn't find any valid cadence requests, return that as an error
            if not expanded_requests:
                return Response(
                    {
                        'errors':
                        'No visible requests within cadence window parameters'
                    },
                    status=status.HTTP_400_BAD_REQUEST)

            # now replace the originally sent requests with the cadence requests and send it back
            ret_data = request.data.copy()
            ret_data['requests'] = expanded_requests

            if len(ret_data['requests']) > 1:
                ret_data['operator'] = 'MANY'
            response_serializer = self.get_response_serializer(
                data=ret_data, context={'request': request})
            if not response_serializer.is_valid():
                return Response(response_serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            return Response(ret_data, status=status.HTTP_200_OK)
        else:
            return (Response(request_serializer.errors,
                             status=status.HTTP_400_BAD_REQUEST))
Esempio n. 4
0
    def cadence(self, request):
        expanded_requests = []
        for req in request.data.get('requests', []):
            if isinstance(req, dict) and req.get('cadence'):
                cadence_request_serializer = CadenceRequestSerializer(data=req)
                if cadence_request_serializer.is_valid():
                    expanded_requests.extend(
                        expand_cadence_request(
                            cadence_request_serializer.validated_data,
                            request.user.is_staff))
                else:
                    return Response(cadence_request_serializer.errors,
                                    status=400)
            else:
                expanded_requests.append(req)

        # if we couldn't find any valid cadence requests, return that as an error
        if not expanded_requests:
            return Response(
                {
                    'errors':
                    'No visible requests within cadence window parameters'
                },
                status=400)

        # now replace the originally sent requests with the cadence requests and send it back
        ret_data = request.data.copy()
        ret_data['requests'] = expanded_requests

        if len(ret_data['requests']) > 1:
            ret_data['operator'] = 'MANY'
        request_group_serializer = RequestGroupSerializer(
            data=ret_data, context={'request': request})
        if not request_group_serializer.is_valid():
            return Response(request_group_serializer.errors, status=400)
        return Response(ret_data)