Example #1
0
    def layer(self):
        from footprint.main.models.presentation.layer.layer import Layer

        try:
            return one_or_none(self.db_entity_interest_owner.presentationmedium_set.all().select_subclasses(Layer))
        except Exception, e:
            logger.warn(e.message)
Example #2
0
 def layer(self):
     from footprint.main.models.presentation.layer.layer import Layer
     try:
         return one_or_none(
             self.db_entity_interest_owner.presentationmedium_set.all(
             ).select_subclasses(Layer))
     except Exception, e:
         logger.warn(e.message)
def update_or_create_layer(layer_library,
                           layer_configuration=None,
                           db_entity_key=None,
                           skip_layer_selection=False):
    """
        Create a Layer for each DbEntity in the LayerConfiguration, These instances constitute the default
        LayerLibrary of the config_entity, which is a library of all DbEntities. The media of these instances can be set
        to style media.
    :param layer_configuration: Optional. A full configuration
    :param db_entity_key. Optional. Use instead of full configuration to generate defaults
    :return:
    """

    # Turn off all layer post_save_publishing, otherwise saving the layer below would infinitely recurse
    # The only layer save that triggers on_layer_post_save is an independent save of a layer.
    # The code here is called by that or by the on_config_entity_post_save layer
    Layer._no_post_save_publishing = True

    db_entity_key = db_entity_key or layer_configuration.db_entity_key
    owning_db_entity_interest = DbEntityInterest.objects.get(
        config_entity=layer_library.config_entity,
        db_entity__key=db_entity_key)

    existing_layer = one_or_none(
        Layer.objects.filter(db_entity_interest=owning_db_entity_interest))

    if not layer_configuration:
        layer_configuration = resolve_layer_configuration(
            layer_library.config_entity_subclassed, db_entity_key,
            existing_layer)

        logger.info("Layer configuration %s" % layer_configuration)

    # Resolve the active DbEntity of the ConfigEntity based on the key of the LayerConfiguration
    db_entity_key = layer_configuration.db_entity_key
    logger.info("Layer Publishing DbEntity Key: %s" % db_entity_key)

    try:
        db_entity = layer_library.config_entity_subclassed.computed_db_entities(
        ).get(key=db_entity_key)
    except Exception, e:
        raise Exception(
            "db_entity_key {0} does not exist for config_entity {1}. Did you configure the LayerConfiguration for the wrong scope? Original exception: {2}"
            .format(db_entity_key, layer_library.config_entity_subclassed,
                    e.message))
def update_or_create_layer(layer_library, layer_configuration=None, db_entity_key=None, skip_layer_selection=False):
    """
        Create a Layer for each DbEntity in the LayerConfiguration, These instances constitute the default
        LayerLibrary of the config_entity, which is a library of all DbEntities. The media of these instances can be set
        to style media.
    :param layer_configuration: Optional. A full configuration
    :param db_entity_key. Optional. Use instead of full configuration to generate defaults
    :return:
    """

    # Turn off all layer post_save_publishing, otherwise saving the layer below would infinitely recurse
    # The only layer save that triggers on_layer_post_save is an independent save of a layer.
    # The code here is called by that or by the on_config_entity_post_save layer
    Layer._no_post_save_publishing = True

    db_entity_key = db_entity_key or layer_configuration.db_entity_key
    owning_db_entity_interest = DbEntityInterest.objects.get(
        config_entity=layer_library.config_entity,
        db_entity__key=db_entity_key)

    existing_layer = one_or_none(
        Layer.objects.filter(
            db_entity_interest=owning_db_entity_interest)
    )

    if not layer_configuration:
        layer_configuration = resolve_layer_configuration(
                layer_library.config_entity_subclassed,
                db_entity_key, existing_layer)

        logger.info("Layer configuration %s" % layer_configuration)

    # Resolve the active DbEntity of the ConfigEntity based on the key of the LayerConfiguration
    db_entity_key = layer_configuration.db_entity_key
    logger.info("Layer Publishing DbEntity Key: %s" % db_entity_key)

    try:
        db_entity = layer_library.config_entity_subclassed.computed_db_entities().get(key=db_entity_key)
    except Exception, e:
        raise Exception(
            "db_entity_key {0} does not exist for config_entity {1}. Did you configure the LayerConfiguration for the wrong scope? Original exception: {2}".format(
                db_entity_key, layer_library.config_entity_subclassed, e.message))
