Beispiel #1
0
def update(request,
           callback=None,
           renderers=None,
           validators=None,
           values=None):
    """Base method to handle update requests. This view will render a
    update form to update items on (GET) requests.

    It the user submits the data (POST) that the data will be validated.
    If validation is successfull the item will be saved in the datebase
    and the callback method will be called. Finally a redirect to either
    the backurl or the edit or read mode of the item will be triggered.

    If the validation fails, the form will be rendered again.
    :request: Current request
    :callback: Current function which is called after the item has been read.
    :renderers: Dictionary of external renderers which should be used
                for renderering some form elements.
    :validators: List of external formbar validators which should be
                 added to the form for validation
    :values: Dictionary of additional values which will be available in
             the form
    :returns: Dictionary or Redirect.
    """
    if values is None:
        values = {}
    values['_roles'] = [str(r.name) for r in request.user.roles]
    form = get_item_form('update', request, renderers, validators, values)
    if request.POST:
        if handle_POST_request(form, request, callback, 'update', renderers):
            return handle_redirect_on_success(request)

    rvalues = get_return_value(request)
    rvalues['form'] = render_item_form(request, form)
    return rvalues
Beispiel #2
0
def update(request, callback=None, renderers=None,
           validators=None, values=None):
    """Base method to handle update requests. This view will render a
    update form to update items on (GET) requests.

    It the user submits the data (POST) that the data will be validated.
    If validation is successfull the item will be saved in the datebase
    and the callback method will be called. Finally a redirect to either
    the backurl or the edit or read mode of the item will be triggered.

    If the validation fails, the form will be rendered again.
    :request: Current request
    :callback: Current function which is called after the item has been read.
    :renderers: Dictionary of external renderers which should be used
                for renderering some form elements.
    :validators: List of external formbar validators which should be
                 added to the form for validation
    :values: Dictionary of additional values which will be available in
             the form
    :returns: Dictionary or Redirect.
    """
    handle_history(request)
    handle_params(request)
    if values is None:
        values = {}
    values['_roles'] = [str(r.name) for r in request.user.roles]
    form = get_item_form('update', request, renderers, validators, values)
    if request.POST:
        if handle_POST_request(form, request, callback, 'update', renderers):
            return handle_redirect_on_success(request)

    rvalues = get_return_value(request)
    rvalues['form'] = render_item_form(request, form, values)
    return rvalues
Beispiel #3
0
def create(request, callback=None, renderers=None, validators=None):
    """Base method to handle create requests. This view will render a
    create form to update items on (GET) requests.

    It the user submits the data (POST) that the data will be validated.
    If validation is successfull the item will be saved in the datebase
    and the callback method will be called. Finally a redirect to either
    the backurl or the edit or read mode of the item will be triggered.

    If the validation fails, the form will be rendered again.

    :request: Current request
    :callback: Current function which is called after the item has been read.
    :renderers: Dictionary of external renderers which should be used
                for renderering some form elements.
    :validators: List of external formbar validators which should be
                 added to the form for validation
    :returns: Dictionary or Redirect.
    """
    handle_history(request)
    params = handle_params(request)

    # Create a new item
    clazz = request.context.__model__
    try:
        factory = clazz.get_item_factory(request)
    except TypeError:
        # Old version of get_item_factory method which does not take an
        # request parameter.
        factory = clazz.get_item_factory()
        factory._request = request
    # Only create a new item if there isn't already an item in the
    # request.context. This can happen if we define a custom view for
    # create where the item gets created before to set some default
    # values e.g (See create function of forms)
    if not request.context.item:
        request.context.item = factory.create(request.user, {})
    form = get_item_form(params.get("form", "create"),
                         request, renderers, validators)
    if request.POST and 'blobforms' not in request.params:
        if handle_POST_request(form, request, callback, 'create', renderers):
            return handle_redirect_on_success(request)
    rvalues = get_return_value(request)
    values = {'_roles': [str(r.name) for r in request.user.roles]}
    values.update(params.get('values', {}))
    rvalues['form'] = render_item_form(request, form, values, False)
    return rvalues
Beispiel #4
0
def read(request, callback=None, renderers=None):
    """Base method to handle read requests. Returns a dictionary of
    values used available in the rendererd template The template to
    render is defined in the view configuration.

    :request: Current request
    :callback: Current function which is called after the item has been read.
    :returns: Dictionary.
    """
    handle_history(request)
    handle_params(request)
    handle_callback(request, callback)
    rvalues = get_return_value(request)
    values = {'_roles': [str(r.name) for r in request.user.roles]}
    form = get_item_form('read', request, renderers, values=values)
    rvalues['form'] = render_item_form(request, form, values)
    return rvalues
