Beispiel #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
Beispiel #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
Beispiel #3
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)
Beispiel #4
0
def get_stakeholder_sitekey(request):
    # Read the profile stakeholder configuration file
    try:
        stream = open("%s/%s" % (locale_profile_directory_path(request), NEW_STAKEHOLDER_YAML), 'r')
    except IOError:
        return None

    config = yaml.load(stream)

    if 'site_key' in config:
        return config['site_key']
    else:
        return None
Beispiel #5
0
def get_activity_sitekey(request):
    # Read the profile activity configuration file
    try:
        stream = open("%s/%s" % (locale_profile_directory_path(request), NEW_ACTIVITY_YAML), 'r')
    except IOError:
        return None

    config = yaml.load(stream)

    if 'site_key' in config:
        return config['site_key']
    else:
        return None
Beispiel #6
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)
Beispiel #7
0
def _getCurrentProfileExtent(request):
    """
    Get the extent from current profile. Get it from YAML, which saves a
    database query
    """

    # Get the path of the yaml
    path = locale_profile_directory_path(request)

    if os.path.exists(os.path.join(path, APPLICATION_YAML)):
        profile_stream = open(os.path.join(path, APPLICATION_YAML))
        profile_config = yaml.load(profile_stream)

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

        if 'geometry' in profile_config['application']:
            return profile_config['application']['geometry']

    # No local or global file found
    return 'null'
Beispiel #8
0
def get_context_layers(request):

    res = "var contextLayers = ["

    # Read the global configuration file
    try:
        global_stream = open("%s/%s" % (
            locale_profile_directory_path(request), 'application.yml'), 'r')
        config = yaml.load(global_stream)
    except IOError:
        return

    if 'layers' in config['application']:
        layers = config['application']['layers']

        for layer in layers:
            res += "new OpenLayers.Layer.WMS(\""
            res += layer['name']
            res += "\",\""
            res += layer['url']
            res += "\",{\n"

            for o in layer['wms_options']:
                for config, value in o.iteritems():
                    res += "%s: %s,\n" % (config, _cast_type(config, value))

            res += "},{\n"
            for o in layer['ol_options']:
                for config, value in o.iteritems():
                    res += "%s: %s,\n" % (config, _cast_type(config, value))

            res += "}"
            res += "),\n"

    res += "]\n"

    return res
Beispiel #9
0
def get_spatial_accuracy_map(request):
    """
    Returns a hashmap
    """

    # Get the path of the yaml
    path = locale_profile_directory_path(request)

    if os.path.exists(os.path.join(path, APPLICATION_YAML)):
        profile_stream = open(os.path.join(path, APPLICATION_YAML))
        profile_config = yaml.load(profile_stream)

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

        if 'spatial_accuracy' in profile_config['application']:
            map = profile_config['application']['spatial_accuracy']

            """
            # Translate the spatial accuracy map ...
            # This is the SQL query that needs to be written:
            # SELECT loc.value, eng.value FROM
            # (SELECT * FROM data.a_values WHERE fk_language = (SELECT id FROM
            # data.languages WHERE locale = 'fr' LIMIT 1)) AS loc JOIN
            # (SELECT * FROM data.a_values WHERE fk_language = 1 AND "value"
            # IN ('1km to 10km', 'better than 100m')) AS eng
            # ON loc.fk_a_value = eng.id
            localizer = get_localizer(request)

            locale = localizer.locale_name

            fk_lang, = DBSession.query(Language.id).filter(Language.locale ==
                locale).first()

            loc_query = DBSession.query(A_Value.id.label("loc_id"),
                                        A_Value.value.label("loc_value"),
                                        A_Value.fk_a_value.label(
                                            "loc_fk_a_value")).filter(A_Value.
            fk_language == fk_lang).subquery("loc")

            eng_query = DBSession.query(A_Value.id.label("eng_id"),
                                        A_Value.value.label("eng_value")).\
                                        filter(A_Value.fk_language == 1).\
                                        filter(A_Value.value.in_(map.keys())).
                                        subquery("eng")

            join_query = DBSession.query(loc_query, eng_query).filter(
                loc_query.c.loc_fk_a_value == eng_query.c.eng_id).subquery(
                "join_tables")

            query = DBSession.query(join_query.c.eng_value, join_query.c.
                loc_value)

            values_map = {}
            for english, translated in query.all():
                values_map[english] = translated

            translated_map = {}
            for k, v in map.items():
                translated_map[values_map[k]] = v"""

            return map

    # Spatial accuracy map is not in application.yml
    return None
Beispiel #10
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'])