def __init__(self, instance=None, attachments=None, auth_context=None, domain=None, app_id=None, build_id=None, path=None, location=None, submit_ip=None, openrosa_headers=None, last_sync_token=None, received_on=None, date_header=None, partial_submission=False): assert domain, domain assert instance, instance assert not isinstance(instance, HttpRequest), instance self.domain = domain self.app_id = app_id self.build_id = build_id # get_location has good default self.location = location or couchforms.get_location() self.received_on = received_on self.date_header = date_header self.submit_ip = submit_ip self.last_sync_token = last_sync_token self.openrosa_headers = openrosa_headers or {} self.instance = instance self.attachments = attachments or {} self.auth_context = auth_context or DefaultAuthContext() self.path = path self.interface = FormProcessorInterface(domain) self.formdb = FormAccessors(domain) self.partial_submission = partial_submission
def __init__(self, instance=None, attachments=None, auth_context=None, domain=None, app_id=None, build_id=None, path=None, location=None, submit_ip=None, openrosa_headers=None, last_sync_token=None, received_on=None, date_header=None, partial_submission=False, case_db=None): assert domain, domain assert instance, instance assert not isinstance(instance, HttpRequest), instance self.domain = domain self.app_id = app_id self.build_id = build_id # get_location has good default self.location = location or couchforms.get_location() self.received_on = received_on self.date_header = date_header self.submit_ip = submit_ip self.last_sync_token = last_sync_token self.openrosa_headers = openrosa_headers or {} self.instance = instance self.attachments = attachments or {} self.auth_context = auth_context or DefaultAuthContext() self.path = path self.interface = FormProcessorInterface(domain) self.formdb = FormAccessors(domain) self.partial_submission = partial_submission # always None except in the case where a system form is being processed as part of another submission # e.g. for closing extension cases self.case_db = case_db self.is_openrosa_version3 = self.openrosa_headers.get(OPENROSA_VERSION_HEADER, '') == OPENROSA_VERSION_3
def submit_form_locally(instance, domain, max_wait=..., **kwargs): """ :param instance: XML instance (as a string) to submit :param domain: The domain to submit the form to :param max_wait: Maximum time (in seconds) to allow the process to be delayed if the project is over its submission rate limit. The value None means "do not throttle". The special value ... (Ellipsis) means "The caller did not pass in a value". (This was chosen because of the special meaning already assumed by None.) """ if max_wait is ...: max_wait = 0.1 if max_wait is not None: rate_limit_submission(domain, delay_rather_than_reject=True, max_wait=max_wait) # intentionally leave these unauth'd for now kwargs['auth_context'] = kwargs.get('auth_context') or DefaultAuthContext() result = SubmissionPost(domain=domain, instance=instance, **kwargs).run() if not 200 <= result.response.status_code < 300: raise LocalSubmissionError('Error submitting (status code %s): %s' % ( result.response.status_code, result.response.content.decode('utf-8', errors='backslashreplace'), )) return result
def test_auth_context(self): xml_data = self.get_xml('meta') _, xform, _ = submit_form_locally(xml_data, 'test-domain', auth_context=DefaultAuthContext()) self.assertEqual(xform.auth_context, {'doc_type': 'DefaultAuthContext'})
def submit_form_locally(instance, domain, **kwargs): # intentionally leave these unauth'd for now kwargs['auth_context'] = kwargs.get('auth_context') or DefaultAuthContext() result = SubmissionPost(domain=domain, instance=instance, **kwargs).run() if not 200 <= result.response.status_code < 300: raise LocalSubmissionError('Error submitting (status code %s): %s' % ( result.response.status_code, result.response.content, )) return result
def handle(self, domain, folder_path, **options): if not os.path.exists(folder_path): raise Exception('Folder path must be the path to a directory') for name in os.listdir(folder_path): form_dir = os.path.join(folder_path, name) if not os.path.isdir(form_dir): continue with open(os.path.join(form_dir, 'metadata.json'), 'r', encoding='utf-8') as meta: metadata = FormMetadata.wrap(json.load(meta)) form_path = os.path.join(form_dir, 'form.xml') if not os.path.exists(form_path) and os.path.isfile(form_path): self.stderr.write('{} missing'.format(form_path)) continue attachments_dict = {} for name in metadata.attachments: path = os.path.join(form_dir, name) if os.path.exists(path): file = open(path, 'rb') attachments_dict[name] = UploadedFile(file, name) else: self.stderr.write( 'WARN: missing attachment: {}'.format(path)) with open(form_path, 'r', encoding='utf-8') as form: xml_data = form.read() auth_type = metadata.auth_context.get('doc_type', None) if auth_type == 'AuthContext': auth_context = AuthContext.wrap( deepcopy(metadata.to_json()['auth_context'])) auth_context.domain = domain else: auth_context = DefaultAuthContext() result = submit_form_locally(xml_data, domain, attachments=attachments_dict, received_on=metadata.received_on, auth_context=auth_context, app_id=metadata.app_id, build_id=metadata.build_id) if not result.response.status_code == 201: self.stderr.write(str(result.response))
def process(xform): xform['auth_context'] = DefaultAuthContext().to_json()