def get_width_alias_from_plugin(plugin):
    """
    This function is called from different context:

    TODO This would be a lot more explicit, if we would make width_alias a mandatory parameter on render_image and
         evaluate the correct width_alias on a property on the plugin itself. (e.g on AllinkImagePlugin,
         AllinkGalleryPlugin or AllinkBaseAppContentPlugin)

    1. inside a AllinkContentColumnPlugin -> the width_alias corresponding to the parents
       (always a CMSAllinkContentPlugin) template field is returned

    2. AllinkBaseAppContentPlugin -> the width_alias corresponding to the field items_per_row is returned

    3. some other context or without a plugin instance -> always '2-of-3': this is a fallback, but should not come up
       if the correct width_alias is supplied in the template

    :param plugin:
    a model instance of a cms plugin

    :return:
    a string with the appropriate width alias depending on which context the plugin is embedded.
    """
    if not hasattr(plugin, 'items_per_row'):
        try:
            column_plugin = None
            potential_column = plugin.parent.get_plugin_instance()[0]
            while column_plugin is None:
                if (potential_column.plugin_type ==
                    'CMSAllinkContentColumnPlugin'):
                    column_plugin = potential_column
                else:
                    potential_column = (potential_column.parent.
                        get_plugin_instance()[0])
            if column_plugin.template == 'col-1':
                return '1-of-1'
            elif column_plugin.template == 'col-1-1':
                return '1-of-2'
            elif column_plugin.template == 'col-1-2':
                return '1-of-3' if column_plugin.position == 0 else '2-of-3'
            elif column_plugin.template == 'col-2-1':
                return '2-of-3' if column_plugin.position == 0 else '1-of-3'
            elif column_plugin.template == 'col-3':
                return '1-of-3'
            elif column_plugin.template == 'col-4':
                return '1-of-4'
            else:
                return '1-of-1'
        except AttributeError:
            return '2-of-3'
    elif hasattr(plugin, 'items_per_row'):
        return '1-of-{}'.format(getattr(plugin, 'items_per_row', '1'))
    else:
        return '2-of-3'
