示例#1
0
def remove_collection_item(collection_item, commit=True):
    collection = collection_item.in_collection

    collection.items = collection.items - 1
    Session.delete(collection_item)
    Session.add(collection)

    hook_runall('collection_remove_media', collection_item=collection_item)

    if commit:
        Session.commit()
示例#2
0
def setup_celery_app(app_config, global_config,
                     settings_module=DEFAULT_SETTINGS_MODULE,
                     force_celery_always_eager=False):
    """
    Setup celery without using terrible setup-celery-module hacks.
    """
    celery_settings = get_celery_settings_dict(
        app_config, global_config, force_celery_always_eager)
    celery_app = Celery()
    celery_app.config_from_object(celery_settings)

    hook_runall('celery_setup', celery_app)
示例#3
0
def extra_validation(register_form):
    from mediagoblin.auth.tools import basic_extra_validation

    extra_validation_passes = basic_extra_validation(register_form)
    if False in hook_runall("auth_extra_validation", register_form):
        extra_validation_passes = False
    return extra_validation_passes
示例#4
0
def add_media_to_collection(collection, media, note=None, commit=True):
    collection_item = CollectionItem()
    collection_item.collection = collection.id
    collection_item.media_entry = media.id
    if note:
        collection_item.note = note
    Session.add(collection_item)

    collection.items = collection.items + 1
    Session.add(collection)
    Session.add(media)

    hook_runall("collection_add_media", collection_item=collection_item)

    if commit:
        Session.commit()
示例#5
0
def extra_validation(register_form):
    from mediagoblin.auth.tools import basic_extra_validation

    extra_validation_passes = basic_extra_validation(register_form)
    if False in hook_runall("auth_extra_validation", register_form):
        extra_validation_passes = False
    return extra_validation_passes
示例#6
0
文件: lib.py 项目: ausbin/mediagoblin
def add_media_to_collection(collection, media, note=None, commit=True):
    collection_item = CollectionItem()
    collection_item.collection = collection.id
    collection_item.get_object = media
    if note:
        collection_item.note = note
    Session.add(collection_item)

    collection.num_items = collection.num_items + 1
    Session.add(collection)
    Session.add(media)

    hook_runall('collection_add_media', collection_item=collection_item)

    if commit:
        Session.commit()
示例#7
0
def add_media_to_collection(collection, media, note=None, commit=True):
    collection_item = CollectionItem()
    collection_item.collection = collection.id
    collection_item.get_object = media
    if note:
        collection_item.note = note
    Session.add(collection_item)

    collection.num_items = collection.num_items + 1
    Session.add(collection)
    Session.add(media)

    hook_runall('collection_add_media', collection_item=collection_item)

    if commit:
        Session.commit()
示例#8
0
def setup_logging_from_paste_ini(loglevel, **kw):
    if os.path.exists(os.path.abspath('paste_local.ini')):
        logging_conf_file = 'paste_local.ini'
    else:
        logging_conf_file = 'paste.ini'

    # allow users to set up explicitly which paste file to check via the
    # PASTE_CONFIG environment variable
    logging_conf_file = os.environ.get('PASTE_CONFIG', logging_conf_file)

    if not os.path.exists(logging_conf_file):
        raise OSError('{} does not exist. Logging can not be set up.'.format(
            logging_conf_file))

    logging.config.fileConfig(logging_conf_file)

    hook_runall('celery_logging_setup')
示例#9
0
def test_hook_runall():
    """
    Test the hook_runall method
    """
    cfg = build_config(CONFIG_ALL_CALLABLES)

    mg_globals.app_config = cfg['mediagoblin']
    mg_globals.global_config = cfg

    setup_plugins()

    # Just one hook, check results
    call_log = []
    assert pluginapi.hook_runall(
        "just_one", call_log) == ["Called just once"]
    assert call_log == ["expect this one call"]

    # None provided, check results
    call_log = []
    assert pluginapi.hook_runall(
        "nothing_handling", call_log) == []
    assert call_log == []

    # Multiple provided, check results
    call_log = []
    assert pluginapi.hook_runall(
        "multi_handle", call_log) == [
            "the first returns",
            "the second returns",
            "the third returns",
        ]
    assert call_log == [
        "Hi, I'm the first",
        "Hi, I'm the second",
        "Hi, I'm the third"]

    # Multiple provided, one has CantHandleIt, check results
    call_log = []
    assert pluginapi.hook_runall(
        "multi_handle_with_canthandle", call_log) == [
            "the second returns",
            "the third returns",
        ]
    assert call_log == [
        "Hi, I'm the second",
        "Hi, I'm the third"]
