Code


Version 3 (modified by gary.wilson@…, 8 years ago) (diff)

Added Python procossor to code blocks. Added curly braces around __init__.

Using Manipulators can be annoying and confusing, use this to create simple, elegant ones:

from django.core import formfields, validators

class Manipulator(formfields.Manipulator):
    default = {}
    done = False
    
    def getData(self, request):
        return request.POST
    
    def getForm(self, data, errors):
        return formfields.FormWrapper(self, data, errors)
    
    def process(self, request):
        data = self.getData(request)
        if data:
            new_data = data.copy()
            errors = self.get_validation_errors(new_data)
            if not errors:
                self.do_html2python(new_data)
                
                self.done = True
                return self.complete(request, new_data)                
        else:
            errors = {}
            new_data = self.default
        self.form = self.getForm(new_data, errors)
        return self.form

    def complete(self, request, data):
        pass

Now to use, subclass it, and fill in the necessary methods ("__init__" and "complete"), also give it the fields:

class PollEdit(Manipulator):
    fields = (
        formfields.TextField("title", maxlength=32, is_required=True),
        formfields.TextField("question", maxlength=128, is_required=True),
    )
        
    def __init__(self, poll):
        # You can also define your fields here, if you'd like.
        self.poll = poll
        
    def complete(self, request, data):
        self.poll.title = data['title']
        self.poll.question = data['question']
        self.poll.save()

Usage of the final Manipulator is quite simple:

def edit_poll(request, id):
    poll = polls.get_object(pk=id)      #Assume it works
    manipulator = PollEdit(poll)
    form = manipulator.process(request)    #Process returns a form if it's needed
    if (form):
         return render_to_response('polls/edit_form', {'form': form})
    else:
         return HttpResponseRedirect("/polls/" % poll.id)

Here's a custom Add Manipulator, I like to build off of the first, so that the fields are the same:

class PollAdd(PollEdit):        
    def __init__(self):
        self.default = {'title': "Unnamed", 'question': 'Insert your question here.'}
        #This sets your default values.
    
    def complete(self, request, data):
        poll = polls.Poll(title=data['title'], question=data['question'])
        poll.save()
        return poll

And here's the add usage:

def add_poll(request):
    manipulator = PollAdd()
    result = manipulator.process(request)
    if (manipulator.done): #Are we done processing?
         return HttpResponseRedirect("/polls/" % result.id)
    else: #Otherwise, place the form.
         return render_to_response('polls/new_form', {'form': manipulator.form})

Attachments (2)

Download all attachments as: .zip