コード例 #1
0
ファイル: test_reports.py プロジェクト: ye-man/commcare-hq
    def setUpClass(cls):
        super(TestReports, cls).setUpClass()
        with open(os.path.join(DATAPATH, 'taux.xml'), encoding='utf-8') as f:
            xml = f.read()
            xml_obj = ElementTree.fromstring(xml)
            xml_obj[2][4].text = cls.mobile_worker.get_id
            xml = ElementTree.tostring(xml_obj)
            cls.taux = submit_form_locally(
                xml,
                TEST_DOMAIN,
                auth_context=AuthContext(user_id=cls.mobile_worker.get_id,
                                         domain=TEST_DOMAIN,
                                         authenticated=True)).xform

        with real_pillow_settings():
            management.call_command('ptop_reindexer_fluff',
                                    'IntraHealthFormFluffPillow')
        cls.config = dict(
            domain=TEST_DOMAIN,
            startdate=datetime(2016, 2, 1),
            enddate=datetime(2016, 2, 29),
            visit="''",
            strsd=json_format_date(datetime(2016, 2, 1)),
            stred=json_format_date(datetime(2016, 2, 29)),
            empty_prd_code='__none__',
        )
コード例 #2
0
ファイル: xform_tags.py プロジェクト: xbryanc/commcare-hq
def _get_form_metadata_context(domain, form, support_enabled=False):
    meta = _top_level_tags(form).get('meta', None) or {}
    meta['received_on'] = json_format_datetime(form.received_on)
    meta['server_modified_on'] = json_format_datetime(
        form.server_modified_on) if form.server_modified_on else ''
    if support_enabled:
        meta['last_sync_token'] = form.last_sync_token

    definition = get_default_definition(sorted_form_metadata_keys(list(meta)))
    form_meta_data = get_tables_as_columns(meta,
                                           definition,
                                           timezone=get_timezone_for_request())
    if getattr(form, 'auth_context', None):
        auth_context = AuthContext(form.auth_context)
        auth_context_user_id = auth_context.user_id
        auth_user_info = get_doc_info_by_id(domain, auth_context_user_id)
    else:
        auth_user_info = get_doc_info_by_id(domain, None)
        auth_context = AuthContext(
            user_id=None,
            authenticated=False,
            domain=domain,
        )
    meta_userID = meta.get('userID')
    meta_username = meta.get('username')
    if meta_userID == 'demo_user':
        user_info = DocInfo(
            domain=domain,
            display='demo_user',
        )
    elif meta_username == 'admin':
        user_info = DocInfo(
            domain=domain,
            display='admin',
        )
    else:
        user_info = get_doc_info_by_id(domain, meta_userID)

    return {
        "form_meta_data": form_meta_data,
        "auth_context": auth_context,
        "auth_user_info": auth_user_info,
        "user_info": user_info,
    }
コード例 #3
0
 def setUpClass(cls):
     super(TestFluffs, cls).setUpClass()
     cls.table = cls.taux_sat_table
     cls.couverture = cls.couverture_table
     with open(os.path.join(DATAPATH, 'taux.xml')) as f:
         xml = f.read()
         cls.taux = submit_form_locally(
             xml,
             TEST_DOMAIN,
             auth_context=AuthContext(user_id=cls.mobile_worker.get_id,
                                      domain=TEST_DOMAIN,
                                      authenticated=True)).xform
     with open(os.path.join(DATAPATH, 'operateur.xml')) as f:
         xml = f.read()
         cls.couverture_form = submit_form_locally(
             xml,
             TEST_DOMAIN,
             auth_context=AuthContext(user_id=cls.mobile_worker.get_id,
                                      domain=TEST_DOMAIN,
                                      authenticated=True)).xform
