| | 1 | Using Manipulators can be annoying and confusing, use this to create simple, elegant ones: |
| | 2 | |
| | 3 | {{{ |
| | 4 | from django.core import formfields, validators |
| | 5 | |
| | 6 | class 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 | |
| | 36 | Now to use, subclass it, and fill in the necessary methods ("__init__" and "complete"), also give it the fields: |
| | 37 | |
| | 38 | {{{ |
| | 39 | class 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 | |
| | 56 | Usage of the final Manipulator is quite simple: |
| | 57 | |
| | 58 | {{{ |
| | 59 | def 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 | |
| | 69 | Here's a custom Add Manipulator: |
| | 70 | |
| | 71 | {{{ |
| | 72 | class 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 | |
| | 89 | And here's the add usage: |
| | 90 | |
| | 91 | {{{ |
| | 92 | def 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 | |