<commit_before>from __future__ import absolute_import
from __future__ import print_function

from corehq.apps.users.models import CommCareUser

from corehq.apps.es import queries

from custom.enikshay.case_utils import (
    CASE_TYPE_EPISODE,
    CASE_TYPE_ADHERENCE,
    get_person_case_from_episode,
    get_episode_case_from_adherence,
    get_adherence_cases_from_episode,
)
from custom.enikshay.const import (
    ENROLLED_IN_PRIVATE,
)
from custom.enikshay.management.commands.base_data_dump import BaseDataDump

DOMAIN = "enikshay"


class Command(BaseDataDump):
    """
    15. Adherence Details
    https://docs.google.com/spreadsheets/d/1OPp0oFlizDnIyrn7Eiv11vUp8IBmc73hES7qqT-mKKA/edit#gid=2059461644
    """
    TASK_NAME = "15_adherence_details"
    INPUT_FILE_NAME = "data_dumps_adherence_details.csv"

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.case_type = CASE_TYPE_EPISODE

    def get_case_ids_query(self, case_type):
        """
        All open and closed episode cases 1) whose host/host/host = a person case (open or closed)
        with person.dataset = 'real' and person.enrolled_in_private != 'true'
        """
        return (self.case_search_instance
                .case_type(case_type)
                .case_property_query(ENROLLED_IN_PRIVATE, 'true', clause=queries.MUST_NOT)
                .case_property_filter("episode_type", ["confirmed_dstb", "confirmed_drtb"])
                )

    def include_case_in_dump(self, episode):
        assert episode.type == CASE_TYPE_EPISODE
        person = self.get_person(episode)
        return (
            person and
            person.get_case_property('dataset') == 'real' and
            person.get_case_property(ENROLLED_IN_PRIVATE) != 'true'
        )

    def cases_to_dump(self, episode_case):
        assert episode_case.type == CASE_TYPE_EPISODE
        return get_adherence_cases_from_episode(DOMAIN, episode_case.case_id)

    def get_custom_value(self, column_name, adherence):
        assert adherence.type == CASE_TYPE_ADHERENCE, \
            "Unexpected Case type instead of %s" % CASE_TYPE_ADHERENCE
        if column_name == "Date of Creation of Presumptive TB Episode":
            return adherence.opened_on
        elif column_name == "Created by Username":
            user_id = None
            try:
                user_id = adherence.opened_by
                return CommCareUser.get_by_user_id(user_id, DOMAIN).username
            except Exception as e:
                return Exception("Could not get username. case opened by %s, %s" % (user_id, e))
        elif column_name == "Created by User ID":
            return adherence.opened_by
        return Exception("unknown custom column %s" % column_name)

    def get_person(self, adherence):
        assert adherence.type in [CASE_TYPE_ADHERENCE, CASE_TYPE_EPISODE], \
            "Unexpected Case type instead of %s and %s" % (CASE_TYPE_ADHERENCE, CASE_TYPE_EPISODE)

        if 'person' not in self.context:<commit_msg>update custom column name [skip ci]<commit_after>from __future__ import absolute_import
from __future__ import print_function

from corehq.apps.users.models import CommCareUser

from corehq.apps.es import queries

from custom.enikshay.case_utils import (
    CASE_TYPE_EPISODE,
    CASE_TYPE_ADHERENCE,
    get_person_case_from_episode,
    get_episode_case_from_adherence,
    get_adherence_cases_from_episode,
)
from custom.enikshay.const import (
    ENROLLED_IN_PRIVATE,
)
from custom.enikshay.management.commands.base_data_dump import BaseDataDump

DOMAIN = "enikshay"


class Command(BaseDataDump):
    """
    15. Adherence Details
    https://docs.google.com/spreadsheets/d/1OPp0oFlizDnIyrn7Eiv11vUp8IBmc73hES7qqT-mKKA/edit#gid=2059461644
    """
    TASK_NAME = "15_adherence_details"
    INPUT_FILE_NAME = "data_dumps_adherence_details.csv"

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.case_type = CASE_TYPE_EPISODE

    def get_case_ids_query(self, case_type):
        """
        All open and closed episode cases 1) whose host/host/host = a person case (open or closed)
        with person.dataset = 'real' and person.enrolled_in_private != 'true'
        """
        return (self.case_search_instance
                .case_type(case_type)
                .case_property_query(ENROLLED_IN_PRIVATE, 'true', clause=queries.MUST_NOT)
                .case_property_filter("episode_type", ["confirmed_dstb", "confirmed_drtb"])
                )

    def include_case_in_dump(self, episode):
        assert episode.type == CASE_TYPE_EPISODE
        person = self.get_person(episode)
        return (
            person and
            person.get_case_property('dataset') == 'real' and
            person.get_case_property(ENROLLED_IN_PRIVATE) != 'true'
        )

    def cases_to_dump(self, episode_case):
        assert episode_case.type == CASE_TYPE_EPISODE
        return get_adherence_cases_from_episode(DOMAIN, episode_case.case_id)

    def get_custom_value(self, column_name, adherence):
        assert adherence.type == CASE_TYPE_ADHERENCE, \
            "Unexpected Case type instead of %s" % CASE_TYPE_ADHERENCE
        if column_name == "Date of Creation of Adherence Case":
            return adherence.opened_on
        elif column_name == "Created by Username":
            user_id = None
            try:
                user_id = adherence.opened_by
                return CommCareUser.get_by_user_id(user_id, DOMAIN).username
            except Exception as e:
                return Exception("Could not get username. case opened by %s, %s" % (user_id, e))
        elif column_name == "Created by User ID":
            return adherence.opened_by
        return Exception("unknown custom column %s" % column_name)

    def get_person(self, adherence):
        assert adherence.type in [CASE_TYPE_ADHERENCE, CASE_TYPE_EPISODE], \
            "Unexpected Case type instead of %s and %s" % (CASE_TYPE_ADHERENCE, CASE_TYPE_EPISODE)

        if 'person' not in self.context:
{'fraction_non_alphanumeric': 0.0828693088765149, 'fraction_numerical': 0.007861120209629872, 'mean_word_length': 2.978501628664495, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6272551', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1888, 'n_words': 407}