Ejemplo n.º 1
0
def validate_change_status(request, error_handler, **kwargs):
    """
        This validator get dict from adapter and validate availibility
        to change status by dict.
    """
    # Get resource_type
    resource_type = request.validated['resource_type']
    # Get status from PATCH validated data
    new_status = request.validated['data'].get("status")
    # Get model from context
    model = request.context

    # Check status in data and not equal to context status
    if not new_status or new_status == model.status:
        return

    # get available statuses from dict
    statuses = request.content_configurator.available_statuses[model.status]['next_status']
    # verify right status change (auth_role and target status)
    msg = 'Can\'t update {} in current ({}) status'.format(resource_type,
                                                           model.status)

    if new_status not in statuses or \
            request.authenticated_role not in statuses.get(new_status, {}):
        raise_operation_error(request, error_handler, msg)
def validate_document_operation_in_not_allowed_lot_status(
        request, error_handler, **kwargs):
    status = request.validated['lot_status']
    if status != 'pending':
        raise_operation_error(
            request, error_handler,
            'Can\'t update document in current ({}) lot status'.format(status))
Ejemplo n.º 3
0
def validate_patch_lot_data(request, error_handler, **kwargs):
    data = validate_json_data(request)
    if request.context.status != 'draft':
        return validate_data(request, type(request.lot), True, data)
    default_status = type(request.lot).fields['status'].default
    if data.get('status') != default_status:
        raise_operation_error(request, error_handler, 'Can\'t update lot in current (draft) status')
    request.validated['data'] = {'status': default_status}
    request.context.status = default_status
Ejemplo n.º 4
0
def validate_lot_status_update_in_terminated_status(request, error_handler,
                                                    **kwargs):
    lot = request.context
    if request.authenticated_role != 'Administrator' and lot.status in [
            'active', 'deleted'
    ]:
        raise_operation_error(
            request, error_handler,
            'Can\'t update lot in current ({}) status'.format(lot.status))
Ejemplo n.º 5
0
def validate_patch_lot_data(request, error_handler, **kwargs):
    data = validate_json_data(request)
    if request.context.status != 'draft':
        return validate_data(request, type(request.lot), True, data)
    default_status = type(request.lot).fields['status'].default
    if data.get('status') != default_status:
        raise_operation_error(request, error_handler,
                              'Can\'t update lot in current (draft) status')
    request.validated['data'] = {'status': default_status}
    request.context.status = default_status
def validate_patch_asset_data(request, error_handler, **kwargs):
    data = validate_json_data(request)
    editing_roles = request.content_configurator.available_statuses[
        request.context.status]['editing_permissions']
    if request.authenticated_role not in editing_roles:
        msg = 'Can\'t update {} in current ({}) status'.format(
            request.validated['resource_type'], request.context.status)
        raise_operation_error(request, error_handler, msg)
    default_status = type(request.asset).fields['status'].default
    if data.get('status') == default_status and data.get(
            'status') != request.context.status:
        raise_operation_error(
            request, error_handler,
            'Can\'t switch asset to {} status'.format(default_status))
    return validate_data(request, type(request.asset), True, data)
def validate_change_lot_status(request, error_handler, **kwargs):
    '''Validate status changes using STATUS_CHANGES rules.'''

    current_status = request.context.status
    new_status = validate_json_data(request).get('status')
    auth_role = request.authenticated_role

    if not new_status or auth_role == 'Administrator':
        return

    if new_status in STATUS_CHANGES[current_status] and \
       auth_role == STATUS_CHANGES[current_status][new_status]:
        request.validated['data'] = {'status': new_status}
    else:
        raise_operation_error(
            request, error_handler,
            'Can\'t update lot in current ({}) status'.format(current_status))
Ejemplo n.º 8
0
def validate_change_asset_status(request, error_handler, **kwargs):
    '''
        Validate method that check status changes.
    '''
    data = validate_json_data(request)
    asset = request.context
    for status in STATUS_CHANGES:
        if asset.status == status:
            new_status = data.get('status', False)
            auth_role = request.authenticated_role
            if new_status and new_status not in STATUS_CHANGES[status].keys():
                raise_operation_error(
                    request, error_handler,
                    'Can\'t update asset in current ({}) status'.format(
                        asset.status))
            elif new_status and auth_role != 'Administrator' and auth_role != STATUS_CHANGES[
                    status].get(new_status, ''):
                raise_operation_error(
                    request, error_handler,
                    'Can\'t update asset in current ({}) status'.format(
                        asset.status))
            request.validated['data'] = {'status': status}
            request.context.status = status
            break
Ejemplo n.º 9
0
def validate_lot_status_update_in_terminated_status(request, error_handler, **kwargs):
    lot = request.context
    if request.authenticated_role != 'Administrator' and lot.status in ['active', 'deleted']:
        raise_operation_error(request, error_handler, 'Can\'t update lot in current ({}) status'.format(lot.status))