Example #5
0
def update_or_create_result_libraries(config_entity, **kwargs):
    """
        Creates a ResultLibrary and its Result instances upon saving a config_entity if they do not yet exist.
        :param config_entity
        :param kwargs: 'db_entity_keys' Optional list to limit the Results processed. Any result whose
            result_db_entity_key or source_db_entity_key is in db_entity_keys will pass through.
        :return:
    """

    # Force adoption. This is primarily so scenarios get the result_library from their parent project.
    for presentation in config_entity.presentation_set.all():
        presentation_class = type(presentation)
        presentation_subclass = presentation_class.objects.get_subclass(
            id=presentation.id)
        if presentation_subclass.presentation_media_alias:
            presentation_subclass._adopt_from_donor(
                presentation_subclass.presentation_media_alias)
    db_entity_keys = kwargs.get(
        'db_entity_keys',
        map(lambda db_entity: db_entity.key,
            config_entity.owned_db_entities()))

    from footprint.client.configuration.fixture import ResultConfigurationFixture
    from footprint.client.configuration.utils import resolve_fixture

    client_result = resolve_fixture("presentation",
                                    "result",
                                    ResultConfigurationFixture,
                                    config_entity.schema(),
                                    config_entity=config_entity)

    client_result.update_or_create_media(
        config_entity, db_entity_keys=kwargs.get('db_entity_keys'))

    # Create each ResultLibrary and store them as a dict keyed by their key
    result_library_lookup = map_to_dict(
        lambda result_library_config: [
            result_library_config.key,
            ResultLibrary.objects.update_or_create(
                key=result_library_config.key,
                config_entity=config_entity,
                scope=config_entity.schema(),
                defaults=dict(name=result_library_config.name.format(
                    titleize(config_entity.key)),
                              description=result_library_config.description.
                              format(config_entity.name)))[0]
        ], client_result.result_libraries())

    # Create each configured Result
    for result_config in filter(
            lambda result: result.result_db_entity_key in db_entity_keys or
            result.source_db_entity_key in db_entity_keys,
            client_result.results()):

        logger.info(
            "Result Publishing Result DbEntity Key: %s from Source DbEntity Key %s"
            % (result_config.result_db_entity_key,
               result_config.source_db_entity_key))
        # Make the db_entity the default selected one for its key
        previous = config_entity._no_post_save_publishing
        config_entity._no_post_save_publishing = True
        config_entity.save()
        config_entity._no_post_save_publishing = previous

        # Get the Results DbEntity if it already exists
        existing_db_entity_interest = one_or_none(
            DbEntityInterest.objects.filter(
                config_entity=config_entity,
                db_entity__key=result_config.result_db_entity_key))
        existing_result = one_or_none(
            Result.objects.filter(
                db_entity_interest=existing_db_entity_interest))
        # Create the db_entity and db_entity_interest for the result if it doesn't exist
        db_entity_interest = result_config.update_or_create_db_entity_interest(
            config_entity, existing_result
            and existing_result.db_entity_interest)
        db_entity = db_entity_interest.db_entity
        # Test the query. This will raise an error if the query was configured wrong
        db_entity.parse_query(config_entity)

        dummy_user = get_user_model().objects.get(
            username=UserGroupKey.SUPERADMIN)

        # Create a result for each result key given.
        result, created, updated = Result.objects.update_or_create(
            db_entity_interest=db_entity_interest,
            name=result_config.name,
            defaults=dict(
                # Use the Result's custom Medium, keyed by the Result key
                medium=result_config.resolve_result_medium(),
                configuration=result_config.
                get_presentation_medium_configuration(),
                creator=dummy_user,
                updater=dummy_user))

        # If created, add the result to the matching result libraries, always including the Default Library
        # Use the related_collection_adoption to make sure donor results are adopted prior
        # to adding the result if they haven't yet been
        if created:
            for library_key in [ResultLibraryKey.DEFAULT
                                ] + result_config.library_keys:
                result_library_lookup[library_key]._add(
                    'presentation_media', result)
