newforms-admin conversion utility

0 views
Skip to first unread message

nliberg

unread,
Feb 12, 2008, 8:53:27 AM2/12/08
to Satchmo developers
Hi everyone,
This is my first post on the satchmo-developers list.

I wrote a little utility script today that takes a models.py file as
input and outputs the corresponding newforms-admin class declarations
to stdout. It does this by scanning the source code of the model. It
was just a quick hack, so the code is kind of ugly and it is assumed
that an indentation width of four spaces is used in model files, but
it seems to work nicely. The output can be saved as an admin.py file
that can be imported from __init__.py (this convention is proposed in
the documentation for the newforms-admin branch).

Here is what this little tool does:
* Outputs an import line that imports only the needed classes from the
model.
* Generates inline editing classes - inheriting from either
admin.TabularInline or admin.StackedInline
* Generates admin classes and adds a field called 'inlines' linking
them to the inline classes.
* Detects prepopulate_from and outputs the corresponding
prepopulated_fields in the admin class.
* Detects filter_interface and outputs the corresponding
filter_horizontal / filter_vertical in the admin class.
* Replaces 'fields' by 'fieldsets'
* Replaces 'classes': 'collapse' by 'classes': ('collapse',)
* Outputs invokations of admin.site.register for all generated admin
classes.

Usage:
python newforms_gen.py models.py module-name > admin.py

Example:
python newforms_gen.py product/models.py satchmo.product > product/
admin.py

The newforms_gen.py script is available here:
http://nilsliberg.se/python/django/newforms_gen.html

Please note that the script does not modify anything, it only prints
the generated source to stdout. The script does not yet handle
raw_id_admin fields, but I don't think they are used in Satchmo
anyway.

I got many parts of Satchmo working with the newforms-admin branch
using purely auto-generated admin.py files imported from __init__.py
plus a few changes to urls.py. I guess some of Satchmo's admin
customizations might need to be handled manually though.

- Nils

Chris Moffitt

unread,
Feb 12, 2008, 5:33:17 PM2/12/08
to satchmo-d...@googlegroups.com
Very cool. We'll need to take a look at this when we start the migration. I'd suggest you post this to the broader Django community to get their feedback. This should be very useful for folks outside of Satchmo too.

-Chris
Reply all
Reply to author
Forward
0 new messages