<commit_before>import json
import pytz
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from django.utils.dateparse import parse_datetime

from corehq import toggles
from corehq.apps.domain.decorators import login_or_digest_or_basic_or_apikey
from dimagi.utils.web import json_response

from custom.enikshay.integrations.ninetyninedots.exceptions import AdherenceException
from custom.enikshay.integrations.ninetyninedots.utils import (
    create_adherence_cases,
    update_adherence_confidence_level,
    update_default_confidence_level,
)


@toggles.ENIKSHAY_INTEGRATIONS.required_decorator()
@login_or_digest_or_basic_or_apikey()
@require_POST
@csrf_exempt
def update_patient_adherence(request, domain):
    request_json = json.loads(request.body)
    beneficiary_id = request_json.get('beneficiary_id')
    adherence_values = request_json.get('adherences')

    try:
        validate_beneficiary_id(beneficiary_id)
        validate_adherence_values(adherence_values)
        create_adherence_cases(domain, beneficiary_id, adherence_values, adherence_source="99DOTS")
    except AdherenceException as e:
        return json_response({"error": e.message}, status_code=400)

    return json_response({"success": "Patient adherences updated."})


@toggles.ENIKSHAY_INTEGRATIONS.required_decorator()
@login_or_digest_or_basic_or_apikey()
@require_POST
@csrf_exempt
def update_adherence_confidence(request, domain):
    request_json = json.loads(request.body)
    beneficiary_id = request_json.get('beneficiary_id')
    start_date = request_json.get('start_date')
    end_date = request_json.get('end_date')
    confidence_level = request_json.get('confidence_level')

    try:
        validate_beneficiary_id(beneficiary_id)
        validate_dates(start_date, end_date)
        validate_confidence_level(confidence_level)
        update_adherence_confidence_level(
            domain=domain,
            person_id=beneficiary_id,
            start_date=parse_datetime(start_date),
            end_date=parse_datetime(end_date),
            new_confidence=confidence_level
        )
    except AdherenceException as e:
        return json_response({"error": e.message}, status_code=400)

    return json_response({"success": "Patient adherences updated."})


@toggles.ENIKSHAY_INTEGRATIONS.required_decorator()
@login_or_digest_or_basic_or_apikey()
@require_POST
@csrf_exempt
def update_default_confidence(request, domain):
    request_json = json.loads(request.body)
    beneficiary_id = request_json.get('beneficiary_id')
    confidence_level = request_json.get('confidence_level')
    try:
        validate_beneficiary_id(beneficiary_id)<commit_msg>Catch malformed JSON<commit_after>import json
import pytz
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from django.utils.dateparse import parse_datetime

from corehq import toggles
from corehq.apps.domain.decorators import login_or_digest_or_basic_or_apikey
from dimagi.utils.web import json_response

from custom.enikshay.integrations.ninetyninedots.exceptions import AdherenceException
from custom.enikshay.integrations.ninetyninedots.utils import (
    create_adherence_cases,
    update_adherence_confidence_level,
    update_default_confidence_level,
)


@toggles.ENIKSHAY_INTEGRATIONS.required_decorator()
@login_or_digest_or_basic_or_apikey()
@require_POST
@csrf_exempt
def update_patient_adherence(request, domain):
    try:
        request_json = json.loads(request.body)
    except ValueError:
        return json_response({"error": "Malformed JSON"}, status_code=400)

    beneficiary_id = request_json.get('beneficiary_id')
    adherence_values = request_json.get('adherences')

    try:
        validate_beneficiary_id(beneficiary_id)
        validate_adherence_values(adherence_values)
        create_adherence_cases(domain, beneficiary_id, adherence_values, adherence_source="99DOTS")
    except AdherenceException as e:
        return json_response({"error": e.message}, status_code=400)

    return json_response({"success": "Patient adherences updated."})


@toggles.ENIKSHAY_INTEGRATIONS.required_decorator()
@login_or_digest_or_basic_or_apikey()
@require_POST
@csrf_exempt
def update_adherence_confidence(request, domain):
    try:
        request_json = json.loads(request.body)
    except ValueError:
        return json_response({"error": "Malformed JSON"}, status_code=400)
    beneficiary_id = request_json.get('beneficiary_id')
    start_date = request_json.get('start_date')
    end_date = request_json.get('end_date')
    confidence_level = request_json.get('confidence_level')

    try:
        validate_beneficiary_id(beneficiary_id)
        validate_dates(start_date, end_date)
        validate_confidence_level(confidence_level)
        update_adherence_confidence_level(
            domain=domain,
            person_id=beneficiary_id,
            start_date=parse_datetime(start_date),
            end_date=parse_datetime(end_date),
            new_confidence=confidence_level
        )
    except AdherenceException as e:
        return json_response({"error": e.message}, status_code=400)

    return json_response({"success": "Patient adherences updated."})


@toggles.ENIKSHAY_INTEGRATIONS.required_decorator()
@login_or_digest_or_basic_or_apikey()
@require_POST
@csrf_exempt
def update_default_confidence(request, domain):
    try:
        request_json = json.loads(request.body)
    except ValueError:
        return json_response({"error": "Malformed JSON"}, status_code=400)

    beneficiary_id = request_json.get('beneficiary_id')
    confidence_level = request_json.get('confidence_level')

    try:
        validate_beneficiary_id(beneficiary_id)
{'fraction_non_alphanumeric': 0.08138122505639424, 'fraction_numerical': 0.004338018393197987, 'mean_word_length': 5.080168776371308, 'pattern_counts': {'":': 11, '<': 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': '5558274', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1774, 'n_words': 279}