TurboGears

Custom dispatcher in CherryPy

Despite my previous success in using Routes with CherryPy (partially at least… I never could get URL generation to work properly), I’ve been looking into nice, easy, clean ways to use the default method as a custom dispatcher for a given controller.

For example, the app I’m currently working on is a simple ticket system to handle support requests from clients. I want to be able to access tickets in a semi-RESTful manner:

/ticket/[ticket id]/[action]

This might have been made easier on myself if I’d gone for the URL structure below as all of CherryPy’s methods now allow for positional parameters.

/ticket/[action]/[ticket id]

However, that structure doesn’t work logically in my head and I’m a stickler for detail.

Continue Reading »

IT
TurboGears

Comments (1)

Permalink

Arkivo v0.2

Arkivo has reached the heady heights of version 0.2! This version incorporates a complete rewrite of the IRC bot to allow it to connect to multiple servers. This means Arkivobot is no longer restricted to Freenode, but can roam the networks freely as is a bot’s want. Arkivobot is currently connected to Freenode and OFTC, but if you want your channel archived and you’re on another network I can easily accomodate you (email me for more details)

Because of the addition of multiple networks I had to change the URL structure. For example, to access the archive for #turbogears:

Old URL: http://irclog.turbogears.org/archive/turbogears/
New URL: http://irclog.turbogears.org/archive/freenode/turbogears/

This will prevent URL clashes in case a channel has the same name on two different networks. Arkivo is nice and freindly though; if you use one of the old URL’s you will be redirected and asked to update your bookmarks.

Also included in this version is a visible bot status and a bugfix to prevent the bot dying if it encounters encoding that isn’t supported by the channel.

TurboGears
Web Dev

Comments (4)

Permalink

Arkivo launched

I launched Arkivo earlier today, in its first incarnation at least. It was written originally to archive the happenings in #turbogears on Freenode but there is already another channel using the service, #foresight - the channel for the Foresight Linux distribution. Any other channels on Freenode that need archiving are more than welcome to be added to the list. Email me to have the bot join your channel.

Happily, everything seems to be ticking over nicely. I still have quite a few features planned including searchable archives, direct linking to individual messages (obviously in the flow of the conversation, so it’s in context), automatic creation of clickable links (which is harder than it sounds when using Kid) and a few other bits and pieces that I’m going to keep under my hat in case they don’t actually see the light of day.

Hopefully I’ll have a Trac/SVN instance up soon, subject to server load. Failing that I may take a look around for a hosted option, I’ll just have to see how it pans out after a few days of arkivo being up along with the other sites I’m hosting.

TurboGears

Comments Off

Permalink

Idenitity using email addresses

I’m a picky programmer and I like to have things “just so”. I’m creating an app using Turbogears and it’s Identity framework, but I wanted to have people log on using their email address rather than an arbitrary username. I didn’t fancy messing around with the SOProvider myself, nor did I want to just stuff an email address into a field called username in my database (I did mention I was picky, right?).

The solution is simple, but I thought I’d share this anyway as the theory behind it is fairly powerful.

class User(SQLObject):
        email_address = UnicodeCol( length=255, alternateID=True,
                               alternateMethodName="by_email_address" )
        ....
        def _get_user_name(self):
                return self.email_address

        def _set_user_name(self, new):
                self.email_address = new

        @classmethod
        def by_user_name(self, text):
                return self.by_email_address(text)

Now, as I said, this is pretty simple at first glance but it tricks SOProvider (or anything else that accesses the user_name property) into thinking that the user_name is a real item in the database. In effect, we are giving email_address an “alias” of user_name.

You can also use this method to provide some form of action on data before it hits your database:

class Car(SQLObject):
        manufacturer = ForeignKey("Manufacturer")
        model = UnicodeCol(length=50)
        wheels = IntCol()

        def _set_manufacturer(self, name):
                try:
                       mf = Manufacturer.byName(name)
                except SQLObjectNotFound:
                        mf = Manufacturer(name=name)

                self._SO_set_manufacturer_id = mf.id

class Manufacturer(SQLObject):
        name = UnicodeCol(alternateID=True)

I wrote this code on the blog, so it’s not tested in any way, but you get the idea. You’ll be able to create manufacturers on the fly when creating or updating a Car if they’re not already available. Neat, huh?

TurboGears

Comments (4)

Permalink

MochiKit Effects

Thomas Hervé recently posted the answer to one of my lesser prayers to the MochiKit mailing list last week: A port of the scriptaculous effects and drag & drop libraries to use Mochikit.

Basically this frees you from the extra overhead of both the scriptaculous and prototype .js files. I was using scriptaculous for the sole purpose of creating effects. Mochikit has always done all the real work as I understand it a lot better than any other library. The best thing about this port is that, aside from the different script tags, the code for creating and using the effects doesn’t have to be changed at all! It’s literally a drop-in replacement.

This adds to mochikit the one thing that was missing and has made me a lot happier in using it as I don’t have to worry about the bandwidth being wasted on some hefty js files just to make things slide and fade out.

Kudos to Thomas, and many thanks.

TurboGears
Web Dev

Comments (2)

Permalink

Fancy status messages using tg_flash

I like using ajaxy interfaces in my web apps, and as such I also like the type of status messages that you can achieve using Mochikit. However, sometimes you need to pass a status message between pages (when you’re using raise cherrypy.HTTPRedirect() for example) and I just don’t like having two different ways to display things.

To dissolve my perfectionist worries that the standard tg_flash message in a div just doesn’t cut it, I looked at ways of making the message look the same as a status message from my ajax calls.

Updated on 24/11/05 as per Bob’s suggestions.
Updated on 26/11/05 to fix a JavaScript bug in Internet Explorer
Updated on 16/12/05 to fix some typos as pointed out by Tim
Updated on 12/01/06 to reflect changes to the TurboGears jsonify API
Continue Reading »

TurboGears

Comments (9)

Permalink