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"' ])
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"' ])
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])
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)
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"' ], )
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
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())
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())