示例#10
0
def setup_logging_from_paste_ini(loglevel, **kw):
    if os.path.exists(os.path.abspath('paste_local.ini')):
        logging_conf_file = 'paste_local.ini'
    else:
        logging_conf_file = 'paste.ini'

    # allow users to set up explicitly which paste file to check via the
    # PASTE_CONFIG environment variable
    logging_conf_file = os.environ.get(
        'PASTE_CONFIG', logging_conf_file)

    if not os.path.exists(logging_conf_file):
        raise IOError('{0} does not exist. Logging can not be set up.'.format(
            logging_conf_file))

    logging.config.fileConfig(logging_conf_file)

    hook_runall('celery_logging_setup')
示例#11
0
def test_hook_runall():
    """
    Test the hook_runall method
    """
    cfg = build_config(CONFIG_ALL_CALLABLES)

    mg_globals.app_config = cfg['mediagoblin']
    mg_globals.global_config = cfg

    setup_plugins()

    # Just one hook, check results
    call_log = []
    assert pluginapi.hook_runall("just_one", call_log) == ["Called just once"]
    assert call_log == ["expect this one call"]

    # None provided, check results
    call_log = []
    assert pluginapi.hook_runall("nothing_handling", call_log) == []
    assert call_log == []

    # Multiple provided, check results
    call_log = []
    assert pluginapi.hook_runall("multi_handle", call_log) == [
        "the first returns",
        "the second returns",
        "the third returns",
    ]
    assert call_log == [
        "Hi, I'm the first", "Hi, I'm the second", "Hi, I'm the third"
    ]

    # Multiple provided, one has CantHandleIt, check results
    call_log = []
    assert pluginapi.hook_runall("multi_handle_with_canthandle", call_log) == [
        "the second returns",
        "the third returns",
    ]
    assert call_log == ["Hi, I'm the second", "Hi, I'm the third"]
示例#12
0
def get_staticdirector(app_config):
    # At minimum, we need the direct_remote_path
    if not "direct_remote_path" in app_config or not "theme_web_path" in app_config:
        raise ImproperlyConfigured("direct_remote_path and theme_web_path must be provided")

    direct_domains = {None: app_config["direct_remote_path"].strip()}
    direct_domains["theme"] = app_config["theme_web_path"].strip()

    # Let plugins load additional paths
    for plugin_static in hook_runall("static_setup"):
        direct_domains[plugin_static.name] = "%s/%s" % (app_config["plugin_web_path"].rstrip("/"), plugin_static.name)

    return staticdirect.StaticDirect(direct_domains)
示例#13
0
def setup_plugins():
    """This loads, configures and registers plugins

    See plugin documentation for more details.
    """

    global_config = mg_globals.global_config
    plugin_section = global_config.get('plugins', {})

    if not plugin_section:
        _log.info("No plugins to load")
        return

    pman = pluginapi.PluginManager()

    # Go through and import all the modules that are subsections of
    # the [plugins] section and read in the hooks.
    for plugin_module, config in plugin_section.items():
        # Skip any modules that start with -. This makes it easier for
        # someone to tweak their configuration so as to not load a
        # plugin without having to remove swaths of plugin
        # configuration.
        if plugin_module.startswith('-'):
            continue

        _log.info("Importing plugin module: %s" % plugin_module)
        pman.register_plugin(plugin_module)
        # If this throws errors, that's ok--it'll halt mediagoblin
        # startup.
        __import__(plugin_module)
        plugin = sys.modules[plugin_module]
        if hasattr(plugin, 'hooks'):
            pman.register_hooks(plugin.hooks)

    # Execute anything registered to the setup hook.
    pluginapi.hook_runall('setup')
