Esempio n. 1
0
def yaml_add_stakeholder_fields(request):

    # Read the global configuration file
    global_stream = open("%s/%s" % (profile_directory_path(request), STAKEHOLDER_YAML), 'r')
    global_config = yaml.load(global_stream)

    # Read the localized configuration file
    try:
        locale_stream = open("%s/%s" % (locale_profile_directory_path(request), STAKEHOLDER_YAML), 'r')
        locale_config = yaml.load(locale_stream)

        # If there is a localized config file then merge it with the global one
        global_config = merge_profiles(global_config, locale_config)

    except IOError:
        # No localized configuration file found!
        pass

    ret = _add_to_db(global_config, SH_Key, SH_Value)

    # Also scan application YAML (geometry etc.)
    app_scan = _handle_application_config(request)
    if app_scan is not None:
        ret['messagestack'] += app_scan

    return ret
Esempio n. 2
0
def _handle_application_config(request):

    def __check_geometry(yaml_geom, profile_name):
        yaml_geom_geojson = geojson.loads(json.dumps(yaml_geom),
                                          object_hook=geojson.GeoJSON.to_instance)
        yaml_geom_shape = shapely.geometry.asShape(yaml_geom_geojson)
        # Query database
        db_profile = Session.query(Profile).\
            filter(Profile.code == profile_name).first()
        if db_profile is None:
            # Global profile does not yet exist, create it
            Session.add(Profile(profile_name, yaml_geom_shape.wkt))
            return "Geometry for profile '%s' created." % profile_name
        else:
            # Compare existing geometry with the one in config file
            geom_db = (shapely.wkb.loads(str(db_profile.geometry.geom_wkb))
                       if db_profile.geometry else None)
            if geom_db and geom_db.equals(yaml_geom_shape) is True:
                return ("Geometry for profile '%s' did not change."
                        % profile_name)
            else:
                # Update geometry from global profile
                db_profile.geometry = yaml_geom_shape.wkt
                return "Geometry for profile '%s' updated." % profile_name

    msg = []

    # First check global application configuration
    app_stream = open("%s/%s" %
                      (profile_directory_path(request), APPLICATION_YAML), 'r')
    app_config = yaml.load(app_stream)

    if ('application' in app_config and
        'geometry' in app_config['application']):

        msg.append(__check_geometry(app_config['application']['geometry'],
                   'global'))

    # Then check local application configuration if available
    # Try to find the profile in parameters
    locale_code = get_current_profile(request)

    # Only continue if a profile was found
    if locale_code is not None:
        try:
            locale_app_stream = open("%s/%s" %
                                     (locale_profile_directory_path(request), APPLICATION_YAML), 'r')
            locale_app_config = yaml.load(locale_app_stream)

            if ('application' in locale_app_config and
                'geometry' in locale_app_config['application']):

                msg.append(__check_geometry(
                           locale_app_config['application']['geometry'], locale_code))

        except IOError:
            # No localized application configuration file found
            pass

    return msg
Esempio n. 3
0
def get_default_profile(request):
    """
    Return the name of the default profile to be used if no profile was
    specified.

    Return the default profile as set in the application's configuration
    YAML (key ``default_profile``). If no default profile is set in the
    configuration, use ``global`` as default profile.

    Args:
        ``request`` (pyramid.request): A :term:`Pyramid` Request object.

    Returns:
        ``str``. The default profile as set in the configuration or
        ``global``.
    """
    default_profile = 'global'
    try:
        app_stream = open(
            "%s/%s" % (profile_directory_path(request), APPLICATION_YAML), 'r')
    except IOError:
        return default_profile

    config = yaml.load(app_stream)

    return config.get(
        'application', {}).get('default_profile', default_profile)
Esempio n. 4
0
def yaml_translate_activities(request):
    """

    """

    # Read the global configuration file
    global_stream = open(
        "%s/%s" % (profile_directory_path(request), ACTIVITY_YAML), 'r')
    global_config = yaml.load(global_stream)

    # Read the localized configuration file
    try:
        locale_stream = open(
            "%s/%s" % (locale_profile_directory_path(request), ACTIVITY_YAML),
            'r')
        locale_config = yaml.load(locale_stream)

        # If there is a localized config file then merge it with the global one
        global_config = merge_profiles(global_config, locale_config)

    except IOError:
        # No localized configuration file found!
        pass

    return get_translated_keys(request, global_config, A_Key, A_Value)
