Example #1
0
def cache_datasets_with_their_data_elements():
    for dataset in Dataset.objects.all():
        dataset_sections = {}
        for i, section in enumerate(dataset.section_set.all()):
            dataset_sections[i + 1] = {
                'name': section.name,
                'id': section.section_id,
                'data_elements': []
            }
            for sec_de in section.sectiondataelement_set.all():
                de = sec_de.data_element
                for coc in de.category_combo.categoryoptioncombo_set.all():
                    dataset_sections[i + 1]['data_elements'].append({
                        'data_element_name':
                        de.name,
                        'data_element_id':
                        de.data_element_id,
                        'category_option_combo_name':
                        coc.name,
                        'category_option_combo_id':
                        coc.category_option_combo_id,
                        'data_element_value_type':
                        de.value_type
                    })

        Store.set("ds_{}".format(dataset.dataset_id), dataset_sections)

    logger.info(
        'Caching datasets with sections, data elements and category combos  ............ Done'
    )
Example #2
0
def cache_users_with_their_assigned_org_units() -> List[dict]:
    org_units_to_cache = []

    for user in DHIS2User.objects.all():
        user_ou = {}
        for i, ou in enumerate(user.org_units.all()):
            user_ou[i + 1] = {'name': ou.name, 'id': ou.org_unit_id}
            if ou.org_unit_id not in org_units_to_cache:
                org_units_to_cache.append(ou.org_unit_id)

        Store.set("usr_{}".format(user.passcode), user_ou)

    logger.info('Caching user with their assigned org units ............ Done')

    return org_units_to_cache
Example #3
0
def cache_org_units_with_their_datasets(org_units_to_cache):
    for ou in org_units_to_cache:
        org_unit = OrgUnit.objects.get_or_none(org_unit_id=ou)
        if org_unit is not None:
            datasets = org_unit.dataset_set.all()
            org_unit_datasets = {}
            for i, dataset in enumerate(datasets):
                org_unit_datasets[i + 1] = {
                    'name': dataset.name,
                    'id': dataset.dataset_id,
                    'period_type': dataset.period_type,
                    'open_future_periods': dataset.open_future_periods
                }

            Store.set("ou_{}".format(org_unit.org_unit_id), org_unit_datasets)

    logger.info('Caching org units with their datasets ............ Done')
Example #4
0
    def next(self):
        key = "usr_state_{}".format(self.state['passcode'])
        if self.user_response == '1':
            # restore the previous session

            previous_session_id = Store.get(key)
            self.state = Store.get(previous_session_id)
            self.save(self.state['level'])

            # store the current user state and session id
            Store.set(key, self.session_id)

            # delete the previous session state and user state which stores the previous session id
            Store.delete(previous_session_id)

            return get_screen(self.session_id, self.phone_number,
                              self.user_response, self.state['level']).show()
        else:
            Store.delete(key)

            from apps.dhis.ussd.screen import OrgUnitScreen
            return OrgUnitScreen(session_id=self.session_id,
                                 phone_number=self.phone_number).show()
Example #5
0
 def __init__(self, session_id, phone_number, user_response=None):
     super().__init__(session_id, phone_number, user_response,
                      Level.ORG_UNITS)
     # store the current user state and session id
     Store.set("usr_state_{}".format(self.state['passcode']),
               self.session_id)
Example #6
0
 def save(self, level=None):
     self.state['level'] = self.level if level is None else level
     Store.set(self.session_id, self.state)