Django

Code

Manipulator Script

The attached script will produce a custom manipulator for the specified model. Useful if you've got a complex model which requires a slight change to the manipulator.

This is an interesting script to combine with ScaffoldScript

The new version will turn this model:

class Category(meta.Model):
    category = meta.CharField(maxlength=50, unique=True)
    createdOn = meta.DateField(auto_now_add=True)
    modifiedOn = meta.DateField(auto_now=True)
    test = meta.ManyToManyField(auth.User, verbose_name='test', related_name='test')
    test2 = meta.OneToOneField(auth.User, verbose_name='test2', related_name='test2')

    def __repr__(self):
        return "%s" % (self.category,)

    class META:
        admin = meta.Admin()

into this (using $ ./manipulator.py -a ToDo -m Category --name CustomToDo

class CustomCatManipulator(formfields.Manipulator):
    def __init__(self, pk=None):
        self.fields = (
            formfields.TextField(field_name='category', is_required=True, maxlength=50),
            formfields.DateField(field_name='createdOn'),
            formfields.DateField(field_name='modifiedOn'),
            formfields.OneToOneField(field_name='test2', is_required=True),
            formfields.SelectMultipleField(field_name='test', is_required=True, choices=[(o.id, o) for o in tests.get_list()]),
        )

    def save(self, new_data):
        temp = Category(
            category=new_data['category'],
            createdOn=new_data['createdOn'],
            modifiedOn=new_data['modifiedOn'],
            test2=new_data['test2']
        )
        temp.set_test(newdata['test'])
        temp.save()
        return temp
Example: {{{ $ python manipulator.py -a ToDo -m Category class ToDoCategoryManipulator(formfields.Manipulator): def __init__(self): self.fields = ( formfields.CharField(field_name='category', is_required=True, unique=True, verbose_name='category', maxlength=50) formfields.DateField(field_name='createdOn', verbose_name='createdOn') formfields.DateField(field_name='modifiedOn', verbose_name='modifiedOn') ) }}} The new version will turn this model: {{{ class Category(meta.Model): category = meta.CharField(maxlength=50, unique=True) createdOn = meta.DateField(auto_now_add=True) modifiedOn = meta.DateField(auto_now=True) test = meta.ManyToManyField(auth.User, verbose_name='test', related_name='test') test2 = meta.OneToOneField(auth.User, verbose_name='test2', related_name='test2') def __repr__(self): return "%s" % (self.category,) class META: admin = meta.Admin() }}} into this (using {{{$ ./manipulator.py -a ToDo -m Category --name CustomToDo}}} {{{ class CustomToDoManipulator(formfields.Manipulator): def __init__(self, pk=None): self.fields = ( formfields.CharField(field_name='category', is_required=True, unique=True, verbose_name='category', maxlength=50) formfields.DateField(field_name='createdOn', verbose_name='createdOn') formfields.DateField(field_name='modifiedOn', verbose_name='modifiedOn') formfields.OneToOneField(field_name='test2', is_required=True, verbose_name='test2') formfields.ManyToManyField(field_name='test', is_required=True, verbose_name='test') ) def save(self, new_data): temp = Category( category=new_data['category'], createdOn=new_data['createdOn'], modifiedOn=new_data['modifiedOn'], test2=new_data['test2'] ) temp.set_test(newdata['test']) temp.save() return temp }}}

Attachments