Пример #1
0
    def show(self):

        key = "ds_{}".format(self.state['dataset'])
        if Store.exists(key):
            sections = Store.get(key)

            if self.state['section'] in sections.keys():
                data_elements = sections[
                    self.state['section']]['data_elements']
                data_element_index = int(self.state['data_element_index'])

                if data_element_index < len(data_elements):
                    # append the category option combination name is not default, then append it to the data element
                    # name
                    menu_text = data_elements[data_element_index][
                        'data_element_name']
                    cat_opt_combo_name = data_elements[data_element_index][
                        'category_option_combo_name']
                    menu_text += " - {}".format(
                        cat_opt_combo_name
                    ) if cat_opt_combo_name != 'default' else ""

                    return self.ussd_proceed(menu_text)
                else:
                    self.next()

        return self.ussd_end("No data sets found")
Пример #2
0
    def validate(self):
        key = "usr_{}".format(self.state['passcode'])
        if Store.exists(key):
            org_units = Store.get(key)
            if self.user_response in org_units.keys():
                self.state['org_unit'] = org_units[self.user_response]['id']
                self.save()
                return True

        return False
Пример #3
0
    def show(self):
        key = "usr_{}".format(self.state['passcode'])
        if Store.exists(key):
            org_units = Store.get(key)
            menu_text = "Org unit:\n"
            for key, value in org_units.items():
                menu_text += "{}. {}\n".format(key, value['name'])

            return self.ussd_proceed(menu_text)

        return self.ussd_end("No org unit found.")
Пример #4
0
    def show(self):
        key = "ds_{}".format(self.state['dataset'])
        if Store.exists(key):
            sections = Store.get(key)
            menu_text = "Section:\n"
            for key, value in sections.items():
                menu_text += "{}. {}\n".format(key, value['name'])

            return self.ussd_proceed(menu_text)

        return self.ussd_end("No sections found.")
Пример #5
0
    def show(self):
        key = "ou_{}".format(self.state['org_unit'])
        if Store.exists(key):
            datasets = Store.get(key)
            menu_text = "Data set:\n"
            for key, value in datasets.items():
                menu_text += "{}. {}\n".format(key, value['name'])

            return self.ussd_proceed(menu_text)

        return self.ussd_end("No data set found.")
Пример #6
0
 def next(self):
     key = "usr_state_{}".format(
         self.state['passcode'])  # used to restore an expired session
     if Store.exists(key) and Store.get(key) != self.session_id:
         from apps.dhis.ussd.screen import RestoreSessionScreen
         return RestoreSessionScreen(session_id=self.session_id,
                                     phone_number=self.phone_number).show()
     else:
         from apps.dhis.ussd.screen import OrgUnitScreen
         return OrgUnitScreen(session_id=self.session_id,
                              phone_number=self.phone_number).show()
Пример #7
0
    def validate(self):
        key = "ds_{}".format(self.state['dataset'])
        if Store.exists(key):
            sections = Store.get(key)
            if self.user_response in sections.keys():
                # self.state['section'] = sections[self.user_response]['id']
                self.state['section'] = self.user_response
                # Always reset the data element index in the selected section to 0 to show the first data element.
                self.state['data_element_index'] = 0
                self.save()
                return True

        return False
Пример #8
0
    def validate(self):
        key = "ou_{}".format(self.state['org_unit'])
        if Store.exists(key):
            datasets = Store.get(key)
            if self.user_response in datasets.keys():
                self.state['dataset'] = datasets[self.user_response]['id']
                self.state['period_type'] = datasets[
                    self.user_response]['period_type']
                self.state['open_future_periods'] = datasets[
                    self.user_response]['open_future_periods']
                self.save()
                return True

        return False
Пример #9
0
    def validate(self):
        key = "ds_{}".format(self.state['dataset'])
        if Store.exists(key):
            sections = Store.get(key)
            if self.state['section'] in sections.keys():

                # if the current section selected is not in the section visited list, add it
                if self.state['section'] not in self.state['sections_visited']:
                    self.state['sections_visited'].append(
                        self.state['section'])

                data_elements = sections[
                    self.state['section']]['data_elements']
                data_element_index = int(self.state['data_element_index'])
                data_element_value_type = data_elements[data_element_index][
                    'data_element_value_type']

                # validate the data element
                result = validate_data_element_by_value_type(
                    data_element_value_type, self.user_response)

                if result[0]:
                    data_element = data_elements[data_element_index][
                        'data_element_id']
                    category_option_combo = data_elements[data_element_index][
                        'category_option_combo_id']

                    # save the value that is received from the user in the state. The key is a concatenation of
                    # data element and category option combo ids.
                    key = "{}-{}".format(data_element, category_option_combo)
                    self.state['data_element_values'][key] = result[1]
                    self.save()

                    # save into database
                    save_values_to_database.delay(
                        self.state['dataset'], data_element,
                        category_option_combo, self.state['org_unit'],
                        self.state['passcode'], self.state['period'],
                        result[1], self.phone_number, self.session_id)
                    return True

        return False
Пример #10
0
    def next(self):

        key = "ds_{}".format(self.state['dataset'])
        if Store.exists(key):
            sections = Store.get(key)

            if self.state['section'] in sections.keys():
                data_elements = sections[
                    self.state['section']]['data_elements']
                data_element_index = int(self.state['data_element_index'])

                data_element_index += 1

                # If all data elements in the selected sections are visited, increment the index and loop
                if data_element_index < len(data_elements):
                    # save state
                    self.state['data_element_index'] = data_element_index
                    self.save()

                    return self.show()
                else:
                    # reset the data element index
                    self.state['data_element_index'] = 0

                    # If all sections are visited, show the save options screen
                    if len(sections.keys()) == len(
                            self.state['sections_visited']):
                        from apps.dhis.ussd.screen import SaveOptionsScreen
                        return SaveOptionsScreen(
                            session_id=self.session_id,
                            phone_number=self.phone_number).show()

                    # to fix circular dependency
                    from apps.dhis.ussd.screen import SectionScreen
                    # all sections are not visited so show section screen
                    return SectionScreen(
                        session_id=self.session_id,
                        phone_number=self.phone_number).show()
        return self.ussd_end("No data sets are found")
Пример #11
0
    def __init__(self, session_id, phone_number=None, user_response=None, level=Level.LOGIN):
        self.session_id = session_id
        self.phone_number = phone_number
        self.user_response = user_response
        self.level = level
        self.state = {
            'passcode': '',
            'level': Level.LOGIN,
            'org_unit': '',
            'period': '',
            'dataset': '',
            'period_type': '',
            'open_future_periods': '',
            'section': '',
            'begin_period': '',
            'direction': '-',
            'direction_change': False,
            'data_element_index': 0,
            'data_element_values': {},
            'sections_visited': []
        }

        if Store.exists(self.session_id):
            self.state = Store.get(self.session_id)
Пример #12
0
 def validate(self):
     if Store.exists("usr_{}".format(self.user_response)):
         self.state['passcode'] = self.user_response
         self.save()
         return True
     return False