예제 #1
0
def validate_unit(unit_name):
    # FIXME: epfl-ldap should a LDAP Exception
    try:
        if unit_name:
            get_unit_id(unit_name)
    except Exception:
        raise ValidationError(
            "The unit name {} doesn't exist".format(unit_name))
예제 #2
0
def export(site,
           wp_site_url,
           unit_name_or_id,
           to_wordpress=False,
           clean_wordpress=False,
           to_dictionary=False,
           admin_password=None,
           output_dir=None,
           theme=None,
           installs_locked=False,
           updates_automatic=False,
           openshift_env=None,
           use_cache=None,
           keep_extracted_files=False,
           features_flags=False,
           category=None,
           **kwargs):
    """
    Export the jahia content into a WordPress site.

    :param site: the name of the WordPress site
    :param wp_site_url: URL of WordPress site
    :param unit_name_or_id: unit name or unit ID of the WordPress site
    :param to_wordpress: to migrate data
    :param clean_wordpress: to clean data
    :param admin_password: an admin password
    :param output_dir: directory where the jahia zip file will be unzipped
    :param theme: WordPress theme used for the WordPress site
    :param installs_locked: boolean
    :param updates_automatic: boolean
    :param openshift_env: openshift_env environment (prod, int, gcharmier ...)
    :param keep_extracted_files: command to keep files extracted from jahia zip
    :param features_flags: Tell to clean page content or not
    :param category: Site category which defines plugin list to install and configure
    """

    # Download, Unzip the jahia zip and parse the xml data
    site = parse(site=site, use_cache=use_cache, output_dir=output_dir)

    # Define the default language
    default_language = _get_default_language(site.languages)

    # For polylang plugin, we need position default lang in first position
    languages = _set_default_language_in_first_position(
        default_language, site.languages)

    if not site.acronym[default_language]:
        logging.warning("No wp site title in %s", default_language)
        wp_site_title = None
    else:
        wp_site_title = site.acronym[default_language]

    # theme
    if not site.theme[default_language] or site.theme[
            default_language] == "epfl":
        theme_faculty = ""
    else:
        theme_faculty = site.theme[default_language]

    if not theme:
        # Setting correct theme depending on parsing result
        theme = BANNER_THEME_NAME if default_language in site.banner else DEFAULT_THEME_NAME

    # If nothing specified, we use default
    if category is None:
        category = DEFAULT_WP_SITE_CATEGORY

    # tagline
    if not site.title[default_language]:
        logging.warning("No wp tagline in %s", default_language)
        wp_tagline = None
    else:
        wp_tagline = site.title

    # If we get unit ID
    if unit_name_or_id.isdigit():
        unit_id = unit_name_or_id

        # fetch unit name from ldap
        try:
            logging.info("Fetching LDAP for unit '%s' name...", unit_id)
            unit_name = get_unit_name(unit_id)
            logging.info("LDAP name found = %s...", unit_name)
        except LDAPSocketOpenError:
            logging.error("LDAP is not responding, aborting here...")
            raise

    else:  # We get unit name

        unit_name = unit_name_or_id
        # fetch unit id from ldap
        try:
            logging.info("Fetching LDAP for unit '%s' ID...", unit_name)
            unit_id = get_unit_id(unit_name)
            logging.info("LDAP ID found = %s...", unit_id)
        except LDAPSocketOpenError:
            logging.error("LDAP is not responding, aborting here...")
            raise

    info = {
        # information from parser
        'langs': ",".join(languages),
        'wp_site_title': wp_site_title,
        'wp_tagline': wp_tagline,
        'theme_faculty': theme_faculty,
        'unit_name': unit_name,

        # information from source of truth
        'openshift_env': openshift_env,
        'wp_site_url': wp_site_url,
        'theme': theme,
        'updates_automatic': updates_automatic,
        'installs_locked': installs_locked,
        'category': category,

        # determined information
        'unit_id': unit_id,
        'from_export': True
    }

    # skip options, used only during development
    #
    # skip_base: if True don't install WordPress, use the existing site
    # skip_media: if True don't import the media
    # skip_pages: if True don't import the pages
    skip_base = False
    skip_media = False
    skip_pages = False

    # List of plugins to let in 'deactivated' state during import. To earn more time, they are not activated during
    # WordPress empty site generation. Because activating them takes time and we have to take the same amount of time
    # to deactivate them before running Jahia site import.
    # Deactivating plugins can improve import time by ~80%
    # WARNING: be careful with the list order. Plugins will be reactivated after import by using list order. So if
    # there are dependencies between plugins, arrange them in the right way.
    deactivated_plugins = [
        'mainwp-child',
        'epfl-faq',
        'epfl-grid',
        'epfl-infoscience',
        'epfl-infoscience-search',
        'epfl-map',
        'epfl-memento',
        'epfl-news',
        'epfl-people',
        'epfl-scheduler',
        'EPFL-Content-Filter',
        'EPFL-Share',
        'epfl-snippet',
        'epfl-toggle',
        'epfl-tableau',
        'epfl-twitter',
        'epfl-xml',
        'epfl-video',
        'epfl-404',
        'epfl-stats',
        'epfl-google-forms',
        'feedzy-rss-feeds',
        'cache-control',
        'remote-content-shortcode',
        'shortcode-ui',
        'shortcode-ui-richtext',  # This one needs to come after the previous one
        'shortcodes-ultimate',
        'simple-sitemap',
        'svg-support',
        'enlighter',
        'pdfjs-viewer-shortcode',
        'tinymce-advanced',
        'varnish-http-purge'
    ]

    # Generate a WordPress site
    wp_generator = WPGenerator(info, admin_password)

    # base installation
    if skip_base:

        logging.info("Deactivating %s plugins...", len(deactivated_plugins))
        for plugin_name in deactivated_plugins:
            # We do a 'try' to handle missing plugins (if exists)
            try:
                wp_generator.run_wp_cli(
                    "plugin deactivate {}".format(plugin_name))
            except:
                logging.info("Plugin %s doesn't seem's to be installed",
                             plugin_name)

        try:

            # even if we skip the base installation we need to reactivate
            # the basic auth plugin for the rest API
            wp_generator.run_wp_cli("plugin activate Basic-Auth")
        except:
            # if activation fails it means the plugin is not installed
            wp_generator.install_basic_auth_plugin()
    else:
        # If returns false, it means there was an error
        if not wp_generator.generate(deactivated_plugins):
            # We just display line to add to CSV
            _generate_csv_line(wp_generator)
            return

        wp_generator.install_basic_auth_plugin()

    # dual auth
    if settings.ACTIVE_DUAL_AUTH:
        wp_generator.active_dual_auth()

    # exporter
    wp_exporter = WPExporter(site,
                             wp_generator,
                             default_language,
                             output_dir=output_dir)

    # clean
    if clean_wordpress:
        logging.info("Cleaning WordPress for %s...", site.name)
        wp_exporter.delete_all_content()
        logging.info("Data of WordPress site %s successfully deleted",
                     site.name)

    # to WordPress
    if to_wordpress:
        logging.info("Exporting %s to WordPress...", site.name)
        try:

            if wp_generator.get_number_of_pages() == 0:

                wp_exporter.import_data_to_wordpress(
                    skip_pages=skip_pages,
                    skip_media=skip_media,
                    features_flags=features_flags)
                wp_exporter.write_redirections()
                _fix_menu_location(wp_generator, languages, default_language)

                logging.info("Reactivating %s plugins...",
                             len(deactivated_plugins))

                # Reactivating plugins
                for plugin_name in deactivated_plugins:
                    # We do a 'try' to handle missing plugins (if exists)
                    try:
                        wp_generator.run_wp_cli(
                            "plugin activate {}".format(plugin_name))
                    except:
                        logging.info(
                            "Plugin %s doesn't seem's to be installed",
                            plugin_name)

                logging.info("Site %s successfully exported to WordPress",
                             site.name)
            else:
                logging.info("Site %s already exported to WordPress",
                             site.name)
        except (Exception, subprocess.CalledProcessError) as e:
            logging.error(str(e))
            Tracer.write_row(site=site.name, step=e, status="KO")
            if not settings.DEBUG:
                wp_generator.clean()
            raise e

        Tracer.write_row(site=site.name, step="export", status="OK")

    wp_generator.uninstall_basic_auth_plugin()
    wp_generator.enable_updates_automatic_if_allowed()

    # to dictionary
    if to_dictionary:
        data = DictExporter.generate_data(site)
        pprint(data, width=settings.LINE_LENGTH_ON_PPRINT)

    _generate_csv_line(wp_generator)

    if not keep_extracted_files:
        # Delete extracted zip files
        # We take dirname because site.base_path is the path to the subfolder in the zip.
        # Example : path_to_extract/dcsl/dcsl
        # And we want to delete path_to_extract/dcsl
        base_zip_path = os.path.dirname(os.path.abspath(site.base_path))
        logging.debug("Removing zip extracted folder '%s'", base_zip_path)
        if os.path.exists(base_zip_path):
            shutil.rmtree(base_zip_path)

    return wp_exporter
