Can you change/redirect a django form's function by passing in your own function?

Posted by Derek on Stack Overflow See other posts from Stack Overflow or by Derek
Published on 2011-07-19T23:11:26Z Indexed on 2012/10/06 9:38 UTC
Read the original article Hit count: 261

I'm dealing with django-paypal and want to change the button src images. So I went the the conf.py file in the source and edited the src destination. However, I really want to leave the source alone, and I noticed that the

class PayPalPaymentsForm(forms.Form):

has

def get_image(self):
    return {
        (True, self.SUBSCRIBE): SUBSCRIPTION_SANDBOX_IMAGE,
        (True, self.BUY): SANDBOX_IMAGE,
        (True, self.DONATE): DONATION_SANDBOX_IMAGE,
        (False, self.SUBSCRIBE): SUBSCRIPTION_IMAGE,
        (False, self.BUY): IMAGE,
        (False, self.DONATE): DONATION_IMAGE,
    }[TEST, self.button_type]

which handles all the image src destinations. Since changing this def in the source is worse than changing conf, I was wondering if there was a way to pass in customized defs you make like passing in initial arguments in forms? This way no source code is changed, and I can customize the get_image def as much as I need.

passing in def something like this?

def get_image(self):
    ....
    ....
paypal = {
    'amount': 10,
    'item_name': 'test1',
    'item_number': 'test1_slug',

    # PayPal wants a unique invoice ID
    'invoice': str(uuid.uuid4()), 
}
form = PayPalPaymentsForm(initial=paypal, get_image)

Thanks!

© Stack Overflow or respective owner

Related posts about python

Related posts about django