def form_to_event(self, program_id, xform, data_element_names, program_stage_id=None, te_inst_id=None):
        """
        Builds a dict representing a DHIS2 event

        :param program_id: The program can't be determined from form data.
        :param xform: An XFormInstance
        :param data_element_names: A dictionary mapping CCHQ form field names
                                   to DHIS2 tracked entity attribute names
        :param program_stage_id: Program stage for programs with stages
        :param te_inst_id: Tracked entity instance ID, for creating an event
                           with registration

        An example of an event: ::

            {
              "program": "eBAyeGv0exc",
              "orgUnit": "DiszpKrYNg8",
              "eventDate": "2013-05-17",
              "status": "COMPLETED",
              "storedBy": "admin",
              "coordinate": {
                "latitude": "59.8",
                "longitude": "10.9"
              },
              "dataValues": [
                { "dataElement": "qrur9Dvnyt5", "value": "22" },
                { "dataElement": "oZg33kd9taw", "value": "Male" },
                { "dataElement": "msodh3rEMJa", "value": "2013-05-18" }
              ]
            }

        See the DHIS2 `Events documentation`_ for more information.


        .. _Events documentation: https://www.dhis2.org/doc/snapshot/en/user/html/ch28s09.html

        """
        # For more information on the data to be sent from CCHQ to DHIS2, see
        # README.rst. Required data is given in 4.3 of the Specification
        if any(e not in self._data_elements for e in data_element_names.values()):
            self._fetch_data_elements()

        if xform.form.get('dhis_org_id'):
            org_unit = xform.form['dhis_org_id']
        else:
            case_id = xform.form['case']['@case_id']
            case = CommCareCase.get(case_id)
            if getattr(case, 'dhis_org_id', None):
                org_unit = case['dhis_org_id']
            else:
                # The case doesn't have an org unit.
                return
        event = {
            'program': program_id,
            'orgUnit': org_unit,
            'eventDate': xform.received_on.date(),
            'status': 'COMPLETED',
            'storedBy': self._username,
            'dataValues': self._get_event_data_values(xform, data_element_names)
        }
        if program_stage_id:
            event['programStage'] = program_stage_id
        if te_inst_id:
            event['trackedEntityInstance'] = te_inst_id
        if xform.metadata.location:
            event['coordinate'] = {
                'latitude': xform.metadata.location.latitude,
                'longitude': xform.metadata.location.longitude,
            }
        return event
dataset/ETHPy150Open dimagi/commcare-hq/custom/dhis2/models.py/Dhis2Api.form_to_event/original