views:

188

answers:

2

I have this code in my forms.py:

from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

When I try to extract the code programmatically with inspect it leaves out fieldsets:

In [1]: import inspect

In [2]: import forms

In [3]: print inspect.getsource(forms)
from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

In [4]: print inspect.getsource(forms.ContactForm)
class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)


In [5]:

This doesn't seem to be an issue with blank lines. I've tested without the blank line in between and I've put additional blank lines in between other attributes. Results don't change.

Any ideas why inspect is returning only the part before fieldsets and not the whole source of the class?

+1  A: 

edit: revised based on comments:

Inside inspect.getsource(forms.ContactForm) the method BlockFinder.tokeneater() is used to determine where the ContactForm block stops. Besides others, it checks for tokenize.DEDENT, which it finds right before fieldsets in your version stored at github. The line contains only a line break, so inspect thinks the current block has ended.

If you insert 4 spaces, it works for me again. I cannot argue on the rationale behind this, maybe performance.

class ContactForm(forms.Form):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)
    # <-- insert 4 spaces here
    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

The reason that inspect.getsource(forms) works differently is because inspect in that case does not have to determine the class definition's start and end. It simply outputs the whole file.

wr
No. I wasn't reload()'ing. I also removed FieldsetMixin now and tries again, still no `fielsets` in source output.
muhuk
Which Python version? Mine was 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
wr
Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32) on Debian Lenny
muhuk
With my 2.5.4 version the bug still does not show up. Can you provide the py-files somewhere as a link to download? Maybe some strange characters, which have not made it into the text above?
wr
Another thing: You have named your file forms.py. This gives a name clash with the django forms module.
wr
@wr: you can find the source here http://github.com/muhuk/django-formfieldset/tree/master. It's in example dir. It is a convention for Django applications to name your file forms.py where you have forms.
muhuk
A little disturbing, but yes adding spaces fixes the problem.
muhuk
A: 

Works for me. I don't have "from formfieldset.forms import FieldsetMixin" in my code. Maybe that is causing an issue..

Chirayu Patel