示例#14
0
def setup_plugins():
    """This loads, configures and registers plugins

    See plugin documentation for more details.
    """

    global_config = mg_globals.global_config
    plugin_section = global_config.get("plugins", {})

    if not plugin_section:
        _log.info("No plugins to load")
        return

    pman = pluginapi.PluginManager()

    # Go through and import all the modules that are subsections of
    # the [plugins] section and read in the hooks.
    for plugin_module, config in plugin_section.items():
        # Skip any modules that start with -. This makes it easier for
        # someone to tweak their configuration so as to not load a
        # plugin without having to remove swaths of plugin
        # configuration.
        if plugin_module.startswith("-"):
            continue

        _log.info("Importing plugin module: %s" % plugin_module)
        pman.register_plugin(plugin_module)
        # If this throws errors, that's ok--it'll halt mediagoblin
        # startup.
        __import__(plugin_module)
        plugin = sys.modules[plugin_module]
        if hasattr(plugin, "hooks"):
            pman.register_hooks(plugin.hooks)

    # Execute anything registered to the setup hook.
    pluginapi.hook_runall("setup")
示例#15
0
def assetlink(args):
    """
    Link the asset directory of the currently installed theme and plugins
    """
    mgoblin_app = commands_util.setup_app(args)
    app_config = mg_globals.app_config

    # link theme
    link_theme_assets(mgoblin_app.current_theme, app_config['theme_linked_assets_dir'])

    # link plugin assets
    ## ... probably for this we need the whole application initialized
    for plugin_static in pluginapi.hook_runall("static_setup"):
        link_plugin_assets(
            plugin_static, app_config['plugin_linked_assets_dir'])
示例#16
0
def assetlink(args):
    """
    Link the asset directory of the currently installed theme and plugins
    """
    mgoblin_app = commands_util.setup_app(args)
    app_config = mg_globals.app_config

    # link theme
    link_theme_assets(mgoblin_app.current_theme,
                      app_config['theme_linked_assets_dir'])

    # link plugin assets
    ## ... probably for this we need the whole application initialized
    for plugin_static in pluginapi.hook_runall("static_setup"):
        link_plugin_assets(plugin_static,
                           app_config['plugin_linked_assets_dir'])
示例#17
0
def get_staticdirector(app_config):
    # At minimum, we need the direct_remote_path
    if not 'direct_remote_path' in app_config \
            or not 'theme_web_path' in app_config:
        raise ImproperlyConfigured(
            "direct_remote_path and theme_web_path must be provided")

    direct_domains = {None: app_config['direct_remote_path'].strip()}
    direct_domains['theme'] = app_config['theme_web_path'].strip()

    # Let plugins load additional paths
    for plugin_static in hook_runall("static_setup"):
        direct_domains[plugin_static.name] = "{}/{}".format(
            app_config['plugin_web_path'].rstrip('/'), plugin_static.name)

    return staticdirect.StaticDirect(direct_domains)
示例#18
0
def test_plugin_assetlink(static_plugin_app):
    """
    Test that the assetlink command works correctly
    """
    linked_assets_dir = mg_globals.app_config['plugin_linked_assets_dir']
    plugin_link_dir = os.path.join(linked_assets_dir.rstrip(os.path.sep),
                                   'staticstuff')

    plugin_statics = pluginapi.hook_runall("static_setup")
    assert len(plugin_statics) == 1
    plugin_static = plugin_statics[0]

    def run_assetlink():
        printer = CollectingPrinter()

        link_plugin_assets(plugin_static, linked_assets_dir, printer)

        return printer

    # it shouldn't exist yet
    assert not os.path.lexists(plugin_link_dir)

    # link dir doesn't exist, link it
    result = run_assetlink().collection[0]
    assert result == \
        'Linked asset directory for plugin "staticstuff":\n  {}\nto:\n  {}\n'.format(
            plugin_static.file_path.rstrip(os.path.sep),
            plugin_link_dir)
    assert os.path.lexists(plugin_link_dir)
    assert os.path.islink(plugin_link_dir)
    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path

    # link dir exists, leave it alone
    # (and it should exist still since we just ran it..)
    result = run_assetlink().collection[0]
    assert result == 'Skipping "staticstuff"; already set up.\n'
    assert os.path.lexists(plugin_link_dir)
    assert os.path.islink(plugin_link_dir)
    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path

    # link dir exists, is a symlink to somewhere else (re-link)
    junk_file_path = os.path.join(linked_assets_dir.rstrip(os.path.sep),
                                  'junk.txt')
    with open(junk_file_path, 'w') as junk_file:
        junk_file.write('barf')

    os.unlink(plugin_link_dir)
    os.symlink(junk_file_path, plugin_link_dir)

    result = run_assetlink().combined_string
    assert result == """Old link found for "staticstuff"; removing.
Linked asset directory for plugin "staticstuff":
  {}
to:
  {}
""".format(plugin_static.file_path.rstrip(os.path.sep), plugin_link_dir)
    assert os.path.lexists(plugin_link_dir)
    assert os.path.islink(plugin_link_dir)
    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path

    # link dir exists, but is a non-symlink
    os.unlink(plugin_link_dir)
    with open(plugin_link_dir, 'w') as clobber_file:
        clobber_file.write('clobbered!')

    result = run_assetlink().collection[0]
    assert result == 'Could not link "staticstuff": %s exists and is not a symlink\n' % (
        plugin_link_dir)

    with open(plugin_link_dir) as clobber_file:
        assert clobber_file.read() == 'clobbered!'
