web.admin

Package web :: Module admin |
[hide private]
[frames] | no frames]

# Source Code for Module web.admin

      1  #!/usr/bin/env python
      2
      3  import web
      4  import gettext
      5  import db
      6  import config
      7  import datetime
      8  from pluginmanager.pluginmanager import PluginManager
      9  from log import Log
     10
     11  urls = (
     12      '/navigator',                               'navigator.navigator.Navigator',
     13      '/navigator/(.*)',                          'navigator.navigator.Navigator',
     14      '/plugin/(.*)',                             'pluginmanager.pluginview.PluginView',
     15      '/pluginmanager',                           'pluginmanager.listview.ListView',
     16      '/pluginmanager/enable/(\w+)',              'pluginmanager.enableview.EnableView',
     17      '/pluginmanager/disable/(\w+)',             'pluginmanager.disableview.DisableView',
     18      '/pluginmanager/uninstall/(\w+)',           'pluginmanager.uninstallview.UninstallView',
     19      '/panel',                                   'panel.panel.Panel',
     20      '/user/register',                           'user.createuserview.CreateUserView',
     21      '/user/login',                              'user.login.LoginView',
     22      '/user/logout',                             'user.login.LogoutView',
     23      '/user/confirmemail/(\w+)/(.*)',            'user.login.ConfirmEmailView',
     24      '/user/recoverpassword',                    'user.login.RecoverPasswordView',
     25      '/user/recoverpassword/(\w+)/(.*)',         'user.login.ChangePasswordView',
     26      '/user/editdetails',                        'user.login.EditDetailsView',
     27      #'/user/view/(.*)',                         'user.login.UserView',
     28      #'/user/recoverpassword'                    'user.recoverpasswordview.RecoverPasswordView',
     29      '/options',                                 'options.options.Options',
     30      '/dbadmin$',                                'dbadmin.dbadmin.Admin',
     31      '/dbadmin/(?P<model>\w+)$',                 'dbadmin.dbadmin.View',
     32      '/dbadmin/(?P<model>\w+)/add$',             'dbadmin.dbadmin.Add',
     33      '/dbadmin/(?P<model>\w+)/edit/(?P<id>.*)$', 'dbadmin.dbadmin.Edit',
     34      '/dbadmin/(?P<model>\w+)/del/(?P<id>.*)$',  'dbadmin.dbadmin.Del',
     35      '/jobs',                                    'clientjob.jobview.ClientJobView',
     36      '.*',                                       'navigator.navigator.Navigator',
     37  )
     38
     39
     40  pmanager = PluginManager()
     41  app = web.application(urls, globals())
     42
     43 -class Index:
     44 -    def GET(self):
     45          return web.ctx.render.index([])
     46
     47
     48  # session with reloader
     49  if web.config.get('_session') is None:
     50      store = web.session.DiskStore('sessions')
     51      session = web.session.Session(app, store)
     52      web.config._session = session
     53  else:
     54      session = web.config._session
     55
     56 -def session_hook():
     57      web.ctx.session = session
     58      lang = web.input().get('lang', '')
     59      if lang:
     60          session.lang = lang
     61      elif session.get('lang', ''):
     62          lang = session.lang
     63      else:
     64          langs = map(str.strip,
     65                  web.ctx.environ.get('HTTP_ACCEPT_LANGUAGE',
     66                      'en').split(','))
     67          lang = [i for i in langs if i in config.langs]
     68          lang = lang[0] if lang else config.langs[0]
     69      if not lang in config.langs:
     70          lang = config.langs[0]
     71      session.lang = lang
     72
     73      web.ctx.gettext = get_gettext(lang)
     74      # templates
     75      context = {'static': '/static',
     76                 'org': 'opengnsys',
     77                 '_': web.ctx.gettext,
     78                 'storage': web.storage,
     79                 'getattr': getattr,
     80                 'session': session,
     81                 'get_notifications' : get_notifications}
     82      normal_render = web.template.render('templates', globals=context)
     83      context['render'] = normal_render
     84      render = web.template.render('templates', base='layout',
     85                              globals=context)
     86      web.ctx.render = render
     87      web.ctx.normal_render = normal_render
     88      web.ctx.notify = add_notification
     89      web.ctx.plugin_manager = pmanager
     90
     91
     92  # Sqlalchemy processor
     93 -def load_sqla(handler):
     94      web.ctx.orm = db.connect()
     95      try:
     96          to_ret = handler()
     97      except web.HTTPError:
     98          web.ctx.orm.commit()
     99          raise
    100      except:
    101          web.ctx.orm.rollback()
    102          raise
    103      web.ctx.orm.commit()
    104      return to_ret
    105
    106 -def get_notifications():
    107      return web.ctx.session.pop('notifications', [])
    108
    109 -def add_notification(text, type_var):
    110      new_notification = web.storage(text=text, type=type_var, date=datetime.datetime.now())
    111      notifications = web.ctx.session.get('notifications', [])
    112      notifications.append(new_notification)
    113      web.ctx.session['notifications'] = notifications
    114
    115  gettext.install('messages', config.localedir, unicode=True)
    116 -def get_gettext(lang='es'):
    117      gtx = gettext.translation('messages',
    118                                 config.localedir,
    119                                 languages=[lang])
    120      return gtx.gettext
    121
    122  app.add_processor(load_sqla)
    123  app.add_processor(web.loadhook(session_hook))
    124
    125  if __name__ == '__main__':
    126      #app.run(Log)
    127      app.run()
    128