def post(self, request, import_queue=None): file = None file_name = None is_upload = 'file' in request.data is_file_url = 'file_url' in request.data is_data = 'data' in request.data file_content = None try: if is_upload: file = request.data['file'] file_name = file.name file_content = file.read().decode('utf-8') elif is_file_url: file_name = request.data['file_url'] file = requests.get(file_name) file_content = file.text except: # pylint: disable=bare-except pass if not file_content and not is_data: return Response(dict(exception=NO_CONTENT_TO_IMPORT), status=status.HTTP_400_BAD_REQUEST) if file_name and is_csv_file(name=file_name): data = OclStandardCsvToJsonConverter( input_list=csv_file_data_to_input_list(file_content), allow_special_characters=True).process() elif file: data = file_content else: data = request.data.get('data') return import_response(self.request, import_queue, data, None, True)
def post(self, request, import_queue=None): file = request.data.get('file', None) if not file: return Response(dict(exception=NO_CONTENT_TO_IMPORT), status=status.HTTP_400_BAD_REQUEST) if is_csv_file(name=file.name): data = OclStandardCsvToJsonConverter( input_list=csv_file_data_to_input_list( file.read().decode('utf-8')), allow_special_characters=True).process() else: data = file.read() return import_response(self.request, import_queue, data)
def test_is_csv_file(self): self.assertFalse(is_csv_file(name='foo/bar')) self.assertTrue(is_csv_file(name='foo/bar.csv')) self.assertFalse(is_csv_file(name='foo.zip')) file_mock = Mock(spec=File) file_mock.name = 'unknown_file' self.assertFalse(is_csv_file(file=file_mock)) file_mock.name = 'unknown_file.json' self.assertFalse(is_csv_file(file=file_mock)) file_mock.name = 'unknown_file.csv' self.assertTrue(is_csv_file(file=file_mock))
def post(self, request, import_queue=None): file = None file_url = request.data.get('file_url') try: file = requests.get(file_url) except: # pylint: disable=bare-except pass if not file: return Response(dict(exception=NO_CONTENT_TO_IMPORT), status=status.HTTP_400_BAD_REQUEST) if is_csv_file(name=file_url): data = OclStandardCsvToJsonConverter( input_list=csv_file_data_to_input_list(file.text), allow_special_characters=True).process() else: data = file.text return import_response(self.request, import_queue, data)