def test_deleting(self): w1 = Watch('codesprinters', 10, "go codesprinters") w1.save(self.connection) w2 = Watch('google', 10, "go google") w2.save(self.connection) w2.delete(self.connection) w1loaded = Watch.load(w1.id, self.connection) w2loaded = Watch.load(w2.id, self.connection) self.compare_watches(w1, w1loaded) assert w2loaded is None
def test_load_all(self): w1 = Watch('www.google.com', 10, "go google") w1.save(self.connection) w2 = Watch('www.codesprinters.com', 10, "go codesprinters") w2.save(self.connection) w3 = Watch('www.squarewheel.pl', 10, "go google") w3.save(self.connection) watches = Watch.load_all(self.connection) assert_equal(3, len(watches)) self.compare_watches(w2, watches[0]) self.compare_watches(w1, watches[1]) self.compare_watches(w3, watches[2])
def edit(self, **kwargs): connection = get_db_connection(self.config) try: watch = Watch.load(self.id, connection) if not watch: raise cherrypy.NotFound() if cherrypy.request.method == 'POST': valid_dict, errors = validate_twill_form(connection, kwargs, watch) if len(errors): connection.rollback() else: for k,v in valid_dict.iteritems(): setattr(watch, k, v) watch.update(connection) connection.commit() self.worker_set.restart(self.id) raise cherrypy.HTTPRedirect(cherrypy.url('/'), status=303) else: errors = [] return self.render('edit.html', watch=watch, data=kwargs, errors=errors) except Exception: connection.rollback() raise
def restart(self): watch = Watch.load(self.id, get_db_connection(self.config)) if not watch: raise cherrypy.NotFound() self.worker_set.restart(self.id) raise cherrypy.HTTPRedirect(cherrypy.url('/'), status=303)
def check_now(self): """ Force a check immediately """ watch = Watch.load(self.id, get_db_connection(self.config)) if not watch: raise cherrypy.NotFound() self.worker_set.check_now(self.id) raise cherrypy.HTTPRedirect(cherrypy.url('/'), status=303)
def test_load_by_name(self): w1 = Watch('codesprinters', 10, "go codesprinters") w1.save(self.connection) w2 = Watch('google', 10, "go google") w2.save(self.connection) w2loaded = Watch.load_by_name('google', self.connection) self.compare_watches(w2, w2loaded) w1loaded = Watch.load_by_name('codesprinters', self.connection) self.compare_watches(w1, w1loaded)
def test_load_by_id(self): w1 = Watch('codesprinters', 10, "go codesprinters") w1.save(self.connection) w2 = Watch('google', 10, "go google") w2.save(self.connection) w2loaded = Watch.load(w2.id, self.connection) self.compare_watches(w2, w2loaded) w1loaded = Watch.load(w1.id, self.connection) self.compare_watches(w1, w1loaded) w3loaded = Watch.load(w1.id + w2.id, self.connection) assert w3loaded is None
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)
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)
def status(self): watch = Watch.load(self.id, get_db_connection(self.config)) if not watch: raise cherrypy.NotFound() data = watch.dict() data.update(self.worker_set.worker_status_dict(watch.id)) cherrypy.response.headers['Content-Type'] = "application/json" return simplejson.dumps(data)
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 validate_twill_form(connection, data, watch=None): """ Checks if data (dictionary) contains valid watch definition. Returns a tuple (valid_dict, list of errors), where valid_dict contains the successfully validated values """ # no need for any fancy form handling library for this single form valid_dict = {} errors = [] name = data.get('name', '') if not name: errors.append(u"Watch name is missing") else: other_watch = Watch.load_by_name(name, connection) if other_watch is not None and other_watch.id != watch.id: errors.append(u"Watch with this name already exists") else: valid_dict['name'] = name interval = data.get('interval', None) if not interval: errors.append(u"Interval is missing") else: try: valid_dict['interval'] = int(interval) except ValueError: errors.append(u"Invalid number (interval)") reminder_interval = data.get('reminder_interval', '').strip() if reminder_interval: try: valid_dict['reminder_interval'] = int(reminder_interval) except ValueError: errors.append(u"Invalid number (reminder interval)") else: valid_dict['reminder_interval'] = None script = data.get('script', None) if not script or script.isspace(): errors.append(u"Script is missing") else: valid_dict['script'] = script valid_dict['emails'] = data.get('emails', '') return valid_dict, errors
def delete(self): connection = get_db_connection(self.config) try: watch = Watch.load(self.id, connection) if not watch: raise cherrypy.NotFound() if cherrypy.request.method == 'POST': watch.delete(connection) self.worker_set.remove(self.id) connection.commit() raise cherrypy.HTTPRedirect(cherrypy.url('/'), status=303) return self.render('delete.html', watch=watch) except Exception: connection.rollback() raise
def test_updating(self): w1 = Watch('codesprinters', 10, "go codesprinters") w1.save(self.connection) w2 = Watch('google', 10, "go google") w2.save(self.connection) w1loaded = Watch.load(w1.id, self.connection) w2loaded = Watch.load(w2.id, self.connection) self.compare_watches(w1, w1loaded) self.compare_watches(w2, w2loaded) w1.script = "go http://codesprinters.com" w1.update(self.connection) w1loaded = Watch.load(w1.id, self.connection) w2loaded = Watch.load(w2.id, self.connection) self.compare_watches(w1, w1loaded) self.compare_watches(w2, w2loaded)
def index(self): watches = Watch.load_all(get_db_connection(self.config)) return self.render('index.html', watches=watches, worker_set=self.worker_set)