def update_or_create_result_libraries(config_entity, **kwargs):
    """
        Creates a ResultLibrary and its Result instances upon saving a config_entity if they do not yet exist.
        :param config_entity
        :param kwargs: 'db_entity_keys' Optional list to limit the Results processed. Any result whose
            result_db_entity_key or source_db_entity_key is in db_entity_keys will pass through.
        :return:
    """

    # Force adoption. This is primarily so scenarios get the result_library from their parent project.
    for presentation in config_entity.presentation_set.all():
        presentation_class = type(presentation)
        presentation_subclass = presentation_class.objects.get_subclass(id=presentation.id)
        if presentation_subclass.presentation_media_alias:
            presentation_subclass._adopt_from_donor(presentation_subclass.presentation_media_alias)
    db_entity_keys = kwargs.get(
        'db_entity_keys',
        map(lambda db_entity: db_entity.key, config_entity.owned_db_entities())
    )

    from footprint.client.configuration.fixture import ResultConfigurationFixture
    from footprint.client.configuration.utils import resolve_fixture

    client_result = resolve_fixture(
        "presentation",
        "result",
        ResultConfigurationFixture,
        config_entity.schema(),
        config_entity=config_entity)

    client_result.update_or_create_media(config_entity, db_entity_keys=kwargs.get('db_entity_keys'))

    # Create each ResultLibrary and store them as a dict keyed by their key
    result_library_lookup = map_to_dict(lambda result_library_config: [
        result_library_config.key,
        ResultLibrary.objects.update_or_create(
            key=result_library_config.key,
            config_entity=config_entity,
            scope=config_entity.schema(),
            defaults=dict(
                name=result_library_config.name.format(titleize(config_entity.key)),
                description=result_library_config.description.format(config_entity.name)
            )
        )[0]],
        client_result.result_libraries())

    # Create each configured Result
    for result_config in filter(
            lambda result:
                result.result_db_entity_key in db_entity_keys or
                result.source_db_entity_key in db_entity_keys,
            client_result.results()):

        logger.info("Result Publishing Result DbEntity Key: %s from Source DbEntity Key %s" %
                    (result_config.result_db_entity_key, result_config.source_db_entity_key))
        # Make the db_entity the default selected one for its key
        previous = config_entity._no_post_save_publishing
        config_entity._no_post_save_publishing = True
        config_entity.save()
        config_entity._no_post_save_publishing = previous

        # Get the Results DbEntity if it already exists
        existing_db_entity_interest = one_or_none(DbEntityInterest.objects.filter(
            config_entity=config_entity,
            db_entity__key=result_config.result_db_entity_key
        ))
        existing_result = one_or_none(Result.objects.filter(db_entity_interest=existing_db_entity_interest))
        # Create the db_entity and db_entity_interest for the result if it doesn't exist
        db_entity_interest = result_config.update_or_create_db_entity_interest(config_entity, existing_result and existing_result.db_entity_interest)
        db_entity = db_entity_interest.db_entity
        # Test the query. This will raise an error if the query was configured wrong
        db_entity.parse_query(config_entity)

        dummy_user = get_user_model().objects.get(username=UserGroupKey.SUPERADMIN)

        # Create a result for each result key given.
        result, created, updated = Result.objects.update_or_create(
            db_entity_interest=db_entity_interest,
            name=result_config.name,
            defaults=dict(
                # Use the Result's custom Medium, keyed by the Result key
                medium=result_config.resolve_result_medium(),
                configuration=result_config.get_presentation_medium_configuration(),
                creator=dummy_user,
                updater=dummy_user)
        )

        # If created, add the result to the matching result libraries, always including the Default Library
        # Use the related_collection_adoption to make sure donor results are adopted prior
        # to adding the result if they haven't yet been
        if created:
            for library_key in [ResultLibraryKey.DEFAULT] + result_config.library_keys:
                result_library_lookup[library_key]._add('presentation_media', result)