示例#1
0
 def test_is_valid__invalid_issue(self):
     self.data['issue'] = [
         u"Test Issue\u2022Sub issue", "Issue 2", u"Test\u2022Test\u2022"
     ]
     serializer = SearchInputSerializer(data=self.data)
     self.assertFalse(serializer.is_valid())
     self.assertEqual(serializer.errors.get('issue'), [
         u'Issue is malformed, it needs to be "issue" or "issue\u2022subissue"'
     ])
示例#2
0
 def test_is_valid__invalid_product(self):
     self.data['product'] = [
         u"Mortgage\u2022FHA Mortgage", "Payday Loan",
         u"Test\u2022Test\u2022"
     ]
     serializer = SearchInputSerializer(data=self.data)
     self.assertFalse(serializer.is_valid())
     self.assertEqual(serializer.errors.get('product'), [
         u'Product is malformed, it needs to be "product" or "product\u2022subproduct"'
     ])
示例#3
0
    def test_is_valid__no_args(self):
        serializer = SearchInputSerializer(data={})
        self.assertTrue(serializer.is_valid())

        exp_dict = copy.deepcopy(PARAMS)

        # This is an OrderedDict
        for k, v in serializer.validated_data.iteritems():

            self.assertIn(k, exp_dict)
            self.assertEqual(v, exp_dict[k])
示例#4
0
def states(request):
    data = _parse_query_params(request.query_params)
    serializer = SearchInputSerializer(data=data)

    if not serializer.is_valid():
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    results = es_interface.states_agg(agg_exclude=AGG_EXCLUDE_FIELDS,
                                      **serializer.validated_data)
    headers = _build_headers()

    return Response(results, headers=headers)
示例#5
0
 def test_is_valid__invalid_issue(self):
     self.data["issue"] = [
         "Test Issue\u2022Sub issue",
         "Issue 2",
         "Test\u2022Test\u2022",
     ]
     serializer = SearchInputSerializer(data=self.data)
     self.assertFalse(serializer.is_valid())
     self.assertEqual(
         serializer.errors.get("issue"),
         [
             'Issue is malformed, it needs to be "issue" or '
             '"issue\u2022subissue"'
         ],
     )
示例#6
0
def search(request):

    fixed_qparam = request.query_params
    data = _parse_query_params(request.query_params)

    # Add format to data
    format = request.accepted_renderer.format
    if format and format in EXPORT_FORMATS:
        data['format'] = format
    else:
        data['format'] = 'default'

    serializer = SearchInputSerializer(data=data)

    if not serializer.is_valid():
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    results = es_interface.search(agg_exclude=AGG_EXCLUDE_FIELDS,
                                  **serializer.validated_data)
    headers = _buildHeaders()

    if format not in EXPORT_FORMATS:
        return Response(results, headers=headers)

    # If format is in export formats, update its attachment response
    # with a filename

    response = StreamingHttpResponse(
        streaming_content=results,
        content_type=FORMAT_CONTENT_TYPE_MAP[format])
    filename = 'complaints-{}.{}'.format(
        datetime.now().strftime('%Y-%m-%d_%H_%M'), format)
    headerTemplate = 'attachment; filename="{}"'
    response['Content-Disposition'] = headerTemplate.format(filename)
    for header in headers:
        response[header] = headers[header]

    return response
示例#7
0
 def test_is_valid__valid_issue(self):
     self.data['issue'] = [u"Test Issue\u2022Sub issue", "Issue 2"]
     serializer = SearchInputSerializer(data=self.data)
     self.assertTrue(serializer.is_valid())
示例#8
0
 def test_is_valid__valid_product(self):
     self.data['product'] = [u"Mortgage\u2022FHA Mortgage", "Payday Loan"]
     serializer = SearchInputSerializer(data=self.data)
     self.assertTrue(serializer.is_valid())