예제 #3
0
 def get_the_unit_id(self, unit_name):
     """
     Get unit id via LDAP Search
     """
     if unit_name is not None:
         return get_unit_id(unit_name)
예제 #4
0
def test_get_unit_id():
    # Success
    unit_id = get_unit_id("IDEVELOP")
    assert unit_id == "13030"

    unit_id = get_unit_id("idevelop")
    assert unit_id == "13030"

    unit_id = get_unit_id("IDEVING")
    assert unit_id == "13031"

    unit_id = get_unit_id("IDEVAM")
    assert unit_id == "13029"

    unit_id = get_unit_id("DCSL")
    assert unit_id == "12634"

    unit_id = get_unit_id("SCITAS")
    assert unit_id == "12922"

    unit_id = get_unit_id("LPBS")
    assert unit_id == "13565"

    unit_id = get_unit_id("SPRING")
    assert unit_id == "13548"

    unit_id = get_unit_id("TUNES")
    assert unit_id == "12493"

    unit_id = get_unit_id("FLEXLAB")
    assert unit_id == "13471"

    unit_id = get_unit_id("LCA1")
    assert unit_id == "10426"

    unit_id = get_unit_id("VPSI")
    assert unit_id == "12637"

    # Fail
    with pytest.raises(
            EpflLdapException,
            match=r"The unit named 'idevelop_pas_souvent' was not found"):
        get_unit_id(unit_name="idevelop_pas_souvent")