Esempio n. 5
0
def yaml_translate_stakeholders(request):

    # Read the global configuration file
    global_stream = open("%s/%s" % (profile_directory_path(request), STAKEHOLDER_YAML), 'r')
    global_config = yaml.load(global_stream)

    # Read the localized configuration file
    try:
        locale_stream = open("%s/%s.yml" % (locale_profile_directory_path(request), STAKEHOLDER_YAML), 'r')
        locale_config = yaml.load(locale_stream)

        # If there is a localized config file then merge it with the global one
        global_config = merge_profiles(global_config, locale_config)

    except IOError:
        # No localized configuration file found!
        pass

    return get_translated_keys(request, global_config, SH_Key, SH_Value)
Esempio n. 6
0
def _processProfile(request, dbProfile, isGlobal=False):

    yaml_file = APPLICATION_YAML
    if isGlobal is False:
        yaml_file = "%s/%s" % (dbProfile.code, APPLICATION_YAML)

    if isGlobal is False:
        pass

    # Try to find and open profile config file
    try:
        profile_stream = open("%s/%s" % (profile_directory_path(request),
                              yaml_file), 'r')
        profile_config = yaml.load(profile_stream)

        if 'application' not in profile_config:
            # Not a valid config file
            return None

        app_config = profile_config['application']

        # Collect values
        code = dbProfile.code
        name = app_config['name'] if 'name' in app_config else 'Unknown'
        active = get_current_profile(request) == dbProfile.code
        geometry = None
        if dbProfile.geometry is not None:
            geometry = geojson.loads(geojson.dumps(shapely.wkb.loads(
                                     str(dbProfile.geometry.geom_wkb)
                                     )))

        return {
            'name': name,
            'profile': code,
            'geometry': geometry,
            'active': active
        }

    except IOError:
        # File not found
        return None
Esempio n. 7
0
def get_config(request):
    """
    Return the configuration file in lmkp/config.yaml as JSON. Using parameter
    format=ext an ExtJS form fields configuration object is returned based on
    the configuration in config.yaml.
    """

    # Get the requested output format
    parameter = request.matchdict['parameter']

    config_yaml = None
    if parameter.lower() == 'activities':
        config_yaml = ACTIVITY_YAML
        Key = A_Key
        Value = A_Value
    elif parameter.lower() == 'stakeholders':
        config_yaml = STAKEHOLDER_YAML
        Key = SH_Key
        Value = SH_Value
    else:
        raise HTTPNotFound()


    # Read the global configuration file
    global_stream = open("%s/%s" % (profile_directory_path(request), config_yaml), 'r')
    global_config = yaml.load(global_stream)

    # Read the localized configuration file
    try:
        locale_stream = open("%s/%s" % (locale_profile_directory_path(request), config_yaml), 'r')
        locale_config = yaml.load(locale_stream)

        # If there is a localized config file then merge it with the global one
        global_config = merge_profiles(global_config, locale_config)

    except IOError:
        # No localized configuration file found!
        pass

    # Get the configuration file from the YAML file
    extObject = []
    # Do the translation work from custom configuration format to an
    # ExtJS configuration object.
    fields = global_config['fields']

    # language is needed because fields are to be displayed translated
    localizer = get_localizer(request)
    lang = Session.query(Language).filter(Language.locale == localizer.locale_name).first()
    if lang is None:
        lang = Language(1, 'English', 'English', 'en')

    # First process the mandatory fields
    for (name, config) in fields['mandatory'].iteritems():
        o = _get_field_config(Key, Value, name, config, lang, True)
        if o is not None:
            extObject.append(o)
    # Then process also the optional fields
    if 'optional' in fields:
        for (name, config) in fields['optional'].iteritems():
            # Don't do a database query for indicators that values are from local
            # YAML (added during merge_yaml above)
            if name == 'local' or name == 'values':
                pass
            else:
                o = _get_field_config(Key, Value, name, config, lang)
            if o is not None:
                extObject.append(o)

    # Return it sorted
    return sorted(extObject, key=lambda value: value['name'])