Exemplo n.º 1
0
def post_case_blocks(case_blocks, form_extras=None, domain=None):
    """
    Post case blocks.

    Extras is used to add runtime attributes to the form before
    sending it off to the case (current use case is sync-token pairing)
    """
    import couchforms
    from corehq.apps.receiverwrapper.util import DefaultAuthContext

    if form_extras is None:
        form_extras = {}

    domain = domain or form_extras.pop('domain', None)
    if getattr(settings, 'UNIT_TESTING', False):
        from casexml.apps.case.tests.util import TEST_DOMAIN_NAME
        domain = domain or TEST_DOMAIN_NAME

    form = make_form_from_case_blocks(case_blocks)

    form_extras['auth_context'] = (form_extras.get('auth_context')
                                   or DefaultAuthContext())
    sp = couchforms.SubmissionPost(instance=form, domain=domain, **form_extras)
    response, xform, cases = sp.run()
    return xform, cases
Exemplo n.º 2
0
def submit_form_locally(instance, domain, **kwargs):
    # intentionally leave these unauth'd for now
    kwargs['auth_context'] = kwargs.get('auth_context') or DefaultAuthContext()
    response = couchforms.SubmissionPost(domain=domain,
                                         instance=instance,
                                         **kwargs).get_response()
    if not 200 <= response.status_code < 300:
        raise LocalSubmissionError('Error submitting (status code %s): %s' % (
            response.status_code,
            response.content,
        ))
    return response
Exemplo n.º 3
0
 def _do_submit(self,
                xml_data,
                dict_attachments,
                sync_token=None,
                date=None):
     """
     RequestFactory submitter - simulates direct submission to server directly (no need to call process case after fact)
     """
     sp = couchforms.SubmissionPost(
         instance=xml_data,
         domain=TEST_DOMAIN_NAME,
         attachments=dict_attachments,
         last_sync_token=sync_token,
         received_on=date,
     )
     response, xform, cases = sp.run()
     self.assertEqual(set(dict_attachments.keys()),
                      set(xform.attachments.keys()))
     [case] = cases
     self.assertEqual(case.case_id, TEST_CASE_ID)
Exemplo n.º 4
0
def _process_form(request,
                  domain,
                  app_id,
                  user_id,
                  authenticated,
                  auth_cls=AuthContext):
    instance, attachments = couchforms.get_instance_and_attachment(request)
    app_id, build_id = get_app_and_build_ids(domain, app_id)
    response = couchforms.SubmissionPost(
        instance=instance,
        attachments=attachments,
        domain=domain,
        app_id=app_id,
        build_id=build_id,
        auth_context=auth_cls(
            domain=domain,
            user_id=user_id,
            authenticated=authenticated,
        ),
        location=couchforms.get_location(request),
        received_on=couchforms.get_received_on(request),
        date_header=couchforms.get_date_header(request),
        path=couchforms.get_path(request),
        submit_ip=couchforms.get_submit_ip(request),
        last_sync_token=couchforms.get_last_sync_token(request),
        openrosa_headers=couchforms.get_openrosa_headers(request),
    ).get_response()
    if response.status_code == 400:
        db_response = get_db('couchlog').save_doc({
            'request':
            unicode(request),
            'response':
            unicode(response),
        })
        logging.error('Status code 400 for a form submission. '
                      'Response is: \n{0}\n'
                      'See couchlog db for more info: {1}'.format(
                          unicode(response),
                          db_response['id'],
                      ))
    return response