@classmethod
    def get_schedule(cls, chw_username, override_date=None):
        """
        Generate schedule object for a given username
        """
        cached_schedules = None

        if override_date == None:
            nowdate = datetime.utcnow()
        else:
            nowdate = override_date

        day_intervaltree = {}
        if cached_schedules == None:
            #no documents, then we need to load them up
            db = CommCareCase.get_db()
            chw_schedules = db.view('pact/chw_dot_schedules', key=chw_username).all()
            to_cache = []
            for item in chw_schedules:
                single_sched = item['value']
                to_cache.append(single_sched)
            cache.set("%s_schedule" % (chw_username), json.dumps(to_cache), 3600)
            cached_arr = to_cache
        else:
            cached_arr = json.loads(cached_schedules)

        for single_sched in cached_arr:
            day_of_week = int(single_sched['day_of_week'])
            if day_intervaltree.has_key(day_of_week):
                daytree = day_intervaltree[day_of_week]
            else:
                #if there's no day of week indication for this, then it's just a null interval node.  To start this node, we make it REALLY old.
                daytree = IntervalNode(get_seconds(datetime.min),
                                       get_seconds(nowdate + timedelta(days=10)))
            if single_sched['ended_date'] == None:
                enddate = nowdate + timedelta(days=9)
            else:
                enddate = iso_string_to_datetime(single_sched['ended_date'])

            startdate = iso_string_to_datetime(single_sched['active_date'])
            case_id = single_sched['case_id']
            if single_sched.has_key('error'):
                #this is a non-showstopping issue due to quirks with older submissions
                logging.error("Error, no pactid: %s" % single_sched['error'])

            daytree.insert(get_seconds(startdate), get_seconds(enddate), other=case_id)
            day_intervaltree[day_of_week] = daytree
        return cls(chw_username, day_intervaltree, cached_arr)
dataset/ETHPy150Open dimagi/commcare-hq/custom/_legacy/pact/reports/chw_schedule.py/CHWPatientSchedule.get_schedule/original