Changes between Version 7 and Version 8 of CookBookManipulatorCustomManipulator


Ignore:
Timestamp:
Jun 13, 2006, 3:48:27 PM (18 years ago)
Author:
brantley
Comment:

Updated it for post-magic-removal.

Legend:

Unmodified
Added
Removed
Modified
  • CookBookManipulatorCustomManipulator

    v7 v8  
    1 Using Manipulators can be annoying and confusing, use this to create simple, elegant ones:
     1Using Manipulators can be rather cumbersome.  Here is a class to simplify the process.
    22
    33{{{
    44#!python
    5 from django.core import formfields, validators
     5from django import forms
     6from django.core import validators
    67
    7 class Manipulator(formfields.Manipulator):
     8class Manipulator(forms.Manipulator):
    89    default = {}
    910    done = False
    1011   
    11     def getData(self, request):
     12    def __bool__(self):
     13        return self.done
     14   
     15    def get_data(self, request):
    1216        return request.POST
    1317   
    14     def getForm(self, data, errors):
    15         return formfields.FormWrapper(self, data, errors)
     18    def get_form(self, data, errors):
     19        return forms.FormWrapper(self, data, errors)
    1620   
    1721    def process(self, request):
    18         data = self.getData(request)
     22        data = self.get_data(request)
    1923        if data:
    2024            new_data = data.copy()
     
    2832            errors = {}
    2933            new_data = self.default
    30         self.form = self.getForm(new_data, errors)
     34        self.form = self.get_form(new_data, errors)
    3135        return self.form
    3236
    3337    def complete(self, request, data):
    34         pass
     38        self.save()
    3539}}}
    3640
    37 Now to use, subclass it, and fill in the necessary methods ("{{{__init__}}}" and "{{{complete}}}"), also give it the fields:
     41Now to use: you should subclass it and fill in the necessary methods ("{{{__init__}}}" and "{{{complete}}}"), and provide fields:
    3842
    3943{{{
    4044#!python
    4145class PollEdit(Manipulator):
     46    # Provide the fields.
    4247    fields = (
    43         formfields.TextField("title", maxlength=32, is_required=True),
    44         formfields.TextField("question", maxlength=128, is_required=True),
     48        forms.TextField("title", maxlength=32, is_required=True),
     49        forms.TextField("question", maxlength=128, is_required=True),
    4550    )
    46        
     51
    4752    def __init__(self, poll):
    48         # You can also define your fields here, if you'd like.
     53        # We could also provide our fields here, which is usefull if you have
     54        # custom validators that you need to reference off of "self."
     55
     56        # Save a reference to our poll.
    4957        self.poll = poll
    50         ## Luca M.: 03/09/2006
    51         ## I've to uncomment the following line to fill the form with the current data
    52         # self.default = poll.__dict__
    53         ## Is this correct ? I'm using django 0.91
     58
     59        # Set our default dictionary to match our poll object.
     60        # That way the data populating the fields will match our current poll.
     61        self.default = poll.__dict__
    5462       
    5563    def complete(self, request, data):
     64        # This is executed after the user submits valid data.
     65
     66        # Set the poll's title to the user-submited 'title' in our data.
    5667        self.poll.title = data['title']
     68
     69        # Same with 'question'
    5770        self.poll.question = data['question']
     71
     72        # Don't forget to save.
    5873        self.poll.save()
    5974
     
    6580#!python
    6681def edit_poll(request, id):
    67     poll = polls.get_object(pk=id)      #Assume it works
    68     manipulator = PollEdit(poll)
    69     form = manipulator.process(request)    #Process returns a form if it's needed
    70     if (form):
    71          return render_to_response('polls/edit_form', {'form': form})
    72     else:
     82    poll = get_object_or_404(Poll, pk=id)  # Get our poll object.
     83    manipulator = PollEdit(poll)    # Create the manipulator.
     84    manipulator.process(request)    # Process the request.
     85    if (manipulator.done):          # If we are done, redirect to the poll view page.
    7386         return HttpResponseRedirect("/polls/%d" % poll.id)
     87    else:                           # Otherwise, we render the form.
     88         return render_to_response('polls/edit_form', {'form': manipulator.form})
    7489}}}
    7590
    76 Here's a custom Add Manipulator I like to build off of first, so that the fields are the same:
     91Here's a custom Add Manipulator.  Here we can sub-class the first manipulator, so that the fields are the same:
    7792
    7893{{{
     
    8095class PollAdd(PollEdit):       
    8196    def __init__(self):
     97        # Set our default values.
    8298        self.default = {'title': "Unnamed", 'question': 'Insert your question here.'}
    83         #This sets your default values.
    8499   
    85100    def complete(self, request, data):
     101        # Create a new poll with our data.
    86102        poll = polls.Poll(title=data['title'], question=data['question'])
    87103        poll.save()
     104        # It'd be nice to be able to get the poll back.
    88105        return poll
    89106
     
    95112#!python
    96113def add_poll(request):
    97     manipulator = PollAdd()
    98     result = manipulator.process(request)
    99     if (manipulator.done): #Are we done processing?
    100          return HttpResponseRedirect("/polls/%d" % result.id)
    101     else: #Otherwise, place the form.
     114    manipulator = PollAdd()             # Create the manipulator
     115    poll = manipulator.process(request) # Process the request
     116    if (manipulator.done):              # Are we done processing?
     117         return HttpResponseRedirect("/polls/%d" % poll.id)
     118    else:                               # Otherwise, respond with the form.
    102119         return render_to_response('polls/new_form', {'form': manipulator.form})
    103120}}}
Back to Top