Beispiel #5
0
def read(request, callback=None, renderers=None):
    """Base method to handle read requests. Returns a dictionary of
    values used available in the rendererd template The template to
    render is defined in the view configuration.

    :request: Current request
    :callback: Current function which is called after the item has been read.
    :returns: Dictionary.
    """
    handle_params(request)
    handle_callback(request, callback, mode="pre,default")
    rvalues = get_return_value(request)
    values = {'_roles': [str(r.name) for r in request.user.roles]}
    form = get_item_form('read', request, renderers, values=values)
    rvalues['form'] = render_item_form(request, form)
    handle_callback(request, callback, mode="post")
    return rvalues
Beispiel #6
0
def rest_update(request, callback=None):
    """Updates an item of type clazz. The item is loaded based on the
    unique id value provided in the matchtict object in the current
    request. The item will be updated with the data submitted in the
    current PUT request. Before updating the item the data will be
    validated against the "update" form of the item. If the validation
    fails the item will not be updated. In all cases the item is return as
    JSON object with the item and updated values back to the client. The
    JSON Response will include further details on the reason why the
    validation failed.

    :request: Current request
    :returns: JSON object.
    """
    item = get_item_from_request(request)
    form = get_item_form('update', request)
    if form.validate(request.params):
        item.save(form.data, request)
        return JSONResponse(True, item)
    else:
        # Validation fails! return item
        return JSONResponse(False, item)
Beispiel #7
0
def rest_update(request, callback=None):
    """Updates an item of type clazz. The item is loaded based on the
    unique id value provided in the matchtict object in the current
    request. The item will be updated with the data submitted in the
    current PUT request. Before updating the item the data will be
    validated against the "update" form of the item. If the validation
    fails the item will not be updated. In all cases the item is return as
    JSON object with the item and updated values back to the client. The
    JSON Response will include further details on the reason why the
    validation failed.

    :request: Current request
    :returns: JSON object.
    """
    item = get_item_from_request(request)
    form = get_item_form('update', request)
    if form.validate(request.params):
        item.save(form.data, request)
        return JSONResponse(True, item)
    else:
        # Validation fails! return item
        return JSONResponse(False, item)
Beispiel #8
0
def create(request, callback=None, renderers=None,
           validators=None, values=None):
    """Base method to handle create requests. This view will render a
    create form to update items on (GET) requests.

    It the user submits the data (POST) that the data will be validated.
    If validation is successfull the item will be saved in the datebase
    and the callback method will be called. Finally a redirect to either
    the backurl or the edit or read mode of the item will be triggered.

    If the validation fails, the form will be rendered again.

    :request: Current request
    :callback: Current function which is called after the item has been read.
    :renderers: Dictionary of external renderers which should be used
                for renderering some form elements.
    :validators: List of external formbar validators which should be
                 added to the form for validation
    :values: Dictionary of additional values which will be available in
             the form
    :returns: Dictionary or Redirect.
    """
    handle_history(request)
    params = handle_params(request)

    # Create a new item
    clazz = request.context.__model__
    try:
        factory = clazz.get_item_factory(request)
    except TypeError:
        # Old version of get_item_factory method which does not take an
        # request parameter.
        factory = clazz.get_item_factory()
        factory._request = request
    # Only create a new item if there isn't already an item in the
    # request.context. This can happen if we define a custom view for
    # create where the item gets created before to set some default
    # values e.g (See create function of forms)
    if not request.context.item:
        request.context.item = factory.create(request.user, {})
    if values is None:
        values = {}
    values['_roles'] = [str(r.name) for r in request.user.roles]
    values.update(params.get('values', {}))

    #  FIXME: "form_values" seems to be only used in one single
    #  application (efa). For now we will leave this here to not break
    #  any things but it should be removed.
    #  See https://github.com/ringo-framework/ringo/issues/31
    #  (ti) <2016-10-18 21:51>
    form_values = request.session.get("form_values") or {}
    values.update(form_values)
    request.session["form_values"] = None
    request.session.save()

    form = get_item_form(params.get("form", "create"),
                         request, renderers, validators, values=values)
    if request.POST and 'blobforms' not in request.params:
        if handle_POST_request(form, request, callback, 'create', renderers):
            return handle_redirect_on_success(request)
    rvalues = get_return_value(request)
    rvalues['form'] = render_item_form(request, form, validate=False)
    return rvalues