# Path: openprocurement/tender/openeu/utils.py
# LOGGER = getLogger(__name__)
# def check_initial_bids_count(request):
# def prepare_qualifications(request, bids=[], lotId=None):
# def all_bids_are_reviewed(request):
# def check_status(request):
# 
# Path: openprocurement/tender/openeu/validation.py
# def validate_qualification_document_operation_not_in_pending(request):
#     qualification = request.validated['qualification']
#     if qualification.status != 'pending':
#         raise_operation_error(request, 'Can\'t {} document in current qualification status'.format(OPERATIONS.get(request.method)))
# 
# def validate_qualification_document_operation_not_in_allowed_status(request):
#     if request.validated['tender_status'] != 'active.pre-qualification':
#         raise_operation_error(request, 'Can\'t {} document in current ({}) tender status'.format(OPERATIONS.get(request.method), request.validated['tender_status']))


from openprocurement.api.utils import (
    get_file,
    upload_file,
    update_file_content_type,
    json_view,
    context_unpack,
    APIResource,
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch
)
from openprocurement.api.validation import (
    validate_file_update,
    validate_file_upload,
    validate_patch_document_data,
)
from openprocurement.tender.core.validation import validate_tender_document_update_not_by_author_or_tender_owner
from openprocurement.tender.openeu.utils import qualifications_resource
from openprocurement.tender.openeu.validation import (
    validate_qualification_document_operation_not_in_pending,
    validate_qualification_document_operation_not_in_allowed_status
)
            self.request.response.status = 201
            document_route = self.request.matched_route.name.replace("collection_", "")
            self.request.response.headers['Location'] = self.request.current_route_url(_route_name=document_route, document_id=document.id, _query={})
            return {'data': document.serialize("view")}

    @json_view(permission='view_tender')
    def get(self):
        """Tender Qualification Document Read"""
        if self.request.params.get('download'):
            return get_file(self.request)
        document = self.request.validated['document']
        document_data = document.serialize("view")
        document_data['previousVersions'] = [
            i.serialize("view")
            for i in self.request.validated['documents']
            if i.url != document.url
        ]
        return {'data': document_data}

    @json_view(validators=(validate_file_update, validate_qualification_document_operation_not_in_allowed_status, validate_qualification_document_operation_not_in_pending,
               validate_tender_document_update_not_by_author_or_tender_owner), permission='upload_tender_documents')
    def put(self):
        """Tender Qualification Document Update"""
        document = upload_file(self.request)
        self.request.validated['qualification'].documents.append(document)
        if save_tender(self.request):
            self.LOGGER.info('Updated tender qualification document {}'.format(self.request.context.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_qualification_document_put'}))
            return {'data': document.serialize("view")}


# Path: openprocurement/tender/openeu/utils.py
# LOGGER = getLogger(__name__)
# def check_initial_bids_count(request):
# def prepare_qualifications(request, bids=[], lotId=None):
# def all_bids_are_reviewed(request):
# def check_status(request):
# 
# Path: openprocurement/tender/openeu/validation.py
# def validate_qualification_document_operation_not_in_pending(request):
#     qualification = request.validated['qualification']
#     if qualification.status != 'pending':
#         raise_operation_error(request, 'Can\'t {} document in current qualification status'.format(OPERATIONS.get(request.method)))
# 
# def validate_qualification_document_operation_not_in_allowed_status(request):
#     if request.validated['tender_status'] != 'active.pre-qualification':
#         raise_operation_error(request, 'Can\'t {} document in current ({}) tender status'.format(OPERATIONS.get(request.method), request.validated['tender_status']))


# Path: openprocurement/tender/openeu/views/qualification_document.py
from openprocurement.api.utils import (
    get_file,
    upload_file,
    update_file_content_type,
    json_view,
    context_unpack,
    APIResource,
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch
)
from openprocurement.api.validation import (
    validate_file_update,
    validate_file_upload,
    validate_patch_document_data,
)
from openprocurement.tender.core.validation import validate_tender_document_update_not_by_author_or_tender_owner
from openprocurement.tender.openeu.utils import qualifications_resource
from openprocurement.tender.openeu.validation import (
    validate_qualification_document_operation_not_in_pending,
    validate_qualification_document_operation_not_in_allowed_status
)

            self.request.response.status = 201
            document_route = self.request.matched_route.name.replace("collection_", "")
            self.request.response.headers['Location'] = self.request.current_route_url(_route_name=document_route, document_id=document.id, _query={})
            return {'data': document.serialize("view")}

    @json_view(permission='view_tender')
    def get(self):
        """Tender Qualification Document Read"""
        if self.request.params.get('download'):
            return get_file(self.request)
        document = self.request.validated['document']
        document_data = document.serialize("view")
        document_data['previousVersions'] = [
            i.serialize("view")
            for i in self.request.validated['documents']
            if i.url != document.url
        ]
        return {'data': document_data}

    @json_view(validators=(validate_file_update, validate_qualification_document_operation_not_in_allowed_status, validate_qualification_document_operation_not_in_pending,
               validate_tender_document_update_not_by_author_or_tender_owner), permission='upload_tender_documents')
    def put(self):
        """Tender Qualification Document Update"""
        document = upload_file(self.request)
        self.request.validated['qualification'].documents.append(document)
        if save_tender(self.request):
            self.LOGGER.info('Updated tender qualification document {}'.format(self.request.context.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_qualification_document_put'}))
            return {'data': document.serialize("view")}


    @json_view(content_type="application/json", validators=(validate_patch_document_data, validate_qualification_document_operation_not_in_allowed_status,