コード例 #4
0
 def setUpClass(cls):
     super(TestFluffs, cls).setUpClass()
     cls.table = cls.taux_sat_table
     cls.couverture = cls.couverture_table
     with open(os.path.join(DATAPATH, 'taux.xml'), encoding='utf-8') as f:
         xml = f.read()
         xml_obj = ElementTree.fromstring(xml)
         xml_obj[2][4].text = cls.mobile_worker.get_id
         xml = ElementTree.tostring(xml_obj, encoding='utf-8')
         cls.taux = submit_form_locally(
             xml, TEST_DOMAIN, auth_context=AuthContext(
                 user_id=cls.mobile_worker.get_id, domain=TEST_DOMAIN, authenticated=True
             )
         ).xform
     with open(os.path.join(DATAPATH, 'operateur.xml'), encoding='utf-8') as f:
         xml = f.read()
         cls.couverture_form = submit_form_locally(
             xml, TEST_DOMAIN, auth_context=AuthContext(
                 user_id=cls.mobile_worker.get_id, domain=TEST_DOMAIN, authenticated=True
             )
         ).xform
コード例 #5
0
ファイル: test_fluffs.py プロジェクト: zbidi/commcare-hq
 def setUpClass(cls):
     super(TestFluffs, cls).setUpClass()
     cls.table = TauxDeSatisfactionFluff._table
     cls.couverture = CouvertureFluff._table
     with cls.engine.begin() as connection:
         cls.table.create(connection, checkfirst=True)
         cls.couverture.create(connection, checkfirst=True)
     with open(os.path.join(DATAPATH, 'taux.xml')) as f:
         xml = f.read()
         cls.taux = submit_form_locally(
             xml, TEST_DOMAIN, auth_context=AuthContext(
                 user_id=cls.mobile_worker.get_id, domain=TEST_DOMAIN, authenticated=True
             )
         )[1]
     with open(os.path.join(DATAPATH, 'operateur.xml')) as f:
         xml = f.read()
         cls.couverture_form = submit_form_locally(
             xml, TEST_DOMAIN, auth_context=AuthContext(
                 user_id=cls.mobile_worker.get_id, domain=TEST_DOMAIN, authenticated=True
             )
         )[1]
コード例 #6
0
    def setUpClass(cls):
        super(TestReports, cls).setUpClass()
        with open(os.path.join(DATAPATH, 'taux.xml')) as f:
            xml = f.read()
            cls.taux = submit_form_locally(
                xml,
                TEST_DOMAIN,
                auth_context=AuthContext(user_id=cls.mobile_worker.get_id,
                                         domain=TEST_DOMAIN,
                                         authenticated=True)).xform

        with real_pillow_settings():
            management.call_command('ptop_reindexer_fluff',
                                    'IntraHealthFormFluffPillow')
コード例 #7
0
    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))
コード例 #8
0
    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') 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') 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()

            response, form, cases = 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 response.status_code == 201:
                self.stderr.write(str(response))
コード例 #9
0
ファイル: test_reports.py プロジェクト: zbidi/commcare-hq
    def setUpClass(cls):
        super(TestReports, cls).setUpClass()
        cls.taux_table = TauxDeSatisfactionFluff._table
        with cls.engine.begin() as connection:
            cls.taux_table.create(connection, checkfirst=True)

        with open(os.path.join(DATAPATH, 'taux.xml')) as f:
            xml = f.read()
            cls.taux = submit_form_locally(
                xml,
                TEST_DOMAIN,
                auth_context=AuthContext(user_id=cls.mobile_worker.get_id,
                                         domain=TEST_DOMAIN,
                                         authenticated=True))[1]

        with real_pillow_settings():
            management.call_command(
                'ptop_reindexer_fluff',
                'TauxDeSatisfactionFluffPillow',
            )
