Development/Validators

Validators

by: ctrochalakis

Validator Breakage

Currently validators are broken in transifex. This is mainly because, to produce nicer urls, we have bypassed the default dispatch algorithm.

Turbogears dispatches requests to controller methods using this url pattern: http://transifex/<controller>/<method>/<argument-1>/<argument-2>

file: <controller>.py

class Controller:
    def <method>(self,<arg-1>, <arg-2>):
        code..code..code

So the "turbogears way" to access the submit method for example is via http://transifex/module/submit/foo-module/.

In order to change the above url schema and use http://transifex/module/foo-module/submit/ we do a small hack:

There is a default controller method that all requests are rooted if no method is matched, this is the default method. What we do is we wait the "http://transifex/module/foo-module/submit/" request to hit that default method (as there is no foo-module function) and we forward the request to the submit method.

The really bad thing is that when we forward the requests to the methods the associated validators are bypassed!

Proposed Solutions

  • Use the turbogears url schema.
  • Find a way to enable validators (This might be difficult)
  • Use the routes package that enables more fine grained url schemas.

General

Taking the above into consideration, in order to access transifex with validators enabled is to use the turbogears url schema.

Validators are not there just to validate data, but also to transform data to python objects and vice versa. A module Validator for example takes the module name as a string, validates that a module with that name exists and returns that module object. Validators are there to perform all kind of input-data checks, we can use a validator to see if the combination of the module and branch supplied is valid, we can even use a validator to msgfmt check an uploaded po file.

This way the controller methods body becomes more robust as we don't have to deal with input data checks. Also the validation code is in one place, it's reusable and easier to test.

Coding

I have created a tx-validators repository where a wrote a few validators transifex/validators.py.

As an example I have altered the info method to use the new validators (info is about 30% smaller now), you can test it by hitting these urls:

Note that these urls use the default turbogears schema (for the validators to work). I have not changed the templates so the urls produced there use the transifex schema and will not work.