Changes between Initial Version and Version 1 of CookBookManipulatorCustomManipulator


Ignore:
Timestamp:
Jan 18, 2006, 7:11:44 PM (19 years ago)
Author:
brantley
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookManipulatorCustomManipulator

    v1 v1  
     1Using Manipulators can be annoying and confusing, use this to create simple, elegant ones:
     2
     3{{{
     4from django.core import formfields, validators
     5
     6class Manipulator(formfields.Manipulator):
     7    default = {}
     8    done = False
     9   
     10    def getData(self, request):
     11        return request.POST
     12   
     13    def getForm(self, data, errors):
     14        return formfields.FormWrapper(self, data, errors)
     15   
     16    def process(self, request):
     17        data = self.getData(request)
     18        if data:
     19            new_data = data.copy()
     20            errors = self.get_validation_errors(new_data)
     21            if not errors:
     22                self.do_html2python(new_data)
     23               
     24                self.done = True
     25                return self.complete(request, new_data)               
     26        else:
     27            errors = {}
     28            new_data = self.default
     29        self.form = self.getForm(new_data, errors)
     30        return self.form
     31
     32    def complete(self, request, data):
     33        pass
     34}}}
     35
     36Now to use, subclass it, and fill in the necessary methods ("__init__" and "complete"), also give it the fields:
     37
     38{{{
     39class PollEdit(Manipulator):
     40    fields = (
     41        formfields.TextField("title", maxlength=32, is_required=True),
     42        formfields.TextField("question", maxlength=128, is_required=True),
     43    )
     44       
     45    def __init__(self, poll):
     46        # You can also define your fields here, if you'd like.
     47        self.poll = poll
     48       
     49    def complete(self, request, data):
     50        self.poll.title = data['title']
     51        self.poll.question = data['question']
     52        self.poll.save()
     53
     54}}}
     55
     56Usage of the final Manipulator is quite simple:
     57
     58{{{
     59def edit_poll(request, id):
     60    poll = polls.get_object(pk=id)      #Assume it works
     61    manipulator = PollEdit(poll)
     62    form = manipulator.process(request)    #Process returns a form if it's needed
     63    if (form):
     64         return render_to_response('polls/edit_form', {'form': form})
     65    else:
     66         return HttpResponseRedirect("/polls/" % poll.id)
     67}}}
     68
     69Here's a custom Add Manipulator:
     70
     71{{{
     72class PollAdd(Manipulator):
     73    fields = (
     74        formfields.TextField("title", maxlength=32, is_required=True),
     75        formfields.TextField("question", maxlength=128, is_required=True),
     76    )
     77       
     78    def __init__(self):
     79        self.default = {'title': "Unnamed", 'question': 'Insert your question here.'}
     80        #This sets your default values.
     81   
     82    def complete(self, request, data):
     83        poll = polls.Poll(title=data['title'], question=data['question'])
     84        poll.save()
     85        return poll
     86
     87}}}
     88
     89And here's the add usage:
     90
     91{{{
     92def add_poll(request):
     93    manipulator = PollAdd()
     94    result = manipulator.process(request)
     95    if (manipulator.done): #Are we done processing?
     96         return HttpResponseRedirect("/polls/" % result.id)
     97    else: #Otherwise, place the form.
     98         return render_to_response('polls/new_form', {'form': manipulator.form})
     99}}}
     100
Back to Top