示例#19
0
def test_plugin_assetlink(static_plugin_app):
    """
    Test that the assetlink command works correctly
    """
    linked_assets_dir = mg_globals.app_config['plugin_linked_assets_dir']
    plugin_link_dir = os.path.join(
        linked_assets_dir.rstrip(os.path.sep),
        'staticstuff')

    plugin_statics = pluginapi.hook_runall("static_setup")
    assert len(plugin_statics) == 1
    plugin_static = plugin_statics[0]

    def run_assetlink():
        printer = CollectingPrinter()

        link_plugin_assets(
            plugin_static, linked_assets_dir, printer)

        return printer

    # it shouldn't exist yet
    assert not os.path.lexists(plugin_link_dir)

    # link dir doesn't exist, link it
    result = run_assetlink().collection[0]
    assert result == \
        'Linked asset directory for plugin "staticstuff":\n  %s\nto:\n  %s\n' % (
            plugin_static.file_path.rstrip(os.path.sep),
            plugin_link_dir)
    assert os.path.lexists(plugin_link_dir)
    assert os.path.islink(plugin_link_dir)
    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path

    # link dir exists, leave it alone
    # (and it should exist still since we just ran it..)
    result = run_assetlink().collection[0]
    assert result == 'Skipping "staticstuff"; already set up.\n'
    assert os.path.lexists(plugin_link_dir)
    assert os.path.islink(plugin_link_dir)
    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path

    # link dir exists, is a symlink to somewhere else (re-link)
    junk_file_path = os.path.join(
        linked_assets_dir.rstrip(os.path.sep),
        'junk.txt')
    with file(junk_file_path, 'w') as junk_file:
        junk_file.write('barf')

    os.unlink(plugin_link_dir)
    os.symlink(junk_file_path, plugin_link_dir)

    result = run_assetlink().combined_string
    assert result == """Old link found for "staticstuff"; removing.
Linked asset directory for plugin "staticstuff":
  %s
to:
  %s
""" % (plugin_static.file_path.rstrip(os.path.sep), plugin_link_dir)
    assert os.path.lexists(plugin_link_dir)
    assert os.path.islink(plugin_link_dir)
    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path

    # link dir exists, but is a non-symlink
    os.unlink(plugin_link_dir)
    with file(plugin_link_dir, 'w') as clobber_file:
        clobber_file.write('clobbered!')

    result = run_assetlink().collection[0]
    assert result == 'Could not link "staticstuff": %s exists and is not a symlink\n' % (
        plugin_link_dir)

    with file(plugin_link_dir, 'r') as clobber_file:
        assert clobber_file.read() == 'clobbered!'
示例#20
0
def add_to_global_context(context):
    if len(pluginapi.hook_runall('authentication')) == 1:
        context['persona_auth'] = True
    context['persona'] = True
    return context
示例#21
0
def create_user(register_form):
    results = hook_runall("auth_create_user", register_form)
    return results[0]
示例#22
0
def create_user(register_form):
    results = hook_runall("auth_create_user", register_form)
    return results[0]
示例#23
0
def add_to_global_context(context):
    if len(pluginapi.hook_runall('authentication')) == 1:
        context['persona_auth'] = True
    context['persona'] = True
    return context