def post(self, request, *args, **kwargs): data = request.data serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) try: connection = DBProviderConnection.objects.get( id=data.get('connection')) provider = connection.provider custom_column = CustomColumnType.objects.get( id=data.get('custom_column')) if provider.name == MONGO: mongo_db = MongoDBManager() db, cache_db = mongo_db.connection_mongo_by_provider( provider_connection=connection) # type in [str, int, float, datetime] table = data.get('table_name') column = data.get('real_column') data_type = custom_column.slug _ = mongo_db.update_convert_column_data_type( db=db, table=table, column=column, data_type=data_type, provider_connection_id=connection.id) return responses.ok(data=serializer.data, method=constant.POST, entity_name='custom_column_mapping') else: return responses.ok(data=None, method=constant.POST, entity_name='custom_column_mapping') except Exception as err: return responses.bad_request(data=str(err), message_code='MAPPING_ERROR')
def update(self, request, *args, **kwargs): pk = kwargs.get('pk', None) try: data = request.data partial = kwargs.pop('partial', False) instance = CustomColumnMapping.objects.get(id=pk) serializer = self.serializer_class(instance, data=data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) connection = DBProviderConnection.objects.get( id=data.get('connection')) provider = connection.provider custom_column = CustomColumnType.objects.get( id=data.get('custom_column')) if provider.name == MONGO: mongo_db = MongoDBManager() db, cache_db = mongo_db.connection_mongo_by_provider( provider_connection=connection) # type in [str, int, float, datetime] table = data.get('table_name') column = data.get('real_column') data_type = custom_column.slug _ = mongo_db.update_convert_column_data_type( db=db, table=table, column=column, data_type=data_type, provider_connection_id=connection.id) return responses.ok(data=serializer.data, method=constant.PUT, entity_name='custom_column_mapping') else: return responses.ok(data=None, method=constant.PUT, entity_name='custom_column_mapping') except Exception as err: responses.bad_request( data=str(err), message_code='CUSTOM_COLUMN_MAPPING_NOT_FOUND')
def put(self, request, *args, **kwargs): data = request.data convert_field = data.get("convert_field") data_type = data.get("data_type") table = data.get("table") provider_connection_id = data.get("provider_connection_id") mongo_db = MongoDBManager() provider_connection = DBProviderConnection.objects.filter( id=provider_connection_id).first() db, cache_db = mongo_db.connection_mongo_by_provider( provider_connection=provider_connection) # type in [str, int, float, datetime] is_convert = mongo_db.update_convert_column_data_type( db=db, table=table, column=convert_field, data_type=data_type, provider_connection_id=provider_connection_id) return responses.ok(data={"is_convert": is_convert}, method="put", entity_name="test")