web.dbadmin.dbadmin

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

# Source Code for Module web.dbadmin.dbadmin

      1  # -*- coding: utf-8 -*-
      2
      3  import web
      4  from user import login
      5  from user.model import User, Role, Action
      6  from main.model import OrganizativeUnit, Computer, Repo
      7  from formalchemy import FieldSet
      8
      9  BASE_URL = '/dbadmin'
     10
     11  MODELS = {
     12          # auth models
     13          'User': User,
     14          'Role': Role,
     15          'Action': Action,
     16
     17          # opengnsys main models
     18          'OrganizativeUnit': OrganizativeUnit,
     19          'Computer': Computer,
     20          'Repo': Repo,
     21          }
     22
     23
     24 -class notimpl:
     25 -    def GET(self, *args, **kwargs):
     26          return "not implemented"
     27
     28
     29 -class Admin:
     30      @login.authenticated
     31 -    def GET(self):
     32          return web.ctx.render.dbadmin.admin(MODELS, BASE_URL=BASE_URL)
     33
     34
     35 -class View:
     36      @login.authenticated
     37 -    def GET(self, model):
     38          dbmodel = MODELS.get(model, '')
     39          if not dbmodel:
     40              raise web.notfound()
     41          query = web.ctx.orm.query(dbmodel).all()
     42          return web.ctx.render.dbadmin.adminview(model, query,
     43                  dbmodel.fields(), BASE_URL=BASE_URL)
     44
     45
     46 -class Edit:
     47      @login.authenticated
     48 -    def GET(self, model, id):
     49          item = get_item(model, id)
     50          form = FieldSet(item)
     51          return web.ctx.render.dbadmin.adminedit("Editar", model, item,
     52                  form, BASE_URL=BASE_URL)
     53
     54      @login.authenticated
     55 -    def POST(self, model, id):
     56          item = get_item(model, id)
     57          dbmodel = MODELS.get(model, '')
     58          if not dbmodel:
     59              raise web.notfound()
     60          fieldset = FieldSet(dbmodel)
     61          field = fieldset.bind(item, data=web.input() or None)
     62          field.sync()
     63
     64          raise web.seeother(BASE_URL + '/%s' % model)
     65
     66
     67 -class Add:
     68      @login.authenticated
     69 -    def GET(self, model):
     70          dbmodel = MODELS.get(model, '')
     71          if not dbmodel:
     72              raise web.notfound()
     73          fieldset = FieldSet(dbmodel)
     74          item = dbmodel()
     75          form = fieldset.bind(item, None)
     76          form.session = session=web.ctx.orm
     77
     78          return web.ctx.render.dbadmin.adminedit(u"Añadir", model,
     79                  item, form, BASE_URL=BASE_URL)
     80
     81      @login.authenticated
     82 -    def POST(self, model):
     83          dbmodel = MODELS.get(model, '')
     84          if not dbmodel:
     85              raise web.notfound()
     86          orm = web.ctx.orm
     87          item = dbmodel()
     88          fieldset = FieldSet(dbmodel)
     89          item = dbmodel()
     90          form = fieldset.bind(item, data=web.input() or None)
     91          form.session = session=web.ctx.orm
     92          form.sync()
     93          orm.add(item)
     94
     95          raise web.seeother(BASE_URL + '/%s' % model)
     96
     97
     98 -class Del:
     99      @login.authenticated
    100 -    def GET(self, model, id):
    101          item = get_item(model, id)
    102          web.ctx.orm.delete(item)
    103          raise web.seeother(BASE_URL + '/%s' % model)
    104
    105
    106 -def get_item(model, id):
    107      dbmodel = MODELS.get(model, '')
    108      if not dbmodel:
    109          raise web.notfound()
    110      orm = web.ctx.orm
    111      try:
    112          item = orm.query(dbmodel).get(id)
    113      except:
    114          raise web.notfound()
    115      return item
    116