def import_csv(file_path):
    database_path = injector.get_database_handler().database_path
    object_type, columns, data = read_file(file_path)
    sub_type = None
    if object_type == 'User' or object_type == 'Subreddit':
        model = RedditObject
        sub_type = object_type
    else:
        model = model_map[object_type]
    table = model.__tablename__
    con = sqlite3.connect(database_path)
    cursor = con.cursor()
    cursor.execute('BEGIN TRANSACTION;')
    sql = f'INSERT INTO {table} ({",".join(columns)}) VALUES({",".join(["?"] * len(columns))});'
    for row in data:
        for x in row:
            if x == '':
                row[row.index(x)] = None
        cursor.execute(sql, row)
    cursor.execute('COMMIT;')

    index = columns.index('id')
    if sub_type == 'User':
        handle_user_import([row[index] for row in data], cursor)
    elif sub_type == 'Subreddit':
        handle_subreddit_import([row[index] for row in data], cursor)
    logger.info('Imported data from csv file',
                extra={
                    'file_path': file_path,
                    'import_model_type': object_type,
                    'import_count': len(data)
                })
Exemple #2
0
    def __init__(self):
        super().__init__()
        self.scheduler = injector.get_scheduler()
        self.db = injector.get_database_handler()
        self.task_map = {}
        self.new_tasks = []
        self.deleted_tasks = []
        self.load_ui()
        self.schedule_download_button.clicked.connect(self.add_task)

        self.scheduled_downloads_list_widget.setContextMenuPolicy(
            Qt.CustomContextMenu)
        self.scheduled_downloads_list_widget.customContextMenuRequested.connect(
            self.task_list_widget_context_menu)
    def __init__(self, **kwargs):
        super().__init__()
        self.db = injector.get_database_handler()
        self.session = self.db.get_session()
        self.main_window = kwargs.pop('main_window', None)
        self.kwargs = kwargs

        self.list_map = {}

        self.master_user_list = None
        self.master_subreddit_list = None
        self.make_master_lists()

        self.add_list(self.master_user_list, True)
        self.add_list(self.master_subreddit_list, True)
        for ro_list in self.session.query(RedditObjectList).order_by(RedditObjectList.name):
            self.add_list(ro_list)

        self.list_widget.currentItemChanged.connect(
            lambda x: self.list_settings_widget.set_objects([self.list_map[x.text()]]))
        self.cascade_changes_checkbox.setChecked(self.settings.cascade_list_changes)
Exemple #4
0
def export_csv(object_list, model, file_path):
    database_path = injector.get_database_handler().database_path
    table = model.__tablename__
    if model != RedditObject:
        model_type = model.__name__
    else:
        model_type = object_list[0].object_type.title()
    id_list = get_id_list(object_list)
    con = sqlite3.connect(database_path)
    with open(file_path, 'w', newline='', encoding='utf-8') as file:
        file.write(model_type + '\n')
        writer = csv.writer(file)
        cursor = con.cursor()
        cursor.execute(
            f'SELECT * FROM {table} WHERE {table}.id in ({",".join(["?"] * len(id_list))})',
            id_list)

        writer.writerow(x[0] for x in cursor.description)
        writer.writerows(cursor.fetchall())
    logger.info('Exported items to csv file',
                extra={
                    'export_model_type': model.__name__,
                    'items_exported': len(id_list)
                })
Exemple #5
0
def get_database_url():
    db = injector.get_database_handler()
    return db.database_url