Example #1
0
    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
Example #2
0
    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])
Example #3
0
    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
Example #4
0
 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)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
    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
Example #8
0
    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)
Example #9
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)
Example #10
0
    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)
Example #11
0
    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)
Example #12
0
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
Example #13
0
    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
Example #14
0
    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)
Example #15
0
 def index(self):
     watches = Watch.load_all(get_db_connection(self.config))
     return self.render('index.html', watches=watches, worker_set=self.worker_set)