def apps_list(request): """ Show list of applications with enabled administrative interface and all models in each application. """ # get list of all applications with admin.py file apps = [appname for appname in installed_apps() if os.path.exists(os.path.join(appname, "admin.py"))] apps_models = [] # get list of all models in each application for app_name in apps: apps_models.append((app_name, _models_list(app_name))) return request.render("admin/apps_list", apps=apps_models)
def prepare_template_engine(self): # load template tags and set global template directory if not django_settings.TEMPLATE_DIRS: django_settings.TEMPLATE_DIRS = (os.path.join(self._project_dir, 'templates'), ) # use patched and additional template tags template.register_template_library('gae.tags') # register template tags for each application for app_name in installed_apps(): try: mod = __import__("apps.%s.tags" % app_name, globals(), {}, app_name) template.django.template.libraries[app_name] = mod.register except ImportError: pass
def _map_urls(self, app_name, parent_rule={}): urls = [] if app_name not in installed_apps(): raise Exception("Application %s defined in rule %r is not exists" % (app_name, parent_rule)) for rule in get_config('%s.urls' % app_name, {}): if "url" not in rule: raise Exception("Not defined 'url' argument in the urls mapping for application '%s'. Rule: %r" % (app_name, rule)) if "run" not in rule: raise Exception("Not defined 'run' argument in the urls mapping for application '%s'. Rule: %r" % (app_name, rule)) if rule["run"].count('.') == 1: # run: app.controller try: rule['url'] = self._prepare_url(rule['url'], parent_rule.get('url')) except IncorrectUrlDefinition, e: raise IncorrectUrlDefinition("Url rule %r in app %s is incorrect. Error: %s" % (rule, app_name, e)) urls.append(rule) elif rule["run"].count('.') == 0: # run: app urls.extend(self._map_urls(rule["run"], rule))
def load_models(): ''' Load models from all installed applications. This is need to resolve conflict with references between some models placed in different applications. ''' flag_name = "gae_models_loaded" if flag_name in db._kind_map: return False for app_name in installed_apps(): try: app_models = __import__("apps.%s.models" % app_name, {}, {}, ["models"]) except ImportError, e: logging.warning("File 'apps/%s/models.py' not loaded. %s" % (app_name, e)) continue models = [getattr(app_models, model_name) for model_name in dir(app_models) if not model_name.startswith("_") and model_name[0].isupper()] for model in models: db._kind_map[model.kind()] = model
""" Wrapper for loading templates from apps "templates" directories. """ import os, sys, logging from django.conf import settings from django.template import TemplateDoesNotExist from django.template.loader import BaseLoader from django.utils._os import safe_join from gae.tools import installed_apps # At compile time, cache the directories to search. fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() app_template_dirs = {} for app in installed_apps(): template_dir = os.path.join('apps', app, 'templates') if os.path.isdir(template_dir): app_template_dirs[app] = template_dir.decode(fs_encoding) class Loader(BaseLoader): is_usable = True def get_template_sources(self, template_name, template_dirs=None): """ Returns the absolute paths to "template_name", when appended to each directory in "template_dirs". Any paths that don't lie inside one of the template dirs are excluded from the result set, for security reasons. """ if not template_dirs: template_dirs = app_template_dirs
def __init__(self): # get list of configuration files apps_configs = [(app_name, os.path.abspath(os.path.join('apps', app_name, "config.yaml"))) for app_name in installed_apps()] # load configuration files for app_name, app_config in apps_configs: self._apps_configs[app_name] = self._load_config(app_config)
def installed_apps(self): return installed_apps()