### Bad Example: Redundant setUpClass Execution Source: https://commcarehq.readthedocs.io/en/latest/testing.html Avoid running `setUpClass` multiple times for classes inheriting from the same base class if the setup operations are expensive. This can significantly slow down test execution. ```python # BAD EXAMPLE class MyBaseTestClass(TestCase): @classmethod def setUpClass(cls): ... class MyTestClass(MyBaseTestClass): def test1(self): ... class MyTestClassTwo(MyBaseTestClass): def test2(self): ... ``` -------------------------------- ### Install coverage.py Source: https://commcarehq.readthedocs.io/en/latest/test_coverage.html Install the coverage.py library using pip. This is the first step to enable code coverage analysis. ```bash $ pip install coverage ``` -------------------------------- ### Install Grunt globally Source: https://commcarehq.readthedocs.io/en/latest/js-guide/testing.html Install grunt and grunt-cli globally for command-line access. ```bash $ npm install -g grunt $ npm install -g grunt-cli ``` -------------------------------- ### Install dependencies Source: https://commcarehq.readthedocs.io/en/latest/js-guide/testing.html Install required npm packages using yarn. ```bash $ yarn install --frozen-lockfile ``` -------------------------------- ### Install and Run ESLint Locally Source: https://commcarehq.readthedocs.io/en/latest/js-guide/linting.html Install ESLint globally using npm and then run it on a specific file to check for errors and style issues. ```bash # Install it through npm: $ npm install -g eslint # Try it out locally if you like $ eslint path/to/file.js ``` -------------------------------- ### Bulk User Resource Example Query Source: https://commcarehq.readthedocs.io/en/latest/api.html An example query string for the bulk user resource. It demonstrates how to use parameters like 'q', 'fields', 'limit', and 'offset' to filter and paginate results. ```http ?q=foo&fields=username&fields=first_name&fields=last_name&limit=100&offset=200 ``` -------------------------------- ### Example Static File Path with CDN Source: https://commcarehq.readthedocs.io/en/latest/js-guide/static-files.html This example shows how a static JavaScript file path is prefixed with a CDN URL during compilation for efficient delivery. ```html ``` -------------------------------- ### Report API Data Format Example Source: https://commcarehq.readthedocs.io/en/latest/reporting.html Example structure of the data returned by the Report API. ```python data = [ { 'slug1': 'abc', 'slug2': 2 }, { 'slug1': 'def', 'slug2': 1 } ... ] ``` -------------------------------- ### Session Variables for Module A - Patient Case Example Source: https://commcarehq.readthedocs.io/en/latest/advanced_app_features.html Illustrates the session variables for Module A, including loading a 'patient' case and a variable for a 'new visit' case. ```text case_id: load patient case case_id_new_visit: id for new visit case ( uuid() ) ``` -------------------------------- ### Atom Feed XML Examples Source: https://commcarehq.readthedocs.io/en/latest/openmrs.html Example XML structures for patient and encounter Atom feeds used by MOTECH. ```xml Patient AOP OpenMRS bec795b1-3d17-451d-b43e-a094019f6984+32 OpenMRS Feed Publisher 2018-04-26T10:56:10Z Patient tag:atomfeed.ict4h.org:6fdab6f5-2cd2-4207-b8bb-c2884d6179f6 2018-01-17T19:44:40Z 2018-01-17T19:44:40Z Patient tag:atomfeed.ict4h.org:5c6b6913-94a0-4f08-96a2-6b84dbced26e 2018-01-17T19:46:14Z 2018-01-17T19:46:14Z Patient tag:atomfeed.ict4h.org:299c435d-b3b4-4e89-8188-6d972169c13d 2018-01-17T19:57:09Z 2018-01-17T19:57:09Z ``` ```xml Patient AOP OpenMRS bec795b1-3d17-451d-b43e-a094019f6984+335 OpenMRS Feed Publisher 2018-06-13T08:32:57Z Encounter tag:atomfeed.ict4h.org:af713a2e-b961-4cb0-be59-d74e8b054415 2018-06-13T05:08:57Z 2018-06-13T05:08:57Z Encounter tag:atomfeed.ict4h.org:320834be-e9c8-4b09-a99e-691dff18b3e4 2018-06-13T05:08:57Z 2018-06-13T05:08:57Z Encounter tag:atomfeed.ict4h.org:fca253aa-b917-4166-946e-9da9baa901da 2018-06-13T05:09:12Z 2018-06-13T05:09:12Z ``` -------------------------------- ### Good: Get by ID Source: https://commcarehq.readthedocs.io/en/latest/elasticsearch.html Use the 'get' API for fetching documents by their ID. This is a more efficient method compared to searching by ID. ```http GET /hqcases_2016-03-04/case/?_source_include=name ``` -------------------------------- ### Enable Dev Setup for Static Files Source: https://commcarehq.readthedocs.io/en/latest/js-guide/static-files.html Configure `localsettings.py` to disable compression and offline processing for development. This allows for server-side on-the-fly LESS compilation without combining files. ```python COMPRESS_ENABLED = False COMPRESS_OFFLINE = False ``` -------------------------------- ### Python Code Block with Pygments Source: https://commcarehq.readthedocs.io/en/latest/documenting.html This Python code block requires the 'pygments' library to be installed ('pip install pygments') for syntax highlighting. ```python # You need to "pip install pygments" to make this work. for i in range(10): like() ``` -------------------------------- ### Python Literal Code Block Example Source: https://commcarehq.readthedocs.io/en/latest/documenting.html A basic Python code example demonstrating a function and a loop, formatted as a literal code block. ```python def like(): print("I like Ice Cream") for i in range(10): like() ``` -------------------------------- ### Configure Sharded Form and Case Data Source: https://commcarehq.readthedocs.io/en/latest/databases.html Set up partitioned databases for form and case data using PLPROXY. Ensure total shards are a power of 2 and shard ranges are continuous and inclusive. ```python USE_PARTITIONED_DATABASE = True DATABASES = { 'proxy': { ... 'PLPROXY': { 'PROXY': True } }, 'p1': { ... 'PLPROXY': { 'SHARDS': [0, 511] } }, 'p2': { ... 'PLPROXY': { 'SHARDS': [512, 1023] } } } ``` -------------------------------- ### Session Variables for Module B (Child of A) - Initial Source: https://commcarehq.readthedocs.io/en/latest/advanced_app_features.html Shows the initial session variables for Module B, a child of Module A. It highlights different naming conventions for loading the 'mother' case and a 'child' case. ```text case_id_mother: load mother case case_id_child: load child case ``` -------------------------------- ### Ledger Balances Indicator Example Source: https://commcarehq.readthedocs.io/en/latest/ucr.html Creates columns for each specified product code, saving ledger data. Requires 'type', 'column_id', 'ledger_section', and 'product_codes'. An optional 'case_id_expression' can be provided. ```json { "type": "ledger_balances", "column_id": "soh", "display_name": "Stock On Hand", "ledger_section": "stock", "product_codes": ["aspirin", "bandaids", "gauze"], "case_id_expression": { "type": "property_name", "property_name": "_id" } } ``` -------------------------------- ### Conditional Expression Example Source: https://commcarehq.readthedocs.io/en/latest/_modules/corehq/apps/userreports/expressions/specs.html This example demonstrates a conditional expression that evaluates a test condition and returns one of two possible outcomes. Ensure data types are consistent for comparisons, like casting age to an integer. ```json { "type": "conditional", "test": { "operator": "gt", "expression": { "type": "property_name", "property_name": "age", "datatype": "integer" }, "type": "boolean_expression", "property_value": 21 }, "expression_if_true": { "type": "constant", "constant": "legal" }, "expression_if_false": { "type": "constant", "constant": "underage" } } ``` -------------------------------- ### Implement a custom SQL report using SqlData Source: https://commcarehq.readthedocs.io/en/latest/reporting.html Example implementation of a report class inheriting from SqlTabularReport and CustomProjectReport, demonstrating the configuration of columns, filters, and data aggregation. ```python class DemoReport(SqlTabularReport, CustomProjectReport): name = "SQL Demo" slug = "sql_demo" fields = ('corehq.apps.reports.filters.dates.DatespanFilter',) # The columns to include the the 'group by' clause group_by = ["user"] # The table to run the query against table_name = "user_report_data" @property def filters(self): return [ BETWEEN('date', 'startdate', 'enddate'), ] @property def filter_values(self): return { "startdate": self.datespan.startdate_param_utc, "enddate": self.datespan.enddate_param_utc, "male": 'M', "female": 'F', } @property def keys(self): # would normally be loaded from couch return [["user1"], ["user2"], ['user3']] @property def columns(self): return [ DatabaseColumn("Location", SimpleColumn("user_id"), format_fn=self.username), DatabaseColumn("Males", CountColumn("gender"), filters=self.filters+[EQ('gender', 'male')]), DatabaseColumn("Females", CountColumn("gender"), filters=self.filters+[EQ('gender', 'female')]), AggregateColumn( "C as percent of D", self.calc_percentage, [SumColumn("indicator_c"), SumColumn("indicator_d")], format_fn=self.format_percent) ] _usernames = {"user1": "Location1", "user2": "Location2", 'user3': "Location3"} # normally loaded from couch def username(self, key): return self._usernames[key] def calc_percentage(num, denom): if isinstance(num, Number) and isinstance(denom, Number): if denom != 0: return num * 100 / denom else: return 0 else: return None def format_percent(self, value): return format_datatables_data("%d%%" % value, value) ``` -------------------------------- ### OpenMRS Start Datetime Mapping Source: https://commcarehq.readthedocs.io/en/latest/openmrs.html Configuration for mapping the encounter start datetime from OpenMRS. This setting specifies the direction of data flow, the JSON path for the datetime, the corresponding case property, and the external and CommCare data types. ```json "openmrs_start_datetime": { "direction": "in", ``` -------------------------------- ### Run Django Tests Source: https://commcarehq.readthedocs.io/en/latest/testing.html Tests are invoked using the standard Django convention `./manage.py test` because the `nosetests` command does not perform necessary Django setup. ```bash ./manage.py test ``` -------------------------------- ### Retrieve all live cases with one query Source: https://commcarehq.readthedocs.io/en/latest/restore-logic.html Recursive SQL query to resolve the entire tree of parent and extension cases for a device in a single execution. ```sql with owned_case_ids as ( select case_id from cases where domain = OWNER_DOMAIN and owner_id in OWNER_IDS and is_open = true ), recursive parent_tree as ( -- parent cases (outgoing) select parent_id, child_id, child_type, array[child_id] as path from case_index where domain = OWNER_DOMAIN and child_id in owned_case_ids union -- parents of parents (recursive) select ci.parent_id, ci.child_id, ci.child_type, path || ci.child_id from case_index ci inner join parent_tree as refs on ci.child_id = refs.parent_id where ci.domain = OWNER_DOMAIN and not (ci.child_id = any(refs.path)) -- stop infinite recursion ), recursive child_tree as ( -- child cases (incoming) select parent_id, child_id, child_type, array[parent_id] as path from case_index where domain = OWNER_DOMAIN and (parent_id in owned_case_ids or parent_id in parent_tree) and child_type = EXTENSION union -- children of children (recursive) select ci.parent_id, ci.child_id, ci.child_type, path || ci.parent_id from case_index ci inner join child_tree as refs on ci.parent_id = refs.child_id where ci.domain = OWNER_DOMAIN and not (ci.parent_id = any(refs.path)) -- stop infinite recursion and child_type = EXTENSION ) select case_id as parent_id, null as child_id, null as child_type, null as path from owned_case_ids union select * from parent_tree union select * from child_tree ``` -------------------------------- ### Toxic Example: Global Variables Without Closure Source: https://commcarehq.readthedocs.io/en/latest/js-guide/code-organization.html This is a toxic example that should not be followed. It demonstrates the negative consequences of exposing variables and functions directly to the global scope without using a closure, leading to potential overwrites and namespace pollution. ```javascript /* This is a toxic example, do not follow */ // actually a global var myPrivateGreeting = "Hello"; // also a global var sayHi = function (name) { console.log(myPrivateGreeting + " from my module, " + name); }; // also a global myModule = { sayHi: sayHi, favoriteColor: “blue”, }; ``` -------------------------------- ### Initialize LedgerToElasticsearchPillow Source: https://commcarehq.readthedocs.io/en/latest/_modules/corehq/pillows/ledger.html Creates and configures the LedgerToElasticsearchPillow instance with Kafka change feed and checkpoint management. ```python def get_ledger_to_elasticsearch_pillow(pillow_id='LedgerToElasticsearchPillow', num_processes=1, process_num=0, **kwargs): """Ledger pillow Note that this pillow's id references Elasticsearch, but it no longer saves to ES. It has been kept to keep the checkpoint consistent, and can be changed at any time. Processors: - :py:class:`corehq.pillows.ledger.LedgerProcessor` """ assert pillow_id == 'LedgerToElasticsearchPillow', 'Pillow ID is not allowed to change' IndexInfo = namedtuple('IndexInfo', ['index']) checkpoint = get_checkpoint_for_elasticsearch_pillow( pillow_id, IndexInfo("ledgers_2016-03-15"), [topics.LEDGER] ) change_feed = KafkaChangeFeed( topics=[topics.LEDGER], client_id='ledgers-to-es', num_processes=num_processes, process_num=process_num ) return ConstructedPillow( name=pillow_id, checkpoint=checkpoint, change_feed=change_feed, processor=LedgerProcessor(), change_processed_event_handler=KafkaCheckpointEventHandler( checkpoint=checkpoint, checkpoint_frequency=100, change_feed=change_feed ), ) ``` -------------------------------- ### Define a CaseProperty Source: https://commcarehq.readthedocs.io/en/latest/openmrs.html Example JSON structure for referencing a specific case property. ```json { "case_property": "dob" } ``` -------------------------------- ### Define a CaseOwnerAncestorLocationField Source: https://commcarehq.readthedocs.io/en/latest/openmrs.html Example JSON structure for referencing a location metadata value. ```json { "doc_type": "CaseOwnerAncestorLocationField", "location_field": "openmrs_uuid" } ``` -------------------------------- ### Quickcache with Vary On Parameters Source: https://commcarehq.readthedocs.io/en/latest/caching_and_memoization.html Use @quickcache to cache function results based on specific parameters. The `vary_on` argument specifies which parameters (or their attributes) determine cache uniqueness. This example caches based on domain name and user ID. ```python @quickcache(['domain_obj.name', 'user._id'], timeout=10) def count_users_forms_by_device(domain_obj, user): return { FormAccessors(domain_obj.name).count_forms_by_device(device.device_id) for device in user.devices } ``` -------------------------------- ### Get User ES Processor Source: https://commcarehq.readthedocs.io/en/latest/_modules/corehq/pillows/user.html Returns a BulkElasticProcessor configured for user document indexing. ```python def get_user_es_processor(): return BulkElasticProcessor( elasticsearch=get_es_new(), index_info=USER_INDEX_INFO, doc_prep_fn=transform_user_for_elasticsearch, ) ``` -------------------------------- ### Implement Celery Task with Soil Progress Tracking Source: https://commcarehq.readthedocs.io/en/latest/celery.html Use DownloadBase and expose_cached_download to manage task progress and file availability. ```python from soil import DownloadBase from soil.progress import update_task_state from soil.util import expose_cached_download @task def my_cool_task(): DownloadBase.set_progress(my_cool_task, 0, 100) # do some stuff DownloadBase.set_progress(my_cool_task, 50, 100) # do some more stuff DownloadBase.set_progress(my_cool_task, 100, 100) expose_cached_download(payload, expiry, file_extension) ``` -------------------------------- ### Basic Shadow Module Menu Structure XML Source: https://commcarehq.readthedocs.io/en/latest/advanced_app_features.html Illustrates the basic menu structure for a shadow module, where it has its own unique menu element separate from its source module. ```xml ``` -------------------------------- ### Run Static File Management Commands Source: https://commcarehq.readthedocs.io/en/latest/js-guide/static-files.html Execute these commands in sequence to collect static files, fix LESS import paths, and compile JavaScript internationalization. This setup mimics production static file handling. ```bash manage.py collectstatic manage.py fix_less_imports_collectstatic manage.py compilejsi18n ``` -------------------------------- ### Get Reporting Groups Expression Source: https://commcarehq.readthedocs.io/en/latest/ucr.html Retrieves the reporting groups assigned to a given user ID. ```APIDOC ## Get Reporting Groups Expression ### Description Returns an array of reporting groups assigned to a provided user ID. Each element in the array represents one reporting group. ### Method N/A (Configuration Expression) ### Endpoint N/A (Configuration Expression) ### Parameters #### Request Body - **type** (string) - Required - Must be "get_reporting_groups". - **user_id_expression** (object) - Required - An expression that resolves to a user ID. ### Request Example ```json { "type": "get_reporting_groups", "user_id_expression": { "type": "property_path", "property_path": ["form", "meta", "userID"] } } ``` ### Response N/A (This is a configuration expression, not an API endpoint.) ``` -------------------------------- ### Get Total Documents Source: https://commcarehq.readthedocs.io/en/latest/es_query.html Returns the total number of documents that matched the query criteria from the ESQuerySet. ```python total ``` -------------------------------- ### Get Values Source: https://commcarehq.readthedocs.io/en/latest/es_query.html Retrieves specific fields from the query results, modeled after Django's `QuerySet.values`. ```python values(_*fields_) ``` -------------------------------- ### Run tests via command line Source: https://commcarehq.readthedocs.io/en/latest/js-guide/testing.html Execute tests for all apps or specific apps using Grunt. ```bash $ grunt test ``` ```bash $ grunt test: // (e.g. grunt test:app_manager) ``` ```bash $ grunt list ``` -------------------------------- ### GET /bulk_user Source: https://commcarehq.readthedocs.io/en/latest/api.html Retrieves a list of users in bulk with support for querying, pagination, and field selection. ```APIDOC ## GET /bulk_user ### Description This resource is used to get basic user data in bulk. It is optimized for speed and is useful for retrieving specific user attributes across a domain. ### Method GET ### Endpoint /bulk_user ### Parameters #### Query Parameters - **q** (string) - Optional - Query string to filter users. - **limit** (integer) - Optional - Maximum number of results returned. - **offset** (integer) - Optional - Use with limit to paginate results. - **fields** (string) - Optional - Restrict the fields returned to a specified set. ### Request Example ?q=foo&fields=username&fields=first_name&fields=last_name&limit=100&offset=200 ### Response #### Success Response (200) - **id** (string) - User ID - **email** (string) - User email address - **username** (string) - User username - **first_name** (string) - User first name - **last_name** (string) - User last name - **phone_numbers** (array) - List of user phone numbers ``` -------------------------------- ### Month Start Date Expression Source: https://commcarehq.readthedocs.io/en/latest/_modules/corehq/apps/userreports/expressions/date_specs.html Calculates the first day of the month for a given date expression. ```APIDOC ## Month Start Date Expression ### Description Calculates the first day of the month based on a provided date expression. ### Method N/A (Class definition) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```json { "type": "month_start_date", "date_expression": { "type": "property_name", "property_name": "dob" } } ``` ### Response #### Success Response (200) - **date** (datetime.date) - The first day of the month. #### Response Example ```json { "date": "2023-10-01" } ``` ``` -------------------------------- ### Calculate User Case Counts with TermsAggregation Source: https://commcarehq.readthedocs.io/en/latest/es_query.html Demonstrates how to group pregnancy cases by user and retrieve the document count for each bucket. ```python res = (CaseES() .domain(self.domain) .case_type('pregnancy') .date_range('opened_on', gte=startdate, lte=enddate)) .aggregation(TermsAggregation('by_user', 'opened_by') .size(0) buckets = res.aggregations.by_user.buckets buckets.user1.doc_count ``` -------------------------------- ### Define FormUserAncestorLocationField Configuration Source: https://commcarehq.readthedocs.io/en/latest/openmrs.html Example JSON configuration for a FormUserAncestorLocationField, which references a location metadata value. ```json { "doc_type": "FormUserAncestorLocationField", "location_field": "dhis_id" } ``` -------------------------------- ### Get Hits from ESQuerySet Source: https://commcarehq.readthedocs.io/en/latest/es_query.html Returns the actual documents (hits) from the raw Elasticsearch response within an ESQuerySet. ```python hits ``` -------------------------------- ### List extension points management command Source: https://commcarehq.readthedocs.io/en/latest/extensions.html Command to list all existing extension points and their implementations. ```bash python manage.py list_extension_points ``` -------------------------------- ### Get Document IDs from ESQuerySet Source: https://commcarehq.readthedocs.io/en/latest/es_query.html Returns only the document IDs from the raw Elasticsearch response within an ESQuerySet. ```python doc_ids ``` -------------------------------- ### Configure Manual Choice Lists Source: https://commcarehq.readthedocs.io/en/latest/ucr.html Provides a fixed set of options for the user to select from. ```json { "type": "choice_list", "slug": "role", "field": "role", "choices": [ {"value": "doctor", "display": "Doctor"}, {"value": "nurse"} ] } ``` -------------------------------- ### Define Iteration Number Expression Source: https://commcarehq.readthedocs.io/en/latest/_modules/corehq/apps/userreports/expressions/specs.html Returns the index of a repeat item starting from 0 when used with a base_item_expression. ```json { "type": "base_iteration_number" } ``` -------------------------------- ### Creating a Case in Unit Tests Source: https://commcarehq.readthedocs.io/en/latest/forms_and_cases.html Utilize the `CaseFactory` to create case instances in unit tests. The `run_with_all_backends` decorator should be applied if the test requires execution on both SQL and Couch. ```python from corehq.form_processor.tests.utils import run_with_all_backends from casexml.apps.case.mock import CaseFactory @run_with_all_backends def test_my_case_function(self): factory = CaseFactory(domain='foo') factory.create_case( case_type='my_case_type', owner_id='owner1', case_name='bar', update={'prop1': 'abc'} ) ``` -------------------------------- ### Identify Slow Function Source: https://commcarehq.readthedocs.io/en/latest/profiling.html Example of a typical Django view function that might need performance investigation. ```python from django.http import HttpResponse from corehq.apps.domain.decorators import login_and_domain_required from corehq.util.view_utils import Format @login_and_domain_required def location_export(request, domain): response = HttpResponse(mimetype=Format.from_format('xlsx').mimetype) response['Content-Disposition'] = 'attachment; filename="locations.xlsx"' dump_locations(response, domain) return response ```