Example #1
0
    def django_init(self, context="context.py"):
        "Initializes the django engine. The root must have been set already!"

        BASE_APP = [ ]
        try:
            # checks if the root is importable
            base = os.path.split(self.root)[-1]
            importlib.import_module(base)
            logger.info("imported %s" % base)
            BASE_APP = [ base ]
        except Exception as exc:
            logger.info("%s" % exc)

        settings.configure(
            DEBUG=True, TEMPLATE_DEBUG=True,
            TEMPLATE_DIRS=(self.root, TEMPLATE_DIR),
            TEMPLATE_LOADERS=(
                'django.template.loaders.filesystem.Loader',

            ),
            INSTALLED_APPS=["pyblue", "django.contrib.humanize"] + BASE_APP,
            TEMPLATE_STRING_IF_INVALID = " ??? ",
        )

        django.setup()

        logger.info("templatetag modules: %s" % ", ".join(get_templatetags_modules()))
def get_tag_libraries():
    opts = []
    for module in get_templatetags_modules():
        mod = __import__(module,fromlist=['foo'])
        for l,m,i in pkgutil.iter_modules([os.path.dirname(mod.__file__)]):
            opts.append({'word':m,'menu':mod.__name__})

    return opts
def get_library(library_name, app_name=None):
    """
    (Forked from django.template.get_library)

    Load the template library module with the given name.

    If library is not already loaded loop over all templatetags modules to locate it.

    {% load somelib %} and {% load someotherlib %} loops twice.
    """
    # TODO: add in caching. (removed when forked from django.template.get_library).
    templatetags_modules = get_templatetags_modules()
    tried_modules = []
    best_match_lib = None
    last_found_lib = None
    app_name_parts = 0
    if app_name:
        app_name_parts = app_name.count(".")
    for module in templatetags_modules:
        taglib_module = "%s.%s" % (module, library_name)
        tried_modules.append(taglib_module)
        lib = import_library(taglib_module)
        if not lib:
            continue
        last_found_lib = lib

        if not app_name:
            continue

        module_list = module.split(".")
        module_list.pop()  # remove the last part 'templetags'
        current_app = ".".join(module_list)
        if current_app == app_name:
            break

        start = len(module_list) - app_name_parts - 1
        if start < 0:
            continue

        partial_app = ".".join(module_list[start:])
        if partial_app == app_name:
            best_match_lib = lib

    if best_match_lib:
        last_found_lib = best_match_lib
    if not last_found_lib:
        raise InvalidTemplateLibrary(
            "Template library %s not found, tried %s" % (library_name, ",".join(tried_modules))
        )

    return last_found_lib
Example #4
0
def load_all_installed_template_libraries():
    # Load/register all template tag libraries from installed apps.
    for module_name in template.get_templatetags_modules():
        mod = import_module(module_name)
        libraries = [
            os.path.splitext(p)[0]
            for p in os.listdir(os.path.dirname(mod.__file__))
            if p.endswith('.py') and p[0].isalpha()
        ]
        for library_name in libraries:
            try:
                lib = template.get_library(library_name)
            except template.InvalidTemplateLibrary, e:
                pass
Example #5
0
def load_all_installed_template_libraries():
    # Load/register all template tag libraries from installed apps.
    for module_name in template.get_templatetags_modules():
        mod = import_module(module_name)
        if not hasattr(mod, '__file__'):
            # e.g. packages installed as eggs
            continue

        try:
            libraries = [
                os.path.splitext(p)[0]
                for p in os.listdir(os.path.dirname(upath(mod.__file__)))
                if p.endswith('.py') and p[0].isalpha()
            ]
        except OSError:
            continue
        else:
            for library_name in libraries:
                try:
                    template.get_library(library_name)
                except template.InvalidTemplateLibrary:
                    pass
Example #6
0
def get_library(library_name, app_name=None):
    """
    (Forked from django.template.get_library)

    Load the template library module with the given name.

    If library is not already loaded loop over all templatetags modules to locate it.

    {% load somelib %} and {% load someotherlib %} loops twice.
    """
    #TODO: add in caching. (removed when forked from django.template.get_library).
    templatetags_modules = get_templatetags_modules()
    tried_modules = []
    for module in templatetags_modules:
        taglib_module = '%s.%s' % (module, library_name)
        tried_modules.append(taglib_module)
        lib = import_library(taglib_module)
        if lib and app_name and taglib_module.split('.')[-3] == app_name:
            break
    if not lib:
        raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules)))

    return lib
Example #7
0
from forum.modules import ui, get_modules_script
from django.utils.translation import ugettext as _
from django.utils.encoding import smart_unicode
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify

from forum.templatetags.extra_tags import get_score_badge
from forum.utils.html import cleanup_urls
from forum import settings


try:
    from django.template import get_templatetags_modules
    modules_template_tags = get_modules_script('templatetags')
    django_template_tags = get_templatetags_modules()

    for m in modules_template_tags:
        django_template_tags.append(m.__name__)
except:
    pass

ui.register(ui.HEADER_LINKS,
            ui.Link(_('faq'), ui.Url('faq'), weight=400, name='FAQ'),
            ui.Link(_('about'), ui.Url('about'), weight=300, name='ABOUT'),

            ui.Link(
                    text=lambda u, c: u.is_authenticated() and _('logout') or _('login'),
                    url=lambda u, c: u.is_authenticated() and reverse('logout') or reverse('auth_signin'),
                    weight=200, name='LOGIN/OUT'),

            ui.Link(