<snipped Andres examples>

Thanks Andres,

quite interesting. The way i solved it was based on parts of the wiki
and loads of trial and error.
I made a class that simplifies using custom Manipulators and then
derived a specific class for adding a Patient (model i'm using) where i
initialise the derived class by calling the init function of the
standard add manipulator.
Apparently a class variable default is initialised then and then you can
adjust the values you like.
It's quite elegant and seems a bit clearer (to me that is, eye of the
beholder eh :)).

urls.py
=======
...
(r'^patient/ophalen/(?P<opnamenr>[-\w]+)/$','gema.main.views.patient_ophalen'),
...

views.py
========
from gema.main.patient import PatientAddManipulator
...
def patient_ophalen(request, opnamenr):
    manipulator = PatientAddManipulator()   # Create the manipulator
    patient = manipulator.process(request)  # Process the request
    if ( patient != None ):
         return render_to_response('main/patient_form.html', {'form':
manipulator.form})
    else:               # Otherwise, redirect to the patient view page.
         return HttpResponseRedirect("/patient/lijst/")
..

patient.py
==========
from gema.main.models import Patient
from django import forms

### Custom Manipulator ###
class PatientCustom:
    def __bool__(self):
        return self.done

    def get_data(self, request):
        return request.POST

    def get_form(self, data, errors):
        return forms.FormWrapper(self, data, errors)

    def process(self, request):
        data = self.get_data(request)
        if data:
            new_data = data.copy()
            errors = self.get_validation_errors(new_data)
            if not errors:
                self.do_html2python(new_data)
                self.done = True
                return self.complete(request, new_data)
        else:
            errors = {}
            new_data = self.default
        self.form = self.get_form(new_data, errors)
        return self.form

    def complete(self, request, data):
        self.save(data)
        return None

class PatientAddManipulator(Patient.AddManipulator, PatientCustom):
    default = {}
    done = False

    def __init__(self):
        # Construct the fields and set the values in default
        Patient.AddManipulator.__init__(self)
        PatientAddManipulator.default["voornaam"]="T"

The patient_form.html contains a form with the correct fields and a
piece of javascript to direct the user to the
patient/ophalen/(?P<opnamenr> url. I used a piece of javascript to also
add the number <opnamenr>.

==========================
The last line of PatientAddManipulator is where the "magic" is for
adding a default value.
Here i override the firstname and just put in the letter T (ok silly but
that's just to test things out :)).

I have no idea if this is a good way of doing it but it works and it
looks clean so i'm going with that  :)

Regards,
Benedict



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to