示例#1
0
def create_repeat_records(repeater_cls, payload):
    domain = payload.domain
    if domain:
        repeaters = repeater_cls.by_domain(domain)
        for repeater in repeaters:
            repeater.register(payload)


def form_processing(sender, xform, **kwargs):
    should_update = scrub_meta(sender, xform)
    should_update = add_domain(sender, xform) or should_update
    should_update = add_export_tag(sender, xform) or should_update
    should_update = add_app_id(sender, xform) or should_update
    # form received saves the doc right after calling this signal,
    # so we don't have to save, though would need to here if that changes
    return should_update


def error_processing(sender, xform, **kwargs):
    should_update = add_domain(sender, xform)
    should_update = add_app_id(sender, xform) or should_update
    return should_update


form_received.connect(form_processing)
submission_error_received.connect(error_processing)

successful_form_received.connect(create_form_repeat_records)
successful_form_received.connect(create_short_form_repeat_records)
case_post_save.connect(create_case_repeat_records)
示例#2
0
    create_repeat_records(CaseRepeater, case)


def create_short_form_repeat_records(sender, xform, **kwargs):
    from corehq.apps.receiverwrapper.models import ShortFormRepeater

    xform.domain = _get_domain(xform)
    create_repeat_records(ShortFormRepeater, xform)


def create_repeat_records(repeater_cls, payload):
    domain = payload.domain
    if domain:
        repeaters = repeater_cls.by_domain(domain)
        for repeater in repeaters:
            repeater.register(payload)


form_received.connect(scrub_meta)
form_received.connect(add_domain)
form_received.connect(add_export_tag)
form_received.connect(add_app_id)

submission_error_received.connect(add_domain)
submission_error_received.connect(add_app_id)

successful_form_received.connect(create_form_repeat_records)
successful_form_received.connect(create_short_form_repeat_records)
case_post_save.connect(create_case_repeat_records)
示例#3
0
Question: is it possible to receive registration data from the phone after Case 3?
If so, we need to check for a user created via Case 3 and link them to this account
automatically
"""

def create_user_from_commcare_registration(sender, xform, **kwargs):
    """
    # this comes in as xml that looks like:
    # <n0:registration xmlns:n0="openrosa.org/user-registration">
    # <username>user</username>
    # <password>pw</password>
    # <uuid>MTBZJTDO3SCT2ONXAQ88WM0CH</uuid>
    # <date>2008-01-07</date>
    # <registering_phone_id>NRPHIOUSVEA215AJL8FFKGTVR</registering_phone_id>
    # <user_data> ... some custom stuff </user_data>
    """
    if xform.xmlns not in VALID_USER_REGISTRATION_XMLNSES:
        return False
    
    try:
        couch_user, created = CommCareUser.create_or_update_from_xform(xform)
    except Exception, e:
        #import traceback, sys
        #exc_type, exc_value, exc_traceback = sys.exc_info()
        #traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        logging.exception(e)
        raise
    return ReceiverResult(xml.get_response(couch_user, created), Certainty.CERTAIN)

successful_form_received.connect(create_user_from_commcare_registration)
示例#4
0
        if app and hasattr(app, "langs"):
            try:
                lang = xform.openrosa_headers[OPENROSA_ACCEPT_LANGUAGE]
            except (AttributeError, KeyError):
                lang = "default"
            if lang == "default":
                lang = app.build_langs[0] if app.build_langs else None
            message = app.success_message.get(lang)
            if message:
                success_message = SuccessMessage(message, userID, domain=domain, tz=timedelta(hours=0)).render()
                return ReceiverResult(
                    xml.get_simple_response_xml(success_message, nature=ResponseNature.SUBMIT_SUCCESS), Certainty.STRONG
                )


def create_app_structure_repeat_records(sender, application, **kwargs):
    from corehq.apps.app_manager.models import AppStructureRepeater

    domain = application.domain
    if domain:
        repeaters = AppStructureRepeater.by_domain(domain)
        for repeater in repeaters:
            repeater.register(application)


successful_form_received.connect(get_custom_response_message)

app_post_save = Signal(providing_args=["application"])

app_post_save.connect(create_app_structure_repeat_records)
示例#5
0
    """
    if xform.metadata and xform.metadata.userID:
        userID = xform.metadata.userID
        xmlns = xform.form.get('@xmlns')
        domain = xform.domain

        try:
            app = get_app(domain, xform.app_id)
        except Exception:
            app = Application.get_by_xmlns(domain, xmlns)

        if app and hasattr(app, 'langs'):
            try:
                lang = xform.openrosa_headers[OPENROSA_ACCEPT_LANGUAGE]
            except (AttributeError, KeyError):
                lang = "default"
            if lang == "default":
                lang = app.build_langs[0] if app.build_langs else None
            message = app.success_message.get(lang)
            if message:
                success_message = SuccessMessage(
                    message, userID, domain=domain,
                    tz=timedelta(hours=0)).render()
                return ReceiverResult(
                    xml.get_simple_response_xml(
                        success_message, nature=ResponseNature.SUBMIT_SUCCESS),
                    Certainty.STRONG)


