Changes between Version 7 and Version 8 of CookBookManipulatorCustomManipulator
- Timestamp:
- Jun 13, 2006, 3:48:27 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CookBookManipulatorCustomManipulator
v7 v8 1 Using Manipulators can be annoying and confusing, use this to create simple, elegant ones:1 Using Manipulators can be rather cumbersome. Here is a class to simplify the process. 2 2 3 3 {{{ 4 4 #!python 5 from django.core import formfields, validators 5 from django import forms 6 from django.core import validators 6 7 7 class Manipulator(form fields.Manipulator):8 class Manipulator(forms.Manipulator): 8 9 default = {} 9 10 done = False 10 11 11 def getData(self, request): 12 def __bool__(self): 13 return self.done 14 15 def get_data(self, request): 12 16 return request.POST 13 17 14 def get Form(self, data, errors):15 return form fields.FormWrapper(self, data, errors)18 def get_form(self, data, errors): 19 return forms.FormWrapper(self, data, errors) 16 20 17 21 def process(self, request): 18 data = self.get Data(request)22 data = self.get_data(request) 19 23 if data: 20 24 new_data = data.copy() … … 28 32 errors = {} 29 33 new_data = self.default 30 self.form = self.get Form(new_data, errors)34 self.form = self.get_form(new_data, errors) 31 35 return self.form 32 36 33 37 def complete(self, request, data): 34 pass38 self.save() 35 39 }}} 36 40 37 Now to use , subclass it, and fill in the necessary methods ("{{{__init__}}}" and "{{{complete}}}"), also give it the fields:41 Now to use: you should subclass it and fill in the necessary methods ("{{{__init__}}}" and "{{{complete}}}"), and provide fields: 38 42 39 43 {{{ 40 44 #!python 41 45 class PollEdit(Manipulator): 46 # Provide the fields. 42 47 fields = ( 43 form fields.TextField("title", maxlength=32, is_required=True),44 form fields.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), 45 50 ) 46 51 47 52 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. 49 57 self.poll = poll 50 ## Luca M.: 03/09/2006 51 # # I've to uncomment the following line to fill the form with the current data52 # self.default = poll.__dict__53 ## Is this correct ? I'm using django 0.9158 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__ 54 62 55 63 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. 56 67 self.poll.title = data['title'] 68 69 # Same with 'question' 57 70 self.poll.question = data['question'] 71 72 # Don't forget to save. 58 73 self.poll.save() 59 74 … … 65 80 #!python 66 81 def 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. 73 86 return HttpResponseRedirect("/polls/%d" % poll.id) 87 else: # Otherwise, we render the form. 88 return render_to_response('polls/edit_form', {'form': manipulator.form}) 74 89 }}} 75 90 76 Here's a custom Add Manipulator I like to build off of first, so that the fields are the same:91 Here's a custom Add Manipulator. Here we can sub-class the first manipulator, so that the fields are the same: 77 92 78 93 {{{ … … 80 95 class PollAdd(PollEdit): 81 96 def __init__(self): 97 # Set our default values. 82 98 self.default = {'title': "Unnamed", 'question': 'Insert your question here.'} 83 #This sets your default values.84 99 85 100 def complete(self, request, data): 101 # Create a new poll with our data. 86 102 poll = polls.Poll(title=data['title'], question=data['question']) 87 103 poll.save() 104 # It'd be nice to be able to get the poll back. 88 105 return poll 89 106 … … 95 112 #!python 96 113 def 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, placethe 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. 102 119 return render_to_response('polls/new_form', {'form': manipulator.form}) 103 120 }}}