def put(self, request, table_name): try: if table_name not in settings.QUERY_TABLES: return_result = [ { "result": "error", "message": "Invalid code list table", } ] return HttpResponse(json.dumps(return_result), content_type='application/json', status=status.HTTP_400_BAD_REQUEST) data = request.data['data'] params = tuple(data) table_query_builder = TableQueryBuilder(table_name, "repo") delete_query = table_query_builder.delete() insert_query = table_query_builder.insert(params) return_result = update_table_from_tuple([delete_query, insert_query]) return_status_code = status.HTTP_500_INTERNAL_SERVER_ERROR if return_result[ 0]['result'] == 'error' else status.HTTP_200_OK return HttpResponse(json.dumps(return_result), content_type='application/json', status=return_status_code) except Exception as e: handle_uncaught_error(e) raise CustomAPIException({'detail': str(e)})
def streaming_tables_export_view(request, table_name, schema="repo"): data_table = True frozen_table = True try: FrozenData.objects.get(frozen_db_table=table_name) SavedQueryData.objects.get(saved_query_db_table=table_name) except FrozenData.DoesNotExist: frozen_table = False except SavedQueryData.DoesNotExist: data_table = False if table_name not in settings.QUERY_TABLES and not frozen_table and not data_table: return_result = [ { "result": "error", "message": "Invalid table " + table_name, } ] return HttpResponse(json.dumps(return_result), content_type='application/json', status=status.HTTP_204_NO_CONTENT) try: table_query_builder = TableQueryBuilder(table_name, schema) exporter = TableStreamingExporter( table_query_builder.select().get_sql_without_limit()) response = StreamingHttpResponse( exporter.stream(), content_type="text/csv") response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format( table_name) return response except Exception as e: handle_uncaught_error(e) response = {'detail': f'{str(e)}'} return HttpResponse(json.dumps(response), content_type='application/json', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def delete(self, request, pk, format=None): saved_query_data = self.get_object(pk) table_name = saved_query_data.saved_query_db_table saved_query_data.delete() query_builder = TableQueryBuilder(table_name, "dataset") delete_sql = query_builder.delete_table(table_name, "dataset") delete_result = run_query(delete_sql) if delete_result[0]['result'] == 'success': return Response(status=status.HTTP_204_NO_CONTENT) else: return Response(delete_result, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def delete_archive(id): try: frozen_data = FrozenData.objects.get(pk=id) table_name = frozen_data.frozen_db_table frozen_data.delete() query_builder = TableQueryBuilder(table_name, "archives") delete_sql = query_builder.delete_table(table_name, "archives") delete_result = run_query(delete_sql) if delete_result[0]['result'] == 'success': delete_source(table_name) return delete_result except FrozenData.DoesNotExist: return False
def post(self, request, format=None): serializer = SavedQueryDataSerializer(data=request.data) if serializer.is_valid(): saved_query_db_table = "query_data_" + \ datetime.datetime.now().strftime('%Y%m%d%H%M%S') query_builder = TableQueryBuilder( saved_query_db_table, operation=serializer.validated_data["operation"]) sql = query_builder.get_sql_without_limit() serializer.save(user=self.request.user, full_query=sql, saved_query_db_table=saved_query_db_table) create_dataset_archive.delay(serializer.instance.id) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_dataset_archive(id): try: query_data = SavedQueryData.objects.get(id=id) query_builder = TableQueryBuilder(query_data.saved_query_db_table, operation=query_data.operation) create_query = query_builder.create_table_from_query(query_data.saved_query_db_table, "dataset") create_result = run_query(create_query) if create_result[0]['result'] == 'success': query_data.status = 'c' query_data.save() return { "status": "success" } elif create_result[0]['result'] == 'error': query_data.status = 'e' query_data.logs = 'Failed to create dataset archive: ' + create_result[0]['message'] query_data.save() return { "status": "failed", "result": create_result[0]['message'] } else: query_data.status = 'e' query_data.logs = 'Failed to create dataset archive: ' + json.dumps(create_result) query_data.save() return { "status": "failed", "result": json.dumps(create_result) } except SavedQueryData.DoesNotExist: return { "status": "errored", "result": id }
def create_table_archive(id): try: frozen_data = FrozenData.objects.get(id=id) source = Source.objects.get(active_mirror_name=frozen_data.parent_db_table) schema = source.schema query_builder = TableQueryBuilder(frozen_data.parent_db_table, schema) create_query = query_builder.select().create_table_from_query(frozen_data.frozen_db_table, "archives") create_result = run_query(create_query) if create_result[0]['result'] == 'success': frozen_data.status = 'c' frozen_data.save() return { "status": "success" } elif create_result[0]['result'] == 'error': frozen_data.status = 'e' frozen_data.logs = 'Failed to create table archive: ' + create_result[0]['message'] frozen_data.save() return { "status": "failed", "result": create_result[0]['message'] } else: frozen_data.status = 'e' frozen_data.logs = 'Failed to create table archive: ' + json.dumps(create_result) frozen_data.save() return { "status": "failed", "result": json.dumps(create_result) } except FrozenData.DoesNotExist: return { "status": "errored", "result": id }