Monday, April 11, 2011

TurboGears 2.1 Admin System Change Date Format and Other Values

I have been working a lot with TG2.1 Admin System lately. If you have worked with django, you know that there is automatic admin page generation. In TG2.1 this functionality is based on tgext.admin, tgext.crud, and sprox.

For those you already enjoying TG2.1 Admin System, perhaps you realized that the date format in the admin system is fixed to m-d-Y format. What if you want to change the format? Unfortunately the format is hardcoded inside the TableFiller. Its exact location is sprox.fillerbas.TableFiller.
When you checked the source code of that particular class, you will see that the date format is hardcoded in get_value(self, values=None, **kw) method. So, what you need to do is subclass TableFiller and override get_value method.
Now, after changing the date format, then I needed to format some numbers with grouping. So, I added some more logic to check if the data in 'value' is int/float/Decimal and format it properly.

Next step is to understand that TableFiller can be used in CrudRestControllerConfig (tgext.admin.config.CrudRestControllerConfig), under variable 'table_filler_type'. So, write the appropriate code to set TableFiller inside CrudRestControllerConfig.

CrudRestControllerConfig itself can be used in tgext.admin.tgadminconfig.TGAdminConfig / tgext.admin.config.AdminConfig.
So, put the CrudRestControllerConfig inside *AdminConfig, with the appropriate dbfield variable.

AdminConfig itself can be used as config_type parameter for tgext.admin.controller.AdminController.

Now, everything is set and ready to use.

I want to give you a little hint, that AdminController's _lookup method assumes that all your model's REST url is in plural format. If you don't like or need it, you need to do something about it. I will write about this in the next post.

In my opinion, spending time understanding the layer of TG2.1 framework is really worth it. There are many things that already handled by TG2.1 (plus the plugins/extensions), and if there is not enough, simply extend it by subclass the necessary class. The framework is well designed. The only drawback is the lack (I mean really lacking complete, integrated) documentations.

No comments:

Post a Comment