<commit_before># -*- coding: utf-8 -*-
from cornice.resource import resource, view
from openprocurement.api.models import Award, Complaint, STAND_STILL_TIME, get_now
from openprocurement.api.utils import (
    apply_data_patch,
    filter_data,
    save_tender,
)
from openprocurement.api.validation import (
    validate_complaint_data,
    validate_patch_complaint_data,
    validate_tender_award_complaint_exists,
    validate_tender_award_exists_by_award_id,
)


@resource(name='Tender Award Complaints',
          collection_path='/tenders/{tender_id}/awards/{award_id}/complaints',
          path='/tenders/{tender_id}/awards/{award_id}/complaints/{id}',
          description="Tender award complaints")
class TenderAwardComplaintResource(object):

    def __init__(self, request):
        self.request = request
        self.db = request.registry.db

    @view(content_type="application/json", validators=(validate_complaint_data, validate_tender_award_exists_by_award_id), renderer='json')
    def collection_post(self):
        """Post a complaint for award
        """
        tender = self.request.validated['tender']
        if tender.status not in ['active.qualification', 'active.awarded']:
            self.request.errors.add('body', 'data', 'Can\'t add complaint in current tender status')
            self.request.errors.status = 403
            return
        complaint_data = filter_data(self.request.validated['data'], fields=['id', 'date', 'status'])
        complaint = Complaint(complaint_data)
        src = tender.serialize("plain")
        self.request.validated['award'].complaints.append(complaint)
        save_tender(tender, src, self.request)
        self.request.response.status = 201
        self.request.response.headers['Location'] = self.request.route_url('Tender Award Complaints', tender_id=tender.id, award_id=self.request.validated['award_id'], id=complaint['id'])
        return {'data': complaint.serialize("view")}

    @view(renderer='json', validators=(validate_tender_award_exists_by_award_id,))
    def collection_get(self):
        """List complaints for award
        """
        return {'data': [i.serialize("view") for i in self.request.validated['award'].complaints]}

    @view(renderer='json', validators=(validate_tender_award_complaint_exists,))
    def get(self):
        """Retrieving the complaint for award
        """
        return {'data': self.request.validated['complaint'].serialize("view")}

    @view(content_type="application/json", validators=(validate_patch_complaint_data, validate_tender_award_complaint_exists), renderer='json')
    def patch(self):
        """Post a complaint resolution for award
        """
        tender = self.request.validated['tender']
        if tender.status not in ['active.qualification', 'active.awarded']:
            self.request.errors.add('body', 'data', 'Can\'t update complaint in current tender status')
            self.request.errors.status = 403
            return
        complaint = self.request.validated['complaint']
        if complaint.status != 'accepted':
            self.request.errors.add('body', 'data', 'Can\'t update complaint in current status')
            self.request.errors.status = 403
            return
        complaint_data = filter_data(self.request.validated['data'])
        if complaint_data:
            if complaint_data.get('status', '') == 'cancelled':
                self.request.errors.add('body', 'data', 'Can\'t cancel complaint')
                self.request.errors.status = 403
                return
            src = tender.serialize("plain")
            complaint.import_data(apply_data_patch(complaint.serialize(), complaint_data))
            if complaint.status == 'satisfied':
                award = self.request.validated['award']
                awards = self.request.validated['awards']
                if tender.status == 'active.awarded':
                    tender.status = 'active.qualification'
                    tender.awardPeriod.endDate = None
                if award.status == 'unsuccessful':
                    for i in awards[awards.index(award):]:
                        if i.status == 'pending':
                            i.status = 'cancelled'
                            for j in i.complaints:
                                if i.status == 'pending':
                                    i.status = 'cancelled'
                award.status = 'cancelled'
                unsuccessful_awards = [i.bid_id for i in awards if i.status == 'unsuccessful']
                bids = [i for i in sorted(tender.bids, key=lambda i: (i.value.amount, i.date)) if i.id not in unsuccessful_awards]
                if bids:<commit_msg>Fixed canceling tender and award<commit_after># -*- coding: utf-8 -*-
