Hi,

I'm working on an a little newforms problem and can surely use some
advice.

I have an application process that is broken up into several forms.
So I have one Application model class and several AppFormParts 1-8.
Since the application process is so painfully long we decided to break
it up into parts (smaller forms).  We also allow people to create an
application and continue it/update it later.

The only common field among all the AppFormParts is the workshops
field. It is hidden field on all the AppForms except the first, where
it gets set for the rest of the process.

Here are the components rough sketch:

"""
Forms
"""
class AppForm(forms.Form):

    workshops = forms.MultipleChoiceField(
        widget = widgets.CheckboxSelectMultiple(),
        choices = [ (w.id,w) for w in
Workshop.objects.get_not_past_deadline() ],
        help_text = 'Apply for any or all of the workshops by checking
the boxes. We encourage people to check all workshops to increase
their chances of being accepted into one of them.',
    )

    def save(self):
        # save an application for each workshop
        workshops = self.cleaned_data['workshops']
        # remove these so we can loop through all fields
        del(self.cleaned_data['workshops'])
        del(self.cleaned_data['workshop'])
        for w in workshops:
            try:
                # look for existing app
                app = Application.objects.get(
                          #needs a workshop object
                          workshop=Workshop.objects.get(pk=w),
                          #needs a user object
                          user=self.cleaned_data['user'],
                      )
            except Application.DoesNotExist, Workshop.DoesNotExist:
                # start new app
                app = Application(
                        workshop=Workshop.objects.get(pk=w),
                        user=self.cleaned_data['user']
                      )
            # loop through all keys from the form and map to object
            for key,val in self.cleaned_data:
                if hasattr(app,key):
                    setattr(app,key,val)
            obj.save()

class AppFormPart1(AppForm):

    """
    Personal information section of the application.
    This is the minimum requirement for an application to exist.
    Field names correspond to Application model.
    """

    title = forms.CharField(
    )
    organization = forms.CharField(
    )
    email_2 = forms.CharField(
    )
    phone = forms.CharField(
        max_length=12,
    )
    [snip]

class AppFormPart2(AppForm):

    def __init__(self):
        self.workshops.widget = widgets.MultipleHiddenInput()

   ..... more fields

"""
view
"""

@login_required
def app_create_update(request, part=1):
    from myproject.workshops import forms as myforms

    next_part= part+ 1

    if request.method == 'POST':

        new_data = request.POST.copy()

        # get form dynamically according to part param
        Form = getattr(myforms,'AppFormPart%s' % part)
        form = Form(new_data)

        if form.is_valid():
            form.cleaned_data['user'] = request.user
            form.save()
            # form validates, set the response message
            request.user.message_set.create(
                message=Application.USER_MSGS['saved'],
            )
            # redirect to next part
            return HttpResponseRedirect( '/training/apply/%s/' %
next_part)
     else :
       [snip]

Please let me know if there's a better way to go about this.  Or where
the major problems lie.

Thanks,

--
Milan


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to