def import_template(template, template_name, description, template_type='bootstrap'):
    """
    Imports a template into the templates/imports directory and saves the metadata into the app config
    :param template: string of the template text
    :param template_name: name of the file to save
    :param description: description to save in the configured templates
    :param template_type: type of the template to save. Enum with options 'bootstrap', 'init-cfg', and 'config-snippet'

    :return: boolean
    """
    try:
        t = Template.query.filter(Template.name == template_name).first()

        if t is None:
            print('Adding new record to db')
            unescaped_template = unescape(template)
            t = Template(name=template_name, description=description, template=unescaped_template, type=template_type)
            db_session.add(t)
            db_session.commit()

        else:
            print('template exists in db')

        return True
    except SQLAlchemyError as sqe:
        print('Could not import file')
        print(str(sqe))
        return False
def delete_template(file_name):
    """
    Deletes an imported template
    :param file_name: name of the file to save
    :return: boolean
    """
    try:
        t = Template.query.filter(Template.name == file_name).first()

        if t is not None:
            db_session.delete(t)
            db_session.commit()

        return True
    except SQLAlchemyError as sqe:
        print('Could not delete template!')
        print(sqe)
        return False
def import_templates():
    """
    Ensures all default and imported templates exist in the template table
    :return: None
    """
    loaded_config = load_config()

    config = load_config()
    print('config is %s' % config)
    default_bootstrap_name = config.get('default_template', 'Default')

    default = Template.query.filter(Template.name == default_bootstrap_name).first()
    if default is None:
        print('Importing default bootstrap.xml files')
        default_file_path = os.path.abspath(os.path.join(app.root_path, '..', 'templates/panos/bootstrap.xml'))
        try:
            with open(default_file_path, 'r') as dfpf:
                t = Template(name=default_bootstrap_name,
                             description='Default Bootstrap template',
                             template=dfpf.read(),
                             type='bootstrap')

            db_session.add(t)
            db_session.commit()
        except OSError:
            print('Could not open file for importing')

    print('Importing init-cfg-static')
    init_cfg_static = Template.query.filter(Template.name == 'init-cfg-static.txt').first()
    if init_cfg_static is None:
        print('Importing default init-cfg-static')
        ics_file_path = os.path.abspath(os.path.join(app.root_path, '..', 'templates/panos/init-cfg-static.txt'))
        try:
            print(
                'opening file init-cfg-static'
            )
            with open(ics_file_path, 'r') as icsf:
                i = Template(name='init-cfg-static.txt',
                             description='Init-Cfg with static management IP addresses',
                             template=icsf.read(),
                             type='init-cfg')

                print('add to db')
                db_session.add(i)
                print('commit to db')
                db_session.commit()
        except OSError:
            print('Could not open file for importing')

    init_cfg_dhcp = Template.query.filter(Template.name == 'Default Init-Cfg DHCP').first()
    if init_cfg_dhcp is None:
        print('Importing default init-cfg-dhcp')
        icd_file_path = os.path.abspath(os.path.join(app.root_path, '..', 'templates/panos/init-cfg-dhcp.txt'))
        try:
            with open(icd_file_path, 'r') as icdf:
                i = Template(name='Default Init-Cfg DHCP',
                             description='Init-Cfg with DHCP Assigned IP addresses',
                             template=icdf.read(),
                             type='init-cfg')

                db_session.add(i)
                db_session.commit()
        except OSError:
            print('Could not open file for importing')

    rel_import_directory = loaded_config.get('template_import_directory', 'templates/import/bootstrap')
    import_directory = os.path.abspath(os.path.join(app.root_path, '..', rel_import_directory))
    all_imported_files = os.listdir(import_directory)

    print('Importing bootstrap templates')
    for it in all_imported_files:
        t = Template.query.filter(Template.name == it).first()
        if t is None:
            print('this template does not exist %s' % it)
            try:
                with open(os.path.join(import_directory, it), 'r') as tf:
                    t = Template(name=it,
                                 description="Imported Template",
                                 template=tf.read(),
                                 type='bootstrap')
                    db_session.add(t)
                    db_session.commit()
            except OSError:
                print('Could not import bootstrap template!')