EXAMPLE_SCRIPT.py 1.81 KB
Newer Older
Dax Mickelson's avatar
Dax Mickelson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#! /usr/bin/env python3

import csv
from freepbx_bulk_handler import Extension

# GLOBALS
INPUT_CSV_FILE = 'extensions_to_create.csv'
OUTPUT_CSV_FILE = 'extensions_to_import.csv'
EMAIL_DOMAIN = 'example.com'
VOICEMAIL_PASSWORD = '12345'


def write_csv_file(extensions) -> None:
    with open(OUTPUT_CSV_FILE, 'w', newline='') as f:
15
        writer = csv.DictWriter(f, fieldnames=Extension.HEADER)
Dax Mickelson's avatar
Dax Mickelson committed
16 17 18 19 20
        writer.writeheader()
        writer.writerows(extension.to_dict for extension in extensions)


def main() -> None:
21 22 23
    # Create a "dumping place" where all the built Extension objects will be stored.
    extensions = []

Dax Mickelson's avatar
Dax Mickelson committed
24
    # Load input csv file
25
    fieldnames = ('Extension', 'First Name', 'Last Name')
Dax Mickelson's avatar
Dax Mickelson committed
26
    with open(INPUT_CSV_FILE, 'r') as f:
27 28
        raw_csv_input = csv.DictReader(f, quotechar='"', fieldnames=fieldnames)
        # Use the collected information to build each extension
Dax Mickelson's avatar
Dax Mickelson committed
29
        for row in raw_csv_input:
30 31 32 33 34 35 36 37 38 39 40 41
            # Skip the header row and blank rows, or rather, rows with no data in the first field.
            if row['Extension'] and row['Extension'] != 'Extension':
                extension = row['Extension']
                name = f"{row['First Name'].strip().capitalize()} {row['Last Name'].strip().capitalize()}"
                email = f"{row['First Name'].strip().lower()}.{row['Last Name'].strip().lower()}@{EMAIL_DOMAIN}"

                # Now that the pertinent information has been formatted, build the extension class
                data_vars = {'name': name,
                             'extension': extension,
                             'voicemail_vmpwd': VOICEMAIL_PASSWORD,
                             'voicemail_email': email}
                extensions.append(Extension(**data_vars))
Dax Mickelson's avatar
Dax Mickelson committed
42 43 44 45 46 47 48

    # Write finished extensions to a csv file.
    write_csv_file(extensions)


if __name__ == '__main__':
    main()