<commit_before>from django.utils.translation import ugettext_lazy as _

from corehq import toggles
from corehq.apps.cowin.repeater_generators import (
    BeneficiaryRegistrationPayloadGenerator,
    BeneficiaryVaccinationPayloadGenerator,
)
from corehq.apps.hqcase.utils import update_case
from corehq.motech.repeaters.models import CaseRepeater


class BaseCOWINRepeater(CaseRepeater):
    class Meta:
        app_label = 'repeaters'

    @classmethod
    def available_for_domain(cls, domain):
        return toggles.COWIN_INTEGRATION.enabled(domain)

    def get_headers(self, repeat_record):
        headers = super().get_headers(repeat_record)

        headers.update({
            'Accept-Language': 'en_US',
            'User-Agent': '',
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        })
        return headers


class BeneficiaryRegistrationRepeater(BaseCOWINRepeater):
    payload_generator_classes = (BeneficiaryRegistrationPayloadGenerator,)
    friendly_name = _("Register beneficiaries on COWIN")

    def get_headers(self, repeat_record):
        headers = super(BeneficiaryRegistrationRepeater, self).get_headers(repeat_record)
        headers.update({
            'X-Api-Key': self.connection_settings.plaintext_password,
        })
        return headers

    def handle_response(self, response, repeat_record):
        attempt = super().handle_response(response, repeat_record)
        if response.status_code == 200:
            beneficiary_reference_id = response.json()['beneficiary_reference_id']
            update_case(self.domain, repeat_record.payload_id, case_properties={
                'cowin_id': beneficiary_reference_id
            })
        return attempt

    def allowed_to_forward(self, case):
        allowed = super().allowed_to_forward(case)
        if allowed:
            return not bool(case.get_case_property('cowin_id'))
        return allowed


class BeneficiaryVaccinationRepeater(BaseCOWINRepeater):
    payload_generator_classes = (BeneficiaryVaccinationPayloadGenerator,)
    friendly_name = _("Update vaccination for beneficiaries on COWIN")

    def get_headers(self, repeat_record):
        headers = super(BeneficiaryVaccinationRepeater, self).get_headers(repeat_record)
        headers.update({
            'X-Api-Key': self.connection_settings.plaintext_password,
        })
        return headers

    def allowed_to_forward(self, case):
        allowed = super().allowed_to_forward(case)
        if allowed:
            return bool(case.get_case_property('cowin_id'))
        return allowed<commit_msg>dedup<commit_after>from django.utils.translation import ugettext_lazy as _

from corehq import toggles
from corehq.apps.cowin.repeater_generators import (
    BeneficiaryRegistrationPayloadGenerator,
    BeneficiaryVaccinationPayloadGenerator,
)
from corehq.apps.hqcase.utils import update_case
from corehq.motech.repeaters.models import CaseRepeater


class BaseCOWINRepeater(CaseRepeater):
    class Meta:
        app_label = 'repeaters'

    @classmethod
    def available_for_domain(cls, domain):
        return toggles.COWIN_INTEGRATION.enabled(domain)

    def get_headers(self, repeat_record):
        headers = super().get_headers(repeat_record)

        headers.update({
            'Accept-Language': 'en_US',
            'User-Agent': '',
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'X-Api-Key': self.connection_settings.plaintext_password,
        })
        return headers


class BeneficiaryRegistrationRepeater(BaseCOWINRepeater):
    payload_generator_classes = (BeneficiaryRegistrationPayloadGenerator,)
    friendly_name = _("Register beneficiaries on COWIN")

    def handle_response(self, response, repeat_record):
        attempt = super().handle_response(response, repeat_record)
        if response.status_code == 200:
            beneficiary_reference_id = response.json()['beneficiary_reference_id']
            update_case(self.domain, repeat_record.payload_id, case_properties={
                'cowin_id': beneficiary_reference_id
            })
        return attempt

    def allowed_to_forward(self, case):
        allowed = super().allowed_to_forward(case)
        if allowed:
            return not bool(case.get_case_property('cowin_id'))
        return allowed


class BeneficiaryVaccinationRepeater(BaseCOWINRepeater):
    payload_generator_classes = (BeneficiaryVaccinationPayloadGenerator,)
    friendly_name = _("Update vaccination for beneficiaries on COWIN")

    def allowed_to_forward(self, case):
        allowed = super().allowed_to_forward(case)
        if allowed:
            return bool(case.get_case_property('cowin_id'))
        return allowed
{'fraction_non_alphanumeric': 0.08478488982161594, 'fraction_numerical': 0.0012591815320041973, 'mean_word_length': 3.3926267281105993, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4307515', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1327, 'n_words': 245}