Esempio n. 1
0
def import_file(env, filename):
    # Create reference to DB
    db = make_db(env)
    try:
        db.begin()
        tree = ET.parse(filename)
        root = tree.getroot()
        query = "INSERT INTO webpage_files(title, last_edit_time, content) VALUES( %s, %s, %s)"

        with db.cursor() as cursor:
            for elem in root:
                text = ""
                date = ""
                title = ""
                for ele in elem:
                    if 'title' in ele.tag:
                        if 'Image:' in ele.text:
                            break
                        title = ele.text
                    if 'revision' in ele.tag:
                        for i in ele:
                            if 'timestamp' in i.tag:
                                date = i.text
                            if 'text' in i.tag:
                                text = i.text
                if title != '' and text != '' and text is not None:
                    if date != "":
                        date = date.replace("T", " ")
                        date = date.replace("Z", "")
                    title = title.replace(" ", "_")
                    text = format_text(text)
                    cursor.execute(query, [title, date, text])
        db.commit()
    finally:
        db.close()
Esempio n. 2
0
def import_file(env, filename):
    # Create reference to DB
    db = make_db(env)
    try:
        db.begin()
        with open(filename) as f:
            reader = csv.DictReader(f)
            for line in reader:
                uid = line['uid']
                group_name = clean_names(line['org_name'])
                pos_name = line['pos_name']
                control = line['control']
                start_date = line['start_date']
                end_date = line['end_date']
                group_id = get_create_group_id(group_name, db)
                pos_id = get_create_position_id(pos_name, group_id, control,
                                                db)
                user_id = get_user_id_from_uid(uid, db)

                query = """
                INSERT INTO position_holders(pos_id, user_id, start_date, end_date) VALUES(%s, %s, %s, %s)
                """
                with db.cursor() as cursor:
                    cursor.execute(query,
                                   [pos_id, user_id, start_date, end_date])
        db.commit()
    finally:
        db.close()
Esempio n. 3
0
def import_zip(env, file):
    db = make_db(env)
    try:
        db.begin()
        with db.cursor() as cursor:
            with ZipFile(file) as images_zip:
                import_images(cursor, images_zip)
        db.commit()
    finally:
        db.close()
Esempio n. 4
0
def insert_permissions(env):
    db = make_db(env)
    try:
        db.begin()
        with db.cursor() as cursor:
            for group, group_permissions in groups_permissions.items():
                for control, permission_ids in group_permissions.items():
                    for position in get_positions(group, control, cursor):
                        for permission_id in permission_ids:
                            if add_permission(position['pos_id'],
                                              permission_id.value, cursor):
                                print(
                                    f'Granted "{permission_id.name}" to group "{group}" position "{position["pos_name"]}"'
                                )
        db.commit()
    finally:
        db.close()
Esempio n. 5
0
def update_ug_groups(env):
    db = make_db(env)
    try:
        db.begin()
        with db.cursor() as cursor:
            ug_group = get_ug_group(cursor)
            ug_type = ug_group['type']
            ug_pos_id = ug_group['pos_id']
            ug_admin_positions = get_ug_admin_positions(cursor)

            remove_ug_group_members(cursor, ug_pos_id)
            remove_ug_year_group_members(cursor)
            add_ug_group_members(cursor, ug_pos_id)
            pos_ids = make_ug_year_groups(cursor, ug_type, ug_admin_positions)
            add_ug_year_members(cursor, pos_ids)
        db.commit()
    finally:
        db.close()
Esempio n. 6
0
def import_file(env, file):
    # Create reference to DB
    db = make_db(env)
    try:
        db.begin()
        with db.cursor() as cursor:
            option_ids = get_option_ids(cursor)
            house_pos_ids = get_house_pos_ids(cursor)

            # Read the TSV row-by-row TSV data
            with open(file, encoding="latin-1", newline="") as tsv_file:
                headers = None
                for row in csv.reader(tsv_file, delimiter="\t"):
                    if headers is None:
                        headers = row
                        continue

                    row_dict = dict(zip(headers, row))
                    add_user_to_db(cursor, row_dict, option_ids, house_pos_ids)
        db.commit()
    finally:
        db.close()
Esempio n. 7
0
        file = image_files[inum_image['image_id']]
        extension = file.split('.')[-1]
        try:
            with open('images/' + file, 'rb') as image_file:
                contents = image_file.read()
        except FileNotFoundError:
            continue

        cursor.execute(query, (member['user_id'], extension, contents))
    os.system('rm -r images')


if __name__ == '__main__':
    # Parse input arguments
    parser = argparse.ArgumentParser(
        description='Imports tables exported from the legacy database')
    parser.add_argument('-e',
                        '--env',
                        default='dev',
                        help='Database to update')
    args = parser.parse_args()

    db = make_db(args.env)
    try:
        db.begin()
        with db.cursor() as cursor:
            import_tables(cursor)
        db.commit()
    finally:
        db.close()