예제 #5
0
def export(site,
           wp_site_url,
           unit_name,
           to_wordpress=False,
           clean_wordpress=False,
           admin_password=None,
           output_dir=None,
           theme=None,
           installs_locked=False,
           updates_automatic=False,
           openshift_env=None,
           **kwargs):
    """
    Export the jahia content into a WordPress site.

    :param site: the name of the WordPress site
    :param wp_site_url: URL of WordPress site
    :param unit_name: unit name of the WordPress site
    :param to_wordpress: to migrate data
    :param clean_wordpress: to clean data
    :param admin_password: an admin password
    :param output_dir: directory where the jahia zip file will be unzipped
    :param theme: WordPress theme used for the WordPress site
    :param installs_locked: boolean
    :param updates_automatic: boolean
    :param openshift_env: openshift_env environment (prod, int, gcharmier ...)
    """

    # Download, Unzip the jahia zip and parse the xml data
    site = parse(site=site)

    # Define the default language
    default_language = _get_default_language(site.languages)

    # For polylang plugin, we need position default lang in first position
    languages = _set_default_language_in_first_position(
        default_language, site.languages)

    info = {
        # information from parser
        'langs': ",".join(languages),
        'wp_site_title': site.acronym[default_language],
        'wp_tagline': site.title[default_language],
        'theme_faculty': site.theme[default_language],
        'unit_name': unit_name,

        # information from source of truth
        'openshift_env': openshift_env,
        'wp_site_url': wp_site_url,
        'theme': theme,
        'updates_automatic': updates_automatic,
        'installs_locked': installs_locked,

        # determined information
        'unit_id': get_unit_id(unit_name),
    }

    # Generate a WordPress site
    wp_generator = WPGenerator(info, admin_password)
    wp_generator.generate()

    wp_generator.install_basic_auth_plugin()

    if settings.ACTIVE_DUAL_AUTH:
        wp_generator.active_dual_auth()

    wp_exporter = WPExporter(site, wp_generator, output_dir)

    if to_wordpress:
        logging.info("Exporting %s to WordPress...", site.name)
        wp_exporter.import_all_data_to_wordpress()
        _fix_menu_location(wp_generator, languages, default_language)
        logging.info("Site %s successfully exported to WordPress", site.name)

    if clean_wordpress:
        logging.info("Cleaning WordPress for %s...", site.name)
        wp_exporter.delete_all_content()
        logging.info("Data of WordPress site %s successfully deleted",
                     site.name)

    wp_generator.uninstall_basic_auth_plugin()

    return wp_exporter