successful_form_received.connect(get_custom_response_message)
示例#6
0
"""


def create_user_from_commcare_registration(sender, xform, **kwargs):
    """
    # this comes in as xml that looks like:
    # <n0:registration xmlns:n0="openrosa.org/user-registration">
    # <username>user</username>
    # <password>pw</password>
    # <uuid>MTBZJTDO3SCT2ONXAQ88WM0CH</uuid>
    # <date>2008-01-07</date>
    # <registering_phone_id>NRPHIOUSVEA215AJL8FFKGTVR</registering_phone_id>
    # <user_data> ... some custom stuff </user_data>
    """
    if xform.xmlns not in VALID_USER_REGISTRATION_XMLNSES:
        return False

    try:
        couch_user, created = CommCareUser.create_or_update_from_xform(xform)
    except Exception, e:
        #import traceback, sys
        #exc_type, exc_value, exc_traceback = sys.exc_info()
        #traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        logging.exception(e)
        raise
    return ReceiverResult(xml.get_response(couch_user, created),
                          Certainty.CERTAIN)


successful_form_received.connect(create_user_from_commcare_registration)
示例#7
0
    
    def forms_submitted_today_count(user):
        today = datetime.today()
        startkey = [user, today.year, today.month - 1, today.day]
        endkey = [user, today.year, today.month - 1, today.day, {}]
        forms_submitted_today = get_db().view("couchforms/by_user", 
                                              startkey=startkey, 
                                              endkey=endkey).one()
        return forms_submitted_today["value"] if forms_submitted_today else "at least 1"
        
    if xform.metadata and xform.metadata.userID:
        if xform.metadata.username == "demo_user":
            message = "Thanks for submitting from demo mode!"
        else: 
            to = ", %s" % xform.metadata.username if xform.metadata.username else ""
            message = ("Thanks for submitting%s.  We have received %s forms from "
                       "you today (%s forms all time)") % \
                       (to,
                        forms_submitted_today_count(xform.metadata.userID), 
                        forms_submitted_count(xform.metadata.userID))
        return ReceiverResult(xml.get_simple_response_xml(
            message, nature=ResponseNature.SUBMIT_SUCCESS), Certainty.MILD)
            
    else:
        return ReceiverResult(xml.get_simple_response_xml(
            "Thanks for submitting!", ResponseNature.SUBMIT_SUCCESS), 
            Certainty.MILD)


successful_form_received.connect(send_default_response)
示例#8
0
def raise_events(xform, cases):
    supply_points = [SupplyPointCase.wrap(c._doc) for c in cases if c.type == const.SUPPLY_POINT_CASE_TYPE]
    case_updates = get_case_updates(xform)
    for sp in supply_points:
        created = any(filter(lambda update: update.id == sp._id and update.creates_case(), case_updates))
        supply_point_modified.send(sender=None, supply_point=sp, created=created)
    requisition_cases = [RequisitionCase.wrap(c._doc) for c in cases if c.type == const.REQUISITION_CASE_TYPE]
    if requisition_cases and requisition_cases[0].requisition_status == RequisitionStatus.APPROVED:
        requisition_approved.send(sender=None, requisitions=requisition_cases)
    if requisition_cases and requisition_cases[0].requisition_status == RequisitionStatus.RECEIVED:
        requisition_receipt.send(sender=None, requisitions=requisition_cases)

    if requisition_cases and requisition_cases[0].requisition_status == RequisitionStatus.REQUESTED:
        requisition_modified.send(sender=None, cases=requisition_cases)

def commtrack_processing(sender, xform, cases, **kwargs):
    if is_commtrack_form(xform):
        attach_locations(xform, cases)
        send_notifications(xform, cases)
        raise_events(xform, cases)


cases_received.connect(commtrack_processing)


def bootstrap_commtrack_settings_if_necessary_signal(sender, **kwargs):
    bootstrap_commtrack_settings_if_necessary(kwargs['domain'])

commcare_domain_post_save.connect(bootstrap_commtrack_settings_if_necessary_signal)
successful_form_received.connect(process_stock_signal_catcher)
示例#9
0
            case.domain = domain
            if domain and hasattr(case, 'type'):
                case['#export_tag'] = ["domain", "type"]
            return case
        cases = [attach_domain(case) for case in cases]

    # HACK -- figure out how to do this more properly
    # todo: create a pillow for this
    if cases:
        case = cases[0]
        if case.location_ is not None:
            # should probably store this in computed_
            xform.location_ = list(case.location_)

    # handle updating the sync records for apps that use sync mode
    if hasattr(xform, "last_sync_token") and xform.last_sync_token:
        relevant_log = SyncLog.get(xform.last_sync_token)
        relevant_log.update_phone_lists(xform, cases)

    # set flags for indicator pillows and save
    xform.initial_processing_complete = True
    xform.save()
    for case in cases:
        case.initial_processing_complete = True
        case.save()


successful_form_received.connect(process_cases)

case_post_save = Signal(providing_args=["case"])
示例#10
0
#placeholder for doing blocking vs. async via celery
BLOCKING = True

def process_dots_submission(sender, xform, **kwargs):
    from pact.tasks import recalculate_dots_data, eval_dots_block
    try:
        if xform.xmlns != "http://dev.commcarehq.org/pact/dots_form":
            return

        #grrr, if we were on celery 3.0, we could do this!
        #        chain = eval_dots_block.s(xform.to_json()) | recalculate_dots_data.s(case_id)
        #        chain()

        #2.4.5 subtasking:
        eval_dots_block(xform.to_json())
        case_id = get_case_id(xform)
        #get user from xform
        user_id = xform.metadata.userID
        cc_user = CouchUser.get_by_user_id(user_id)
        last_sync_token = getattr(xform, 'last_sync_token', None)
        recalculate_dots_data(case_id, cc_user, sync_token=last_sync_token)

    except Exception, ex:
        tb = traceback.format_exc()
        notify_exception(None, message="Error processing PACT DOT submission due to an unknown error: %s\n\tTraceback: %s" % (ex, tb))

#xform_saved.connect(process_dots_submission)
successful_form_received.connect(process_dots_submission)