Ejemplo n.º 1
0
def plugin_load_wsgi():
    from pytsite import router, reg
    from plugins import settings
    from . import _settings_form

    settings.define('mail', _settings_form.Form, 'mail_settings@mail',
                    'fa fa-envelope', 'dev')

    if not reg.get('mail.from'):
        reg.put('mail.from', 'info@' + router.server_name())
Ejemplo n.º 2
0
def switch(package_name: str):
    """Switch current theme
    """
    if package_name not in _fallback_theme_name:
        raise _error.ThemeNotRegistered(package_name)

    # Switch only if it really necessary
    if package_name != get().package_name:
        reg.put('theme.current', package_name)  # Mark theme as current
        reg.put('theme.compiled', False)  # Mark that assets compilation needed
        reload.reload()
Ejemplo n.º 3
0
def plugin_load():
    from os import path
    from pytsite import reg, plugman, on_app_load
    from . import _eh

    reg.put('paths.assets', path.join(reg.get('paths.static'), 'assets'))

    plugman.on_pre_install(_eh.on_plugman_pre_install)
    plugman.on_pre_load(_eh.on_plugman_pre_load)
    on_app_load(_eh.on_app_load)

    _api.register_package(__name__)
Ejemplo n.º 4
0
def theme_load_wsgi():
    from pytsite import reg, tpl, router, events
    from . import controllers, eh

    # Home page route
    router.handle(controllers.Home, '/', 'home')

    # Following routes required by 'content' plugin as final point while processing request
    router.handle(controllers.ContentIndex, name='content_index')
    router.handle(controllers.ContentView, name='content_view')
    router.handle(controllers.ContentModify, name='content_modify')

    # "Article index by section" route
    router.handle('content@index', '/section/<term_alias>',
                  'article_index_by_section', {
                      'model': 'article',
                      'term_field': 'section',
                  })

    # "Article index by tag" route
    router.handle('content@index', '/tag/<term_alias>', 'article_index_by_tag',
                  {
                      'model': 'article',
                      'term_field': 'tags',
                  })

    # "Article index by author" route
    router.handle('content@index', '/author/<author>',
                  'article_index_by_author', {
                      'model': 'article',
                  })

    # Event handlers
    router.on_dispatch(eh.on_router_dispatch)
    tpl.on_render(eh.on_tpl_render)
    events.listen('[email protected]_widgets.theme',
                  eh.settings_form_setup_widgets_setup_widgets_theme)

    # Force Twitter Bootstrap v3 on the password authentication page
    reg.put('auth_ui_password.twitter_bootstrap_version', 3)
Ejemplo n.º 5
0
    def _on_submit(self):
        setting_uid = self.attr('setting_uid')

        # Extract all values who's name starts with 'setting_'
        setting_value = reg.get(setting_uid, {})
        for k, v in self.values.items():
            if k.startswith('setting_'):
                k = _re.sub('^setting_', '', k)

                if isinstance(v, (list, tuple)):
                    v = util.cleanup_list(v)

                if isinstance(v, dict):
                    v = util.cleanup_dict(v)

                setting_value[k] = v

        # Update settings
        reg.put(setting_uid, setting_value)

        # Notify user
        router.session().add_success_message(lang.t('settings@settings_has_been_saved'))

        self.redirect = router.rule_url('settings@get_form', {'uid': setting_uid})
Ejemplo n.º 6
0
    def load(self):
        """Load the theme
        """
        from plugins import assetman

        # Check for requirements
        try:
            package_info.check_requirements(self._package_name)
        except package_info.error.Error as e:
            raise RuntimeError('Error while loading theme {}: {}'.format(self._package_name, e))

        # Create translations directory
        lang_dir = path.join(self._path, 'res', 'lang')
        if not path.exists(lang_dir):
            makedirs(lang_dir, 0o755, True)

        # Create translation stub files
        for lng in lang.langs():
            lng_f_path = path.join(lang_dir, '{}.yml'.format(lng))
            if not path.exists(lng_f_path):
                with open(lng_f_path, 'wt'):
                    pass

        # Register translation resources
        lang.register_package(self._package_name)

        # Register template resources
        tpl_path = path.join(self._path, 'res', 'tpl')
        if not path.exists(tpl_path):
            makedirs(tpl_path, 0o755, True)
        tpl.register_package(self._package_name)

        # Register assetman resources
        assets_path = path.join(self._path, 'res', 'assets')
        if not path.exists(assets_path):
            makedirs(assets_path, 0o755, True)
        assetman.register_package(self._package_name)

        # Load required plugins
        for pn, pv in self._requires['plugins'].items():
            plugman.load(pn, VersionRange(pv))

        # Load theme's module
        try:
            self._module = import_module(self._package_name)
            if hasattr(self._module, 'theme_load') and callable(self._module.theme_load):
                self._module.theme_load()

            # theme_load_{env.type}() hook
            env_type = reg.get('env.type')
            hook_names = ['theme_load_{}'.format(env_type)]
            if env_type == 'wsgi':
                hook_names.append('theme_load_uwsgi')
            for hook_name in hook_names:
                if hasattr(self._module, hook_name):
                    getattr(self._module, hook_name)()

            logger.debug("Theme '{}' successfully loaded".format(self._package_name))
        except Exception as e:
            raise _error.ThemeLoadError("Error while loading theme package '{}': {}".format(self._package_name, e))

        # Compile assets
        if not reg.get('theme.compiled'):
            assetman.setup()
            assetman.build(self._package_name)
            reg.put('theme.compiled', True)

        self._is_loaded = True

        return self