Esempio n. 1
0
def render(appname, app_model, **kw):
    '''
    Render model with theme.
    '''
    th = get_current_theme()
    logging.info('render using theme "%s"...' % th)
    # load widget:
    # TODO...
    # prepare model for theme:
    site = siteconfig.get_site_settings()
    tz = site.get_tzinfo()
    utils = runtime.get_runtime_utils(tz, str(site.date_format), str(site.time_format))
    app_model['utils'] = utils
    app_model['user'] = kw['current_user']
    app_model['site'] = site
    embedded_app = view.render(appname, app_model)
    title = site.title
    app_title = app_model.get('__title__', None)
    if app_title:
        title = app_title + ' - ' + title
    model = {
            '__view__' : 'template',
            '__app__' : embedded_app,
            '__header__' : app_model.get('__header__', ''),
            '__footer__' : app_model.get('__footer__', ''),
            'utils' : utils,
            'app' : appname,
            'user' : kw['current_user'],
            'title' : title,
            'site' : site,
            'navigations' : navigation.get_navigation(),
    }
    return view.render('theme', model, view_dir=th)
Esempio n. 2
0
def render(appname, app_model, **kw):
    '''
    Render model with theme.
    '''
    th = get_current_theme()
    logging.info('render using theme "%s"...' % th)
    # load widget:
    # TODO...
    # prepare model for theme:
    site = siteconfig.get_site_settings()
    tz = site.get_tzinfo()
    utils = runtime.get_runtime_utils(tz, str(site.date_format),
                                      str(site.time_format))
    app_model['utils'] = utils
    app_model['user'] = kw['current_user']
    app_model['site'] = site
    embedded_app = view.render(appname, app_model)
    title = site.title
    app_title = app_model.get('__title__', None)
    if app_title:
        title = app_title + ' - ' + title
    model = {
        '__view__': 'template',
        '__app__': embedded_app,
        '__header__': app_model.get('__header__', ''),
        '__footer__': app_model.get('__footer__', ''),
        'utils': utils,
        'app': appname,
        'user': kw['current_user'],
        'title': title,
        'site': site,
        'navigations': navigation.get_navigation(),
    }
    return view.render('theme', model, view_dir=th)
Esempio n. 3
0
 def test_render(self):
     model = {
         'title': 'Life & Style',
     }
     self.assertRaises(view.RenderError, view.render, 'http_test', model)
     model['__view__'] = 'undefined'
     self.assertRaises(view.RenderError, view.render, 'http_test', model)
     model['__view__'] = 'main'
     t = str(view.render('http_test', model))
     self.assertTrue(t.find('<h1>Life &amp; Style</h1>') >= 0)
     self.assertTrue(t.find('<p>Life & Style</p>') >= 0)
Esempio n. 4
0
 def test_render(self):
     model = {
             'title' : 'Life & Style',
     }
     self.assertRaises(view.RenderError, view.render, 'http_test', model)
     model['__view__'] = 'undefined'
     self.assertRaises(view.RenderError, view.render, 'http_test', model)
     model['__view__'] = 'main'
     t = str(view.render('http_test', model))
     self.assertTrue(t.find('<h1>Life &amp; Style</h1>')>=0)
     self.assertTrue(t.find('<p>Life & Style</p>')>=0)
Esempio n. 5
0
 def _render_template(self, kw, appname, model):
     '''
     Render a template using the given model.
     
     Args:
         model: model as dict.
     '''
     use_theme = model.get('__theme__', False) == True
     if use_theme:
         t = theme.render(appname, model, **kw)
     else:
         t = view.render(appname, model)
     content_type = model.get('__content_type__')
     if content_type:
         self.response.content_type = content_type
     self.response.out.write(t)
Esempio n. 6
0
def do_manage(**kw):
    current_user = kw['current_user']
    if current_user is None:
        return 'redirect:/manage/signin?redirect=%s/manage/' % kw['request'].host_url
    ctx = kw['context']
    role = current_user.role
    app = ctx.get_argument('app', 'manage')
    command = ctx.get_argument('command', 'profile')
    menus = []
    selected_menu = None
    selected_menu_item = None
    for appname in appconfig.apps:
        logging.info('Import %s.appmanage...' % appname)
        app_mod = __import__(appname, fromlist=['appmanage']).appmanage
        for m in app_mod.get_menus():
            fm = _filter_menu(m, role, appname)
            if fm is not None:
                menus.append(fm)
                if appname==app:
                    for item in fm.items:
                        if item.command==command:
                            selected_menu_item = item
                            selected_menu = fm
                            break
    # check permission:
    if selected_menu_item is None or selected_menu_item.role < current_user.role:
        raise ApplicationError('No command found.')
    model = {
            'user' : current_user,
            'app' : app,
            'command' : command,
            'menus' : menus,
            'selected_menu' : selected_menu,
            'selected_menu_item' : selected_menu_item,
            'version' : get_version(),
    }
    req = kw['request']
    embed_context = web.Context()
    embed_context.method = req.method.lower()
    embed_context.get_argument = lambda argument_name, default_value=None: req.get(argument_name, default_value)
    embed_context.get_arguments = lambda argument_name: req.get_all(argument_name)
    embed_context.arguments = lambda: req.arguments()
    app_mod = __import__(app, fromlist=['appmanage']).appmanage
    # call app's manage method and get the embed model:
    embedded_model = app_mod.manage(current_user, app, command, embed_context)
    if isinstance(embedded_model, basestring):
        return embedded_model
    if not isinstance(embedded_model, dict):
        raise ApplicationError(r'"%s.appmanage()" must return a dict or basestring.' % app)
    tz = siteconfig.get_site_settings().get_tzinfo()
    embedded_model['app'] = app
    embedded_model['command'] = command
    embedded_model['user'] = current_user
    embedded_model['utils'] = runtime.get_runtime_utils(tz)
    embedded = view.render(app, embedded_model)
    # copy some value from embed_model:
    for key in ['info', 'warning', 'error']:
        if key in embedded_model:
            model[key] = embedded_model[key]
    model['__embedded__'] = embedded
    model['__view__'] = 'manage'
    return model