Esempio n. 1
0
def get_app(config, active_callback=None):
    """
    :param config: the configuration dict
    :param active_callback: a callback without arguments that will be called when the app is fully initialized
    :return: A new app
    """
    config = _put_configuration_defaults(config)

    task_directory = config["tasks_directory"]
    default_allowed_file_extensions = config['allowed_file_extensions']
    default_max_file_size = config['max_file_size']

    appli = web.application((), globals(), autoreload=False)

    # Init the different parts of the app
    plugin_manager = PluginManager()

    mongo_client = MongoClient(
        host=config.get('mongo_opt', {}).get('host', 'localhost'))
    database = mongo_client[config.get('mongo_opt',
                                       {}).get('database', 'INGInious')]
    gridfs = GridFS(database)

    course_factory, task_factory = create_factories(task_directory,
                                                    plugin_manager,
                                                    FrontendCourse,
                                                    FrontendTask)

    user_manager = UserManager(
        CustomSession(appli, MongoStore(database, 'sessions')), database)

    backend_interface.update_pending_jobs(database)

    job_manager = backend_interface.create_job_manager(config, plugin_manager,
                                                       task_directory,
                                                       course_factory,
                                                       task_factory)

    lis_outcome_manager = LisOutcomeManager(database, user_manager,
                                            course_factory, config["lti"])

    submission_manager = LTISubmissionManager(
        job_manager, user_manager, database, gridfs, plugin_manager,
        config.get('nb_submissions_kept', 5), lis_outcome_manager)

    template_helper = TemplateHelper(plugin_manager, 'frontend/lti/templates',
                                     'layout',
                                     config.get('use_minified_js', True))

    # Update the database
    update_database(database)

    # Add some helpers for the templates
    template_helper.add_to_template_globals("user_manager", user_manager)
    template_helper.add_to_template_globals("default_allowed_file_extensions",
                                            default_allowed_file_extensions)
    template_helper.add_to_template_globals("default_max_file_size",
                                            default_max_file_size)

    # Not found page
    appli.notfound = lambda: web.notfound(template_helper.get_renderer().
                                          notfound('Page not found'))

    # Init the mapping of the app
    appli.init_mapping(
        WebPyCustomMapping(dict(urls), plugin_manager, course_factory,
                           task_factory, submission_manager, user_manager,
                           template_helper, database, gridfs,
                           default_allowed_file_extensions,
                           default_max_file_size, config["containers"].keys(),
                           config["lti"]))

    # Active hook
    if active_callback is not None:
        _handle_active_hook(job_manager, plugin_manager, active_callback)

    # Loads plugins
    plugin_manager.load(job_manager, appli, course_factory, task_factory,
                        database, user_manager, config.get("plugins", []))

    # Start the inginious.backend
    job_manager.start()

    return appli, lambda: _close_app(appli, mongo_client, job_manager,
                                     lis_outcome_manager)