Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        data = request.data
        connection_id = kwargs.get("connection_id")
        list_field = data.get("list_field")
        table_name = data.get("table_name")
        connection = DBProviderConnection.objects.filter(
            id=connection_id).first()
        if connection is None:
            return responses.bad_request(
                data=None, message_code="PROVIDER_CONNECTION_NOT_FOUND")

        if connection.provider.name == MONGO:
            mongo_db_manager = MongoDBManager()
            db, cache_db = mongo_db_manager.connection_mongo_by_provider(
                provider_connection=connection)
            collections = mongo_db_manager.get_all_collections(
                db=db, cache_db=cache_db)
            column_mapping = CustomColumnMapping.objects.filter(
                table_name=table_name, connection_id=connection.id).exists()
            if column_mapping is False and table_name not in collections:
                mongo_db_manager.create_new_collection(db, table_name)
            else:
                return responses.bad_request(
                    data=None, message_code="TABLE_NAME_IS_EXISTS")

            for field in list_field:
                try:
                    custom_column = CustomColumnType.objects.get(
                        id=field.get('custom_column'))
                    CustomColumnMapping.objects.create(
                        connection_id=connection.id,
                        table_name=table_name,
                        real_column=field.get("column_name"),
                        custom_column_name=field.get("column_name"),
                        custom_column_id=custom_column.id)
                except Exception as ex:
                    print(ex)
                    continue

        resp = CustomColumnMapping.objects.filter(connection_id=connection.id,
                                                  table_name=table_name)
        serializer = self.get_serializer(resp, many=True)
        return responses.ok(data=serializer.data,
                            method=constant.POST,
                            entity_name="custom_column_mapping")
Ejemplo n.º 2
0
 def post(self, request, *args, **kwargs):
     try:
         sql_function_id = kwargs.get('sql_function_id')
         table_name = kwargs.get('table_name')
         sql_function = self.get_queryset().get(id=sql_function_id)
         connection = sql_function.connection
         provider = connection.provider
         if provider.name == MONGO:
             mongo_db_manager = MongoDBManager()
             db, cache_db = mongo_db_manager.connection_mongo_by_provider(
                 provider_connection=connection)
             collection = mongo_db_manager.create_new_collection(
                 db=db, collection_name=table_name)
             mongo_db_manager.create_table_with_sql_function(
                 db, collection, sql_function)
         else:
             return responses.ok(data=None,
                                 method=constant.POST,
                                 entity_name=sql_function)
     except Exception as err:
         return responses.bad_request(data=str(err),
                                      message_code='SQL_FUNCTION_ERROR')
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        try:
            user = request.user
            file_obj = request.FILES['file']
            # url_csv = upload_to_s3("import", file_obj.name, file_obj, user_id=user.id)

            file = file_obj.read().decode('utf-8')
            csv_data = csv.DictReader(StringIO(file))

            connection_id = kwargs.get("connection")
            table_name = kwargs.get("table_name")
            connection = DBProviderConnection.objects.filter(
                id=connection_id).first()
            if connection.provider.name == MONGO:
                mongo_db_manager = MongoDBManager()
                db, cache_db = mongo_db_manager.connection_mongo_by_provider(
                    provider_connection=connection)
                collections = mongo_db_manager.get_all_collections(
                    db=db, cache_db=cache_db)
                if table_name not in collections:
                    mongo_db_manager.create_new_collection(db, table_name)
                else:
                    headers = list(csv_data.fieldnames)
                    try:
                        columns = mongo_db_manager.get_all_keys(
                            db=db, collection=table_name)
                        if columns:
                            for header in headers:
                                if header not in columns:
                                    return responses.bad_request(
                                        data=
                                        f"Column '{header}' is not exists in table {table_name}",
                                        message_code="Column is not exists")

                    except Exception as err:
                        return responses.bad_request(data=str(err),
                                                     message_code=str(err))

                # list_insert = []
                # for row in csv_data:
                #     data = dict(row)
                #     # data["_id"] = str(ObjectId())
                #     list_insert.append(data)
                #
                # print(list_insert)

                file_full_name = file_obj.name.split(".")
                time_stamp = datetime.datetime.now().timestamp()
                file_name = f"{file_full_name[0]}_{str(int(time_stamp))}.{file_full_name[1]}"
                file_name = file_name.replace(" ", "_")
                fs = FileSystemStorage(
                    location=f"{settings.MEDIA_ROOT}/import",
                    base_url=f"{settings.MEDIA_ROOT}/import")
                filename = fs.save(file_name, file_obj)
                uploaded_file_url = fs.url(filename)

                static_dir = f"{settings.MEDIA_ROOT}/import/{filename}"

                import_record = ImportData.objects.create(
                    provider_connection_id=connection.id,
                    username=user.username,
                    table=table_name,
                    file_url=static_dir)
                # process_import_database.delay(import_id=import_record.id)
                return responses.ok(data="waiting import data",
                                    method='post',
                                    entity_name='import_database')
            return responses.bad_request(data=None,
                                         message_code="SQL_PROVIDER_NOT_FOUND")

        except Exception as err:
            return responses.not_found(data=None,
                                       message_code='SQL_FUNCTION_NOT_FOUND',
                                       message_system=err)