<commit_before>import datetime
import uuid
from xml.etree import ElementTree
from couchdbkit import ResourceNotFound
from dimagi.utils.couch.database import iter_docs
from casexml.apps.case.mock import CaseBlock
from casexml.apps.case.models import CommCareCase
from dimagi.utils.parsing import json_format_datetime
from django.template.loader import render_to_string
from casexml.apps.case.xml import V2
from casexml.apps.phone.caselogic import get_related_cases
from corehq.apps.hqcase.exceptions import CaseAssignmentError

from corehq.apps.receiverwrapper import submit_form_locally

ALLOWED_CASE_IDENTIFIER_TYPES = [
    "contact_phone_number",
    "external_id",
]


def submit_case_blocks(case_blocks, domain, username="system", user_id="",
                       xmlns='http://commcarehq.org/case'):
    now = json_format_datetime(datetime.datetime.utcnow())
    if not isinstance(case_blocks, basestring):
        case_blocks = ''.join(case_blocks)
    form_xml = render_to_string('hqcase/xml/case_block.xml', {
        'xmlns': xmlns,
        'case_block': case_blocks,
        'time': now,
        'uid': uuid.uuid4().hex,
        'username': username,
        'user_id': user_id,
    })
    submit_form_locally(
        instance=form_xml,
        domain=domain,
    )


def get_case_wrapper(data):
    from corehq.apps.commtrack.util import get_case_wrapper as commtrack_wrapper
    def pact_wrapper(data):
        if data['domain'] == 'pact' and data['type'] == 'cc_path_client':
            from pact.models import PactPatientCase
            return PactPatientCase

    wrapper_funcs = [pact_wrapper, commtrack_wrapper]

    wrapper = None
    for wf in wrapper_funcs:
        wrapper = wf(data)
        if wrapper is not None:
            break
    return wrapper


def get_case_by_domain_hq_user_id(domain, user_id, include_docs=False):
    """
    Get the 'user case' for user_id. User cases are part of the call center feature.
    """
    return CommCareCase.view('hqcase/by_domain_hq_user_id',
                         key=[domain, user_id],
                         reduce=False,<commit_msg>have submit_case_blocks return the submitted form id<commit_after>import datetime
import uuid
from xml.etree import ElementTree
from couchdbkit import ResourceNotFound
from dimagi.utils.couch.database import iter_docs
from casexml.apps.case.mock import CaseBlock
from casexml.apps.case.models import CommCareCase
from dimagi.utils.parsing import json_format_datetime
from django.template.loader import render_to_string
from casexml.apps.case.xml import V2
from casexml.apps.phone.caselogic import get_related_cases
from corehq.apps.hqcase.exceptions import CaseAssignmentError

from corehq.apps.receiverwrapper import submit_form_locally

ALLOWED_CASE_IDENTIFIER_TYPES = [
    "contact_phone_number",
    "external_id",
]


def submit_case_blocks(case_blocks, domain, username="system", user_id="",
                       xmlns='http://commcarehq.org/case'):
    """
    Submits casexml in a manner similar to how they would be submitted from a phone.

    returns the UID of the resulting form.
    """
    now = json_format_datetime(datetime.datetime.utcnow())
    if not isinstance(case_blocks, basestring):
        case_blocks = ''.join(case_blocks)
    form_id = uuid.uuid4().hex
    form_xml = render_to_string('hqcase/xml/case_block.xml', {
        'xmlns': xmlns,
        'case_block': case_blocks,
        'time': now,
        'uid': form_id,
        'username': username,
        'user_id': user_id,
    })
    submit_form_locally(
        instance=form_xml,
        domain=domain,
    )
    return form_id


def get_case_wrapper(data):
    from corehq.apps.commtrack.util import get_case_wrapper as commtrack_wrapper
    def pact_wrapper(data):
        if data['domain'] == 'pact' and data['type'] == 'cc_path_client':
            from pact.models import PactPatientCase
            return PactPatientCase

    wrapper_funcs = [pact_wrapper, commtrack_wrapper]

    wrapper = None
    for wf in wrapper_funcs:
        wrapper = wf(data)
        if wrapper is not None:
            break
    return wrapper


def get_case_by_domain_hq_user_id(domain, user_id, include_docs=False):
    """
    Get the 'user case' for user_id. User cases are part of the call center feature.
    """
    return CommCareCase.view('hqcase/by_domain_hq_user_id',
                         key=[domain, user_id],
                         reduce=False,
{'fraction_non_alphanumeric': 0.08559874185576274, 'fraction_numerical': 0.0008986744551786115, 'mean_word_length': 3.235965746907707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 27}, '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': '2104397', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1356, 'n_words': 325}