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, })
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') side_pane = options.get('side_pane', False) user = options.get('user', None) _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 = 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', None) or {} 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) request = options.get('request', None) user_can_edit = ( request and user and request.domain and (user.can_edit_data() or user.is_commcare_user()) ) show_edit_submission = ( user_can_edit and has_privilege(request, privileges.CLOUDCARE) and toggle_enabled(request, toggles.EDIT_SUBMISSIONS) ) # stuffing this in the same flag as case rebuild show_resave = ( user_can_edit and toggle_enabled(request, toggles.CASE_REBUILD) ) 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, "side_pane": side_pane, "user": user, "show_edit_submission": show_edit_submission, "show_resave": show_resave, })
def render_case(case, 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. """ from corehq.apps.hqwebapp.templatetags.proptable_tags import get_tables_as_rows, get_definition case = wrapped_case(case) timezone = options.get('timezone', pytz.utc) timezone = timezone.localize(datetime.datetime.now()).tzinfo _get_tables_as_rows = partial(get_tables_as_rows, timezone=timezone) display = options.get('display') or case.get_display_config() show_transaction_export = options.get('show_transaction_export') or False get_case_url = options['get_case_url'] data = copy.deepcopy(case.to_full_dict()) default_properties = _get_tables_as_rows(data, display) # pop seen properties off of remaining case properties dynamic_data = dict(case.dynamic_case_properties()) # hack - as of commcare 2.0, external id is basically a dynamic property # so also check and add it here if case.external_id: dynamic_data['external_id'] = case.external_id for section in display: for row in section['layout']: for item in row: dynamic_data.pop(item.get("expr"), None) if dynamic_data: dynamic_keys = sorted(dynamic_data.keys()) definition = get_definition( dynamic_keys, num_columns=DYNAMIC_CASE_PROPERTIES_COLUMNS) dynamic_properties = _get_tables_as_rows(dynamic_data, definition) else: dynamic_properties = None actions = case.to_json()['actions'] actions.reverse() the_time_is_now = datetime.datetime.now() tz_offset_ms = int(timezone.utcoffset(the_time_is_now).total_seconds()) * 1000 tz_abbrev = timezone.localize(the_time_is_now).tzname() # ledgers def _product_name(product_id): try: return SQLProduct.objects.get(product_id=product_id).name except SQLProduct.DoesNotExist: return (_('Unknown Product ("{}")').format(product_id)) ledgers = get_current_ledger_transactions(case._id) for section, product_map in ledgers.items(): product_tuples = sorted( (_product_name(product_id), product_map[product_id]) for product_id in product_map ) ledgers[section] = product_tuples return render_to_string("case/partials/single_case.html", { "default_properties": default_properties, "default_properties_options": { "style": "table" }, "dynamic_properties": dynamic_properties, "dynamic_properties_options": { "style": "table" }, "case": case, "case_actions": mark_safe(simplejson.dumps(actions)), "timezone": timezone, "tz_abbrev": tz_abbrev, "case_hierarchy_options": { "show_view_buttons": True, "get_case_url": get_case_url, "timezone": timezone }, "ledgers": ledgers, "timezone_offset": tz_offset_ms, "show_transaction_export": show_transaction_export, })
def render_case(case, 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. """ # todo: what are these doing here? from corehq.apps.hqwebapp.templatetags.proptable_tags import get_tables_as_rows, get_definition case = wrapped_case(case) timezone = options.get('timezone', pytz.utc) _get_tables_as_rows = partial(get_tables_as_rows, timezone=timezone) display = options.get('display') or case.get_display_config() get_case_url = options['get_case_url'] data = copy.deepcopy(case.to_full_dict()) default_properties = _get_tables_as_rows(data, display) # pop seen properties off of remaining case properties dynamic_data = dict(case.dynamic_case_properties()) # hack - as of commcare 2.0, external id is basically a dynamic property # so also check and add it here if case.external_id: dynamic_data['external_id'] = case.external_id for section in display: for row in section['layout']: for item in row: dynamic_data.pop(item.get("expr"), None) if dynamic_data: dynamic_keys = sorted(dynamic_data.keys()) definition = get_definition( dynamic_keys, num_columns=DYNAMIC_CASE_PROPERTIES_COLUMNS) dynamic_properties = _get_tables_as_rows(dynamic_data, definition) else: dynamic_properties = None actions = case.to_json()['actions'] actions.reverse() tz_abbrev = timezone.localize(datetime.datetime.now()).tzname() return render_to_string("case/partials/single_case.html", { "default_properties": default_properties, "default_properties_options": { "style": "table" }, "dynamic_properties": dynamic_properties, "dynamic_properties_options": { "style": "table" }, "case": case, "case_actions": mark_safe(simplejson.dumps(actions)), "timezone": timezone, "tz_abbrev": tz_abbrev, "case_hierarchy_options": { "show_view_buttons": True, "get_case_url": get_case_url, "timezone": timezone } })
# pop seen properties off of remaining case properties dynamic_data = dict(case.dynamic_case_properties()) # hack - as of commcare 2.0, external id is basically a dynamic property # so also check and add it here if case.external_id: dynamic_data['external_id'] = case.external_id for section in display: for row in section['layout']: for item in row: dynamic_data.pop(item.get("expr"), None) if dynamic_data: dynamic_keys = sorted(dynamic_data.keys()) definition = get_definition( dynamic_keys, num_columns=DYNAMIC_CASE_PROPERTIES_COLUMNS) dynamic_properties = _get_tables_as_rows(dynamic_data, definition) else: dynamic_properties = None actions = case.to_json()['actions'] actions.reverse() tz_abbrev = timezone.localize(datetime.datetime.now()).tzname() return render_to_string("case/partials/single_case.html", { "default_properties": default_properties, "default_properties_options": { "style": "table" },
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') side_pane = options.get('side_pane', False) user = options.get('user', None) 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_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(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', None) or {} 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) edit_session_data = {'user_id': meta_userID} if len(case_blocks) == 1 and case_blocks[0].get(case_id_attr): edit_session_data["case_id"] = case_blocks[0].get(case_id_attr) return render_to_string("reports/form/partials/single_form.html", { "context_case_id": case_id, "instance": form, "form_meta": options.get('form_meta', {}), "maps_api_key": settings.GMAPS_API_KEY, "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, "side_pane": side_pane, "user": user, "edit_session_data": edit_session_data, "request": options.get('request', None), # needed for toggles })
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 = options.get('timezone', pytz.utc) #display = options.get('display') 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_dict = form.top_level_tags() form_dict.pop('change', None) # this data already in Case Changes tab form_keys = [k for k in form_dict.keys() if form_key_filter(k)] form_data = _get_tables_as_columns(form_dict, get_definition(form_keys)) # 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_dict.pop('meta', {}) definition = get_definition(sorted_form_metadata_keys(meta.keys())) form_meta_data = _get_tables_as_columns(meta, definition) return render_to_string("form/partials/single_form.html", { "context_case_id": case_id, "instance": form, "is_archived": form.doc_type == "XFormArchived", "domain": domain, "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, })
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') readable_form_data = READABLE_FORM_DATA.enabled(domain) use_old_reason = '' case_id_attr = "@%s" % const.CASE_TAG_ID _get_tables_as_columns = partial(get_tables_as_columns, timezone=timezone) form_dict = form.top_level_tags() form_dict.pop('change', None) # this data already in Case Changes tab # Form Data tab if readable_form_data: try: form_data = get_readable_form_data(form) except QuestionListNotFound as e: readable_form_data = False use_old_reason = e.message if not readable_form_data: form_keys = [k for k in form_dict.keys() if form_key_filter(k)] form_data = _get_tables_as_columns(form_dict, get_definition(form_keys)) # 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_dict.pop('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, 'readable_form_data': readable_form_data, 'use_old_reason': use_old_reason, "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, })