<commit_before>from django.conf import settings
from corehq.apps.commtrack.const import RequisitionActions
from corehq.apps.domain.models import Domain
from casexml.apps.case.models import CommCareCase
from corehq.apps.users.models import CommCareUser
from corehq.apps.locations.models import Location
from corehq.apps.commtrack import stockreport, const
from corehq.apps.sms.api import send_sms_to_verified_number
from lxml import etree
import logging
from dimagi.utils.couch.loosechange import map_reduce
from dimagi.utils.parsing import json_format_datetime
from datetime import datetime
from helpers import make_supply_point_product
from corehq.apps.commtrack.util import get_supply_point
from corehq.apps.commtrack.models import Product, CommtrackConfig

logger = logging.getLogger('commtrack.sms')

class SMSError(RuntimeError):
    pass

def handle(verified_contact, text, msg=None):
    """top-level handler for incoming stock report messages"""
    domain = Domain.get_by_name(verified_contact.domain)
    if not domain.commtrack_enabled:
        return False

    try:
        data = StockReportParser(domain, verified_contact).parse(text.lower())
        if not data:
            return False
    except Exception, e: # todo: should we only trap SMSErrors?
        if settings.UNIT_TESTING:
            raise
        send_sms_to_verified_number(verified_contact, 'problem with stock report: %s' % str(e))
        return True

    process(domain.name, data)
    send_confirmation(verified_contact, data)
    return True

def process(domain, data):
    logger.debug(data)
    inst_xml = to_instance(data)
    logger.debug(inst_xml)
    
    stockreport.process(domain, inst_xml)

class StockReportParser(object):
    """a helper object for parsing raw stock report texts"""

    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner
        if isinstance(u, CommCareUser):
            # currently only support one location on the UI
            linked_loc = u.location
            if linked_loc:
                self.location = get_supply_point(self.domain.name, loc=linked_loc)['case']

        self.C = domain.commtrack_settings
<commit_msg>Fix bug causing error on CT sms parsing<commit_after>from django.conf import settings
from corehq.apps.commtrack.const import RequisitionActions
from corehq.apps.domain.models import Domain
from casexml.apps.case.models import CommCareCase
from corehq.apps.locations.models import Location
from corehq.apps.commtrack import stockreport, const
from corehq.apps.sms.api import send_sms_to_verified_number
from lxml import etree
import logging
from dimagi.utils.couch.loosechange import map_reduce
from dimagi.utils.parsing import json_format_datetime
from datetime import datetime
from helpers import make_supply_point_product
from corehq.apps.commtrack.util import get_supply_point
from corehq.apps.commtrack.models import Product, CommtrackConfig, CommTrackUser

logger = logging.getLogger('commtrack.sms')

class SMSError(RuntimeError):
    pass

def handle(verified_contact, text, msg=None):
    """top-level handler for incoming stock report messages"""
    domain = Domain.get_by_name(verified_contact.domain)
    if not domain.commtrack_enabled:
        return False

    try:
        data = StockReportParser(domain, verified_contact).parse(text.lower())
        if not data:
            return False
    except Exception, e: # todo: should we only trap SMSErrors?
        if settings.UNIT_TESTING:
            raise
        send_sms_to_verified_number(verified_contact, 'problem with stock report: %s' % str(e))
        return True

    process(domain.name, data)
    send_confirmation(verified_contact, data)
    return True

def process(domain, data):
    logger.debug(data)
    inst_xml = to_instance(data)
    logger.debug(inst_xml)
    
    stockreport.process(domain, inst_xml)

class StockReportParser(object):
    """a helper object for parsing raw stock report texts"""

    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner
        if domain.commtrack_enabled:
            # currently only support one location on the UI
            linked_loc = CommTrackUser.wrap(u.to_json()).location
            if linked_loc:
                self.location = get_supply_point(self.domain.name, loc=linked_loc)['case']

        self.C = domain.commtrack_settings

{'fraction_non_alphanumeric': 0.0742397137745975, 'fraction_numerical': 0.0, 'mean_word_length': 3.5365111561866125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '3368556', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1282, 'n_words': 353}