Пример #1
0
    def configure(self, cfg):
        """ Configuration and initialization is delayed to allow working
            with CherryPy configuration API
        """
        self.config = cfg
        create_tables(get_db_connection(self.config))

        self.worker_set = WorkerSet(cfg)
        for w in Watch.load_all(get_db_connection(self.config)):
            self.worker_set.add(w.id)
Пример #2
0
class DashboardController(Controller):
    """ Web application interface """
    def __init__(self):
        self.worker_set = None
        self.config = None

    def finish(self):
        """ Call this to clean up when the application is shut down """
        if self.worker_set:
            self.worker_set.finish()

    def configure(self, cfg):
        """ Configuration and initialization is delayed to allow working
            with CherryPy configuration API
        """
        self.config = cfg
        create_tables(get_db_connection(self.config))

        self.worker_set = WorkerSet(cfg)
        for w in Watch.load_all(get_db_connection(self.config)):
            self.worker_set.add(w.id)

    @cherrypy.expose
    def index(self):
        watches = Watch.load_all(get_db_connection(self.config))
        return self.render('index.html', watches=watches, worker_set=self.worker_set)

    @cherrypy.expose
    def status(self):
        watches = Watch.load_all(get_db_connection(self.config))

        data = {}
        for watch in watches:
            data[str(watch.id)] = watch.dict()
            data[str(watch.id)].update(self.worker_set.worker_status_dict(watch.id))

        cherrypy.response.headers['Content-Type'] = "application/json"
        return simplejson.dumps(data)

    @cherrypy.expose
    def new(self, **kwargs):
        watch = None
        if cherrypy.request.method == 'POST':
            c = get_db_connection(self.config)
            try:
                valid_dict, errors = validate_twill_form(c, kwargs)
                if len(errors):
                    c.rollback()
                else:
                    watch = Watch(**valid_dict)
                    watch.save(c)
                    c.commit()
                    self.worker_set.add(watch.id)
            except Exception:
                c.rollback()
                raise
        else:
            errors = []

        if watch:
            raise cherrypy.HTTPRedirect(cherrypy.url('/'), status=303)
        else:
            return self.render('new.html', data=kwargs, errors=errors)


    def __getattr__(self, name):
        try:
            id = int(name)
        except ValueError:
            raise AttributeError

        return WatchController(id, self.config, self.worker_set)