# Path: openprocurement/tender/openua/validation.py
# def validate_update_bid_to_draft(request):
#     bid_status_to = request.validated['data'].get("status", request.context.status)
#     if request.context.status != 'draft' and bid_status_to == 'draft':
#         request.errors.add('body', 'bid', 'Can\'t update bid to ({}) status'.format(bid_status_to))
#         request.errors.status = 403
#         raise error_handler(request.errors)
# 
# def validate_update_bid_to_active_status(request):
#     bid_status_to = request.validated['data'].get("status", request.context.status)
#     if bid_status_to != request.context.status and bid_status_to != 'active':
#         request.errors.add('body', 'bid', 'Can\'t update bid to ({}) status'.format(bid_status_to))
#         request.errors.status = 403
#         raise error_handler(request.errors)


from openprocurement.tender.belowthreshold.views.bid import TenderBidResource
from openprocurement.api.utils import (
    set_ownership,
    json_view,
    context_unpack,
    get_now,
    raise_operation_error
)
from openprocurement.tender.core.validation import (
    validate_bid_data,
    validate_patch_bid_data,
    validate_update_deleted_bid,
    validate_bid_operation_period,
    validate_bid_operation_not_in_tendering
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource
)
from openprocurement.tender.openua.validation import (
    validate_update_bid_to_draft,
    validate_update_bid_to_active_status
)
                        "amount": 489,
                        "currency": "UAH",
                        "valueAddedTaxIncluded": true
                    }
                }
            }

        """
        # See https://github.com/open-contracting/standard/issues/78#issuecomment-59830415
        # for more info upon schema
        tender = self.request.validated['tender']
        bid = self.request.validated['bid']
        if bid.status not in self.allowed_bid_status_on_create:
            raise_operation_error(self.request, 'Bid can be added only with status: {}.'.format(self.allowed_bid_status_on_create))
        tender.modified = False
        set_ownership(bid, self.request)
        tender.bids.append(bid)
        if save_tender(self.request):
            self.LOGGER.info('Created tender bid {}'.format(bid.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_bid_create'}, {'bid_id': bid.id}))
            self.request.response.status = 201
            self.request.response.headers['Location'] = self.request.route_url('{}:Tender Bids'.format(tender.procurementMethodType), tender_id=tender.id, bid_id=bid['id'])
            return {
                'data': bid.serialize('view'),
                'access': {
                    'token': bid.owner_token
                }
            }

    @json_view(content_type="application/json", permission='edit_bid', validators=(validate_patch_bid_data, validate_bid_operation_not_in_tendering, validate_bid_operation_period,

# Path: openprocurement/tender/openua/validation.py
# def validate_update_bid_to_draft(request):
#     bid_status_to = request.validated['data'].get("status", request.context.status)
#     if request.context.status != 'draft' and bid_status_to == 'draft':
#         request.errors.add('body', 'bid', 'Can\'t update bid to ({}) status'.format(bid_status_to))
#         request.errors.status = 403
#         raise error_handler(request.errors)
# 
# def validate_update_bid_to_active_status(request):
#     bid_status_to = request.validated['data'].get("status", request.context.status)
#     if bid_status_to != request.context.status and bid_status_to != 'active':
#         request.errors.add('body', 'bid', 'Can\'t update bid to ({}) status'.format(bid_status_to))
#         request.errors.status = 403
#         raise error_handler(request.errors)


# Path: openprocurement/tender/openua/views/bid.py
from openprocurement.tender.belowthreshold.views.bid import TenderBidResource
from openprocurement.api.utils import (
    set_ownership,
    json_view,
    context_unpack,
    get_now,
    raise_operation_error
)
from openprocurement.tender.core.validation import (
    validate_bid_data,
    validate_patch_bid_data,
    validate_update_deleted_bid,
    validate_bid_operation_period,
    validate_bid_operation_not_in_tendering
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource
)
from openprocurement.tender.openua.validation import (
    validate_update_bid_to_draft,
    validate_update_bid_to_active_status
)

                        "amount": 489,
                        "currency": "UAH",
                        "valueAddedTaxIncluded": true
                    }
                }
            }

        """
        # See https://github.com/open-contracting/standard/issues/78#issuecomment-59830415
        # for more info upon schema
        tender = self.request.validated['tender']
        bid = self.request.validated['bid']
        if bid.status not in self.allowed_bid_status_on_create:
            raise_operation_error(self.request, 'Bid can be added only with status: {}.'.format(self.allowed_bid_status_on_create))
        tender.modified = False
        set_ownership(bid, self.request)
        tender.bids.append(bid)
        if save_tender(self.request):
            self.LOGGER.info('Created tender bid {}'.format(bid.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_bid_create'}, {'bid_id': bid.id}))
            self.request.response.status = 201
            self.request.response.headers['Location'] = self.request.route_url('{}:Tender Bids'.format(tender.procurementMethodType), tender_id=tender.id, bid_id=bid['id'])
            return {
                'data': bid.serialize('view'),
                'access': {
                    'token': bid.owner_token
                }
            }

    @json_view(content_type="application/json", permission='edit_bid', validators=(validate_patch_bid_data, validate_bid_operation_not_in_tendering, validate_bid_operation_period,

               validate_update_deleted_bid, validate_update_bid_to_draft, validate_update_bid_to_active_status))