from cornice.resource import resource, view
from openprocurement.api.models import Award, Complaint, STAND_STILL_TIME, get_now
from openprocurement.api.utils import (
    apply_data_patch,
    filter_data,
    save_tender,
)
from openprocurement.api.validation import (
    validate_complaint_data,
    validate_patch_complaint_data,
    validate_tender_award_complaint_exists,
    validate_tender_award_exists_by_award_id,
)


@resource(name='Tender Award Complaints',
          collection_path='/tenders/{tender_id}/awards/{award_id}/complaints',
          path='/tenders/{tender_id}/awards/{award_id}/complaints/{id}',
          description="Tender award complaints")
class TenderAwardComplaintResource(object):

    def __init__(self, request):
        self.request = request
        self.db = request.registry.db

    @view(content_type="application/json", validators=(validate_complaint_data, validate_tender_award_exists_by_award_id), renderer='json')
    def collection_post(self):
        """Post a complaint for award
        """
        tender = self.request.validated['tender']
        if tender.status not in ['active.qualification', 'active.awarded']:
            self.request.errors.add('body', 'data', 'Can\'t add complaint in current tender status')
            self.request.errors.status = 403
            return
        complaint_data = filter_data(self.request.validated['data'], fields=['id', 'date', 'status'])
        complaint = Complaint(complaint_data)
        src = tender.serialize("plain")
        self.request.validated['award'].complaints.append(complaint)
        save_tender(tender, src, self.request)
        self.request.response.status = 201
        self.request.response.headers['Location'] = self.request.route_url('Tender Award Complaints', tender_id=tender.id, award_id=self.request.validated['award_id'], id=complaint['id'])
        return {'data': complaint.serialize("view")}

    @view(renderer='json', validators=(validate_tender_award_exists_by_award_id,))
    def collection_get(self):
        """List complaints for award
        """
        return {'data': [i.serialize("view") for i in self.request.validated['award'].complaints]}

    @view(renderer='json', validators=(validate_tender_award_complaint_exists,))
    def get(self):
        """Retrieving the complaint for award
        """
        return {'data': self.request.validated['complaint'].serialize("view")}

    @view(content_type="application/json", validators=(validate_patch_complaint_data, validate_tender_award_complaint_exists), renderer='json')
    def patch(self):
        """Post a complaint resolution for award
        """
        tender = self.request.validated['tender']
        if tender.status not in ['active.qualification', 'active.awarded']:
            self.request.errors.add('body', 'data', 'Can\'t update complaint in current tender status')
            self.request.errors.status = 403
            return
        complaint = self.request.validated['complaint']
        if complaint.status != 'accepted':
            self.request.errors.add('body', 'data', 'Can\'t update complaint in current status')
            self.request.errors.status = 403
            return
        complaint_data = filter_data(self.request.validated['data'])
        if complaint_data:
            if complaint_data.get('status', '') == 'cancelled':
                self.request.errors.add('body', 'data', 'Can\'t cancel complaint')
                self.request.errors.status = 403
                return
            src = tender.serialize("plain")
            complaint.import_data(apply_data_patch(complaint.serialize(), complaint_data))
            if complaint.status == 'satisfied':
                award = self.request.validated['award']
                awards = self.request.validated['awards']
                if tender.status == 'active.awarded':
                    tender.status = 'active.qualification'
                    tender.awardPeriod.endDate = None
                if award.status == 'unsuccessful':
                    for i in awards[awards.index(award):]:
                        i.status = 'cancelled'
                        for j in i.complaints:
                            if i.status == 'pending':
                                i.status = 'cancelled'
                award.status = 'cancelled'
                unsuccessful_awards = [i.bid_id for i in awards if i.status == 'unsuccessful']
                bids = [i for i in sorted(tender.bids, key=lambda i: (i.value.amount, i.date)) if i.id not in unsuccessful_awards]
                if bids:
{'fraction_non_alphanumeric': 0.11991022763706316, 'fraction_numerical': 0.003419899540450999, 'mean_word_length': 2.6698039215686276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5037586', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2569, 'n_words': 516}