'Saves changes to an existing request, or creates a new
request if received request is None.'

def __save_request(self, trans, cntrller, request=None, **kwd):

    params = util.Params(kwd)
    request_type_id = params.get('request_type_id', None)
    is_admin = ((cntrller == 'requests_admin') and trans.user_is_admin())
    if (request is None):
        request_type = trans.sa_session.query(trans.model.RequestType).get(trans.security.decode_id(request_type_id))
        if is_admin:
            user_id = params.get('user_id', '')
            user = trans.sa_session.query(trans.model.User).get(trans.security.decode_id(user_id))
        else:
            user = trans.user
    else:
        user = request.user
        request_type = request.type
    name = util.restore_text(params.get('name', ''))
    desc = util.restore_text(params.get('desc', ''))
    notification = dict(email=[user.email], sample_states=[request_type.final_sample_state.id], body='', subject='')
    values = self.get_form_values(trans, user, request_type.request_form, **kwd)
    if (request is None):
        form_values = trans.model.FormValues(request_type.request_form, values)
        trans.sa_session.add(form_values)
        request = trans.model.Request(name, desc, request_type, user, form_values, notification)
        trans.sa_session.add(request)
        trans.sa_session.flush()
        trans.sa_session.refresh(request)
        comment = ('Sequencing    request    created    by    %s' % trans.user.email)
        if (request.user != trans.user):
            comment += ('    on    behalf    of    %s.' % request.user.email)
        event = trans.model.RequestEvent(request, request.states.NEW, comment)
        trans.sa_session.add(event)
        trans.sa_session.flush()
    else:
        request.name = name
        request.desc = desc
        request.type = request_type
        request.user = user
        request.notification = notification
        request.values.content = values
        trans.sa_session.add(request)
        trans.sa_session.add(request.values)
        trans.sa_session.flush()
    return request