コード例 #10
0
def render_form(form, domain, options):
    """
    Uses options since Django 1.3 doesn't seem to support templatetag kwargs.
    Change to kwargs when we're on a version of Django that does.
    
    """
    # don't actually use the passed in timezone since we assume form submissions already come
    # in in local time.
    # todo: we should revisit this when we properly handle timezones in form processing.
    timezone = pytz.utc
    case_id = options.get('case_id')

    case_id_attr = "@%s" % const.CASE_TAG_ID

    _get_tables_as_columns = partial(get_tables_as_columns, timezone=timezone)

    # Form Data tab
    form_data, question_list_not_found = get_readable_form_data(form)

    # Case Changes tab
    case_blocks = extract_case_blocks(form)
    for i, block in enumerate(list(case_blocks)):
        if case_id and block.get(case_id_attr) == case_id:
            case_blocks.pop(i)
            case_blocks.insert(0, block)

    cases = []
    for b in case_blocks:
        this_case_id = b.get(case_id_attr)
        try:
            this_case = CommCareCase.get(
                this_case_id) if this_case_id else None
            valid_case = True
        except ResourceNotFound:
            this_case = None
            valid_case = False

        if this_case and this_case._id:
            url = reverse('case_details', args=[domain, this_case._id])
        else:
            url = "#"

        definition = get_definition(sorted_case_update_keys(b.keys()))
        cases.append({
            "is_current_case": case_id and this_case_id == case_id,
            "name": case_inline_display(this_case),
            "table": _get_tables_as_columns(b, definition),
            "url": url,
            "valid_case": valid_case
        })

    # Form Metadata tab
    meta = form.top_level_tags().get('meta', {})
    definition = get_definition(sorted_form_metadata_keys(meta.keys()))
    form_meta_data = _get_tables_as_columns(meta, definition)
    if 'auth_context' in form:
        auth_context = AuthContext(form.auth_context)
        auth_context_user_id = auth_context.user_id
        auth_user_info = get_doc_info_by_id(domain, auth_context_user_id)
    else:
        auth_user_info = get_doc_info_by_id(domain, None)
        auth_context = AuthContext(
            user_id=None,
            authenticated=False,
            domain=domain,
        )
    meta_userID = meta.get('userID')
    meta_username = meta.get('username')
    if meta_userID == 'demo_user':
        user_info = DocInfo(
            domain=domain,
            display='demo_user',
        )
    elif meta_username == 'admin':
        user_info = DocInfo(
            domain=domain,
            display='admin',
        )
    else:
        user_info = get_doc_info_by_id(domain, meta_userID)

    return render_to_string(
        "reports/form/partials/single_form.html",
        {
            "context_case_id": case_id,
            "instance": form,
            "is_archived": form.doc_type == "XFormArchived",
            "domain": domain,
            'question_list_not_found': question_list_not_found,
            "form_data": form_data,
            "cases": cases,
            "form_table_options": {
                # todo: wells if display config has more than one column
                "put_loners_in_wells": False
            },
            "form_meta_data": form_meta_data,
            "auth_context": auth_context,
            "auth_user_info": auth_user_info,
            "user_info": user_info,
        })
