I really do not like the way TG2.1 Admin System force developer to use plural name. Because in my language we do not use the same grammar. Totally different, so it's a terrible idea to put 's's on every model/table name.
To solve this issue, there are two major area that we need to change. The first one is the genshi template, and the other one is the class that is responsible to parse the model/table REST urls.
Fortunately the issue resides on one class tgext.crud.controller.CrudRestController. There are some methods which use genshi templates located in tgext.admin.templates. So, you might want to copy the templates and make appropriate changes. Done with one area.
Next is to change the way CrudRestController uses the template and perform lookup for the model/table REST urls. The way to change the template is to change the value of @expose decorator.
Another thing to do is to override _lookup method.
Inside, you can see code like the following:
model_name = model_name[:-1]
The code above assumes that the model/table REST url is in plurals. No good. Don't like it at all. Simply remove that particular line of code and we're done.
Showing posts with label tg2. Show all posts
Showing posts with label tg2. Show all posts
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.
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.
Friday, April 8, 2011
TurboGears 2 Editable Primary Key Field
TG2.1, nice web app framework. I just with it has better documentations. Compare to django, TG2.1 documentations are way behind. But, still it is much more flexible.
I encountered problem that TG2 Admin Page, created with TG Administration System, automatically set the (html) input field of the model's PK to disabled. Sprox assumes that your model's PK is auto-generated.
Some one already reported the issue here
and then created the proper tickets to sprox here
Got a good clue where to check, so after taking a look inside sprox.formbase source code, below is the reason why PK fields are non-editable.
class EditableForm(FormBase):
"..."
def _do_get_disabled_fields(self): fields = self.disable_fields[:]
fields.append(self.provider.get_primary_field(self.entity))
# automatically set PK field disabled return fields
same goes with: class AddRecordForm(FormBase)
I wrote it in here.
Hopefully useful for those who need it.
I encountered problem that TG2 Admin Page, created with TG Administration System, automatically set the (html) input field of the model's PK to disabled. Sprox assumes that your model's PK is auto-generated.
Some one already reported the issue here
and then created the proper tickets to sprox here
Got a good clue where to check, so after taking a look inside sprox.formbase source code, below is the reason why PK fields are non-editable.
class EditableForm(FormBase):
"..."
def _do_get_disabled_fields(self): fields = self.disable_fields[:]
fields.append(self.provider.get_primary_field(self.entity))
# automatically set PK field disabled return fields
same goes with: class AddRecordForm(FormBase)
I wrote it in here.
Hopefully useful for those who need it.
Subscribe to:
Posts (Atom)