| | 4 | |
| | 5 | == View Functions == |
| | 6 | |
| | 7 | For view functions to work properly with all new features, a few changes are in order. |
| | 8 | |
| | 9 | From: |
| | 10 | {{{ |
| | 11 | def style_edit(request, object_id): |
| | 12 | try: |
| | 13 | manipulator = styles.ChangeManipulator(object_id) |
| | 14 | except ObjectDoesNotExist: |
| | 15 | raise Http404 |
| | 16 | |
| | 17 | if request.POST: |
| | 18 | new_data = request.POST.copy() |
| | 19 | |
| | 20 | errors = manipulator.get_validation_errors(new_data) |
| | 21 | |
| | 22 | if not errors: |
| | 23 | manipulator.do_html2python(new_data) # '''WRONG!!''' |
| | 24 | manipulator.save(new_data) |
| | 25 | return HttpResponseRedirect("") |
| | 26 | else: |
| | 27 | # Populate new_data with a "flattened" version of the current data. |
| | 28 | new_data = manipulator.original_object.__dict__ # '''WRONG!!''' |
| | 29 | errors = {} |
| | 30 | |
| | 31 | mash_around_with_the_data_dict_to_make_foreign_keys_work(new_data) # '''WRONG!!''' |
| | 32 | |
| | 33 | # Populate the FormWrapper. |
| | 34 | form = formfields.FormWrapper(manipulator, new_data, errors) |
| | 35 | |
| | 36 | return render_to_response('style_edit', { 'form': form }) |
| | 37 | }}} |
| | 38 | |
| | 39 | To: |
| | 40 | {{{ |
| | 41 | def style_edit(request, object_id): |
| | 42 | try: |
| | 43 | manipulator = styles.ChangeManipulator(object_id) |
| | 44 | except ObjectDoesNotExist: |
| | 45 | raise Http404 |
| | 46 | |
| | 47 | if request.POST: |
| | 48 | new_data = request.POST.copy() |
| | 49 | errors = manipulator.get_validation_errors(new_data) |
| | 50 | |
| | 51 | manipulator.do_html2python(new_data) # '''CORRECT!!''' |
| | 52 | if not errors: |
| | 53 | manipulator.save(new_data) |
| | 54 | return HttpResponseRedirect("") |
| | 55 | else: |
| | 56 | # Populate new_data with a "flattened" version of the current data. |
| | 57 | new_data = manipulator.flatten_data() # '''CORRECT!!''' |
| | 58 | errors = {} |
| | 59 | |
| | 60 | |
| | 61 | # Populate the FormWrapper. |
| | 62 | form = formfields.FormWrapper(manipulator, new_data, errors) |
| | 63 | |
| | 64 | return render_to_response('style_edit', { 'form': form }) |
| | 65 | |
| | 66 | }}} |
| | 67 | Without these changes, the new fixes will not work. It should work about as well as the trunk without these changes. |
| | 68 | |
| | 69 | So, |
| | 70 | |
| | 71 | 1. Make sure html2python is called regardless of whether there are errors, but *after* validation. |
| | 72 | 2. Make sure you use flatten_data to get your new_data to fill in your forms, NOT just the __dict__ of the object you are editing. Otherwise, dates, foreign keys, inline editing and a whole host of other things will NOT work properly, just like they do not work on trunk. |
| | 73 | 3. Make sure you remove any hacks for modifying the data in your view function. This is now done by the fields themselves in flatten_data and instances where it works unexpectedly should be filed as bugs. |