Changes between Version 53 and Version 54 of S3/S3REST/s3_rest_controller

01/27/11 22:27:43 (13 years ago)
Dominic König



  • S3/S3REST/s3_rest_controller

    v53 v54  
    310310== Advanced Options ==
     312=== Pre-populating Create-Forms ===
     314Create-forms can be pre-populated with data by one of these 3 methods:
     316  1. model defaults (standard)
     317  2. values from another record in the database
     318  3. values provided by the controller
     320Model defaults are defined per field and can be set as {{{db.my_table.field.default = value}}} at any time before the REST method is applied (even in the pre-process).
     322Values from another record in the database can be used via a URL query like:
     324  - /my/resource/create?from_record=''id''&from_fields=''fieldname1'',''fieldname2'',...
     326The ''id'' of the original record can also be specified as '''', if the original record is in another table. Additionally, fieldnames can be specified as ''fieldname$original_fieldname'' to map between different fieldnames.
     328To pre-populate Create-forms from the controller, you can specify the variable {{{populate}}} in the arguments of {{{s3_rest_controller}}}:
     331output = s3_rest_controller(prefix, resourcename,
     332                            populate=dict(fieldname1=value1,
     333                                          fieldname2=value2))
     334return output
     337Instead of a {{{dict}}}, you can also pass a callable object as {{{populate}}}. This will be executed with the current {{{S3Request}}} and the names arguments of {{{s3_rest_controller}}} in order to produce the field/value {{{dict}}}:
     340def populate(r, **attr):
     341    """
     342    Helper function to pre-populate create-forms
     344    """
     346    # some code to produce the data
     347    # ...
     349    # return the dict
     350    return dict(fieldname1=value1,
     351                fieldname2=value2)
     353output = s3_rest_controller(prefix, resourcename,
     354                            populate=populate)
     356return output
     359Note that {{{populate}}} will only be applied in {{{GET}}} requests and only if no record_id is specified. That means, if it uses a separate form to generate the data, you need to revert the request into {{{GET}}} in order to have the create-form pre-populated:
     362data = None
     363form = FORM(...some form...)
     364output = dict(helper_form=form)
     365if form.accepts(request.vars, session, formname="helper_form"):
     366    output = dict() # remove the helper form
     367    data = Storage(...) # some code to extract the data from the helper form
     368if data:
     369    request.env.request_method = "GET" # revert to GET if data available
     371_output = s3_rest_controller(prefix, resourcename, populate=data)
     373if isinstance(_output, dict):
     374    output.update(_output)
     376    output = _output
     378return output
     381NB: This construction could be used e.g. to loop in an OCR client into a REST controller. The helper form would then be a file upload form which is displayed alongside with the normal create-form - so the user can either enter data manually and submit the create-form, or first submit a file to pre-populate the create form, and then edit the data and submit the create-form.
    312382=== Pre-Process ===