コード例 #11
0
def render_form(form, domain, options):
    """
    Uses options since Django 1.3 doesn't seem to support templatetag kwargs.
    Change to kwargs when we're on a version of Django that does.

    """

    timezone = get_timezone_for_request()
    case_id = options.get('case_id')
    side_pane = options.get('side_pane', False)
    user = options.get('user', None)
    request = options.get('request', None)
    support_enabled = toggle_enabled(request, toggles.SUPPORT)

    _get_tables_as_columns = partial(get_tables_as_columns, timezone=timezone)

    # Form Data tab
    form_data, question_list_not_found = get_readable_data_for_submission(form)

    # Case Changes tab
    case_blocks = extract_case_blocks(form)
    for i, block in enumerate(list(case_blocks)):
        if case_id and block.get(const.CASE_ATTR_ID) == case_id:
            case_blocks.pop(i)
            case_blocks.insert(0, block)

    cases = []
    for b in case_blocks:
        this_case_id = b.get(const.CASE_ATTR_ID)
        try:
            this_case = CaseAccessors(domain).get_case(
                this_case_id) if this_case_id else None
            valid_case = True
        except ResourceNotFound:
            this_case = None
            valid_case = False

        if this_case and this_case.case_id:
            url = reverse('case_details', args=[domain, this_case.case_id])
        else:
            url = "#"

        definition = get_default_definition(
            sorted_case_update_keys(b.keys()),
            assume_phonetimes=(
                not form.metadata
                or (form.metadata.deviceID != CLOUDCARE_DEVICE_ID)),
        )
        cases.append({
            "is_current_case": case_id and this_case_id == case_id,
            "name": case_inline_display(this_case),
            "table": _get_tables_as_columns(b, definition),
            "url": url,
            "valid_case": valid_case
        })

    # Form Metadata tab
    meta = _top_level_tags(form).get('meta', None) or {}
    meta['received_on'] = json_format_datetime(form.received_on)
    if support_enabled:
        meta['last_sync_token'] = form.last_sync_token

    definition = get_default_definition(sorted_form_metadata_keys(meta.keys()))
    form_meta_data = _get_tables_as_columns(meta, definition)
    if getattr(form, 'auth_context', None):
        auth_context = AuthContext(form.auth_context)
        auth_context_user_id = auth_context.user_id
        auth_user_info = get_doc_info_by_id(domain, auth_context_user_id)
    else:
        auth_user_info = get_doc_info_by_id(domain, None)
        auth_context = AuthContext(
            user_id=None,
            authenticated=False,
            domain=domain,
        )
    meta_userID = meta.get('userID')
    meta_username = meta.get('username')
    if meta_userID == 'demo_user':
        user_info = DocInfo(
            domain=domain,
            display='demo_user',
        )
    elif meta_username == 'admin':
        user_info = DocInfo(
            domain=domain,
            display='admin',
        )
    else:
        user_info = get_doc_info_by_id(domain, meta_userID)

    user_can_edit = (request and user and request.domain
                     and (user.can_edit_data() or user.is_commcare_user()))
    show_edit_options = (user_can_edit
                         and can_edit_form_location(domain, user, form))
    show_edit_submission = (user_can_edit
                            and has_privilege(request, privileges.DATA_CLEANUP)
                            and not form.is_deprecated)

    show_resave = (user_can_edit and support_enabled)

    def _get_edit_info(instance):
        info = {
            'was_edited': False,
            'is_edit': False,
        }
        if instance.is_deprecated:
            info.update({
                'was_edited': True,
                'latest_version': instance.orig_id,
            })
        if getattr(instance, 'edited_on', None) and getattr(
                instance, 'deprecated_form_id', None):
            info.update({
                'is_edit': True,
                'edited_on': instance.edited_on,
                'previous_version': instance.deprecated_form_id
            })
        return info

    return render_to_string(
        "reports/form/partials/single_form.html",
        {
            "context_case_id": case_id,
            "instance": form,
            "is_archived": form.is_archived,
            "edit_info": _get_edit_info(form),
            "domain": domain,
            'question_list_not_found': question_list_not_found,
            "form_data": form_data,
            "cases": cases,
            "form_table_options": {
                # todo: wells if display config has more than one column
                "put_loners_in_wells": False
            },
            "form_meta_data": form_meta_data,
            "auth_context": auth_context,
            "auth_user_info": auth_user_info,
            "user_info": user_info,
            "side_pane": side_pane,
            "show_edit_options": show_edit_options,
            "show_edit_submission": show_edit_submission,
            "show_resave": show_resave,
        },
        RequestContext(request))
コード例 #12
0
                                 case_id=claim_id,
                                 case_name=host_name,
                                 case_type=CLAIM_CASE_TYPE,
                                 owner_id=restore_user.user_id,
                                 index={
                                     identifier:
                                     IndexAttrs(
                                         case_type=host_type,
                                         case_id=host_id,
                                         relationship=CASE_INDEX_EXTENSION,
                                     )
                                 }).as_xml()
    form_extras = {}
    if restore_user.request_user:
        form_extras["auth_context"] = AuthContext(
            domain=domain,
            user_id=restore_user.request_user_id,
            authenticated=True)
    submit_case_blocks(
        [
            ElementTree.tostring(claim_case_block,
                                 encoding='utf-8').decode('utf-8')
        ],
        domain=domain,
        form_extras=form_extras,
        username=restore_user.full_username,
        user_id=restore_user.user_id,
        device_id=device_id,
    )
    return claim_id