Beispiel #1
0
def get_rec_session():

    MAX_LENGTH = 60
    for user, days in input.iteritems():
        result = []
        lst_pre_act = Activity.get_activity_by_priority(user)
        pref_activity = lst_pre_act[0]
        global done
        for day in days:
            session = {"user_id": user, "start_datetime": "", "end_datetime": "", "session_type": "NOT_NOTIFIED"}
            today = day.get("date")[:10]
            fslots = day.get("free_slots")
            rslots = day.get("rec_slots")
            done = False
            c_session = day.get("existing_session")

            # if existing session is available for the day
            if c_session:
                s_start = Util.convert_string_to_datetime(c_session.get('start_time'))
                s_end = Util.convert_string_to_datetime(c_session.get('end_time'))
                s_length = (s_end - s_start).seconds/60

                given_length = math.floor(day.get("duration").get(pref_activity))

                # TODO: add logic to check the max length of session

                delta = s_length + (given_length - s_length)
                new_end = s_start + timedelta(minutes=delta)

                # check for free slots if it contains the slot
                for slot in fslots:

                    tmp_st = Util.convert_string_to_datetime(today+" "+slot.get('start'))
                    tmp_end = Util.convert_string_to_datetime(today+" "+slot.get('end'))

                    if s_start >= tmp_st and new_end <= tmp_end:
                        session["start_datetime"] = str(s_start)
                        session["end_datetime"] = str(new_end)

                        # TODO: add session to database
                        result.append({day.get("date")[:10]: session})
                        done = True
                        break

            # if existing session is not available for the day
            else:
                # per rec session check conflict with available slots
                global done
                done = False
                for rec_session in rslots:
                    if done:
                        break
                    for a_session in fslots:
                        latest_start = max(Util.convert_string_to_datetime(today+" "+rec_session.get("start")),
                                           Util.convert_string_to_datetime(today+" "+a_session.get("start")))

                        earliest_end = min(Util.convert_string_to_datetime(today+" "+rec_session.get("end")),
                                           Util.convert_string_to_datetime(today+" "+a_session.get("end")))

                        temp_overlap = (earliest_end - latest_start).seconds/60
                        act_length = math.floor(day.get("duration").get(pref_activity))

                        if temp_overlap >= act_length:
                            session["start_datetime"] = str(latest_start)
                            session["end_datetime"] = str(latest_start + timedelta(minutes=act_length))
                            result.append({day.get("date")[:10]: session})
                            done = True
                            break

                for free_session in fslots:
                    free_session

        print result
def get_rec_sessions():
    input = get_user_free_rec_slots()
    act_type = Activity.get_activity_type()
    MAX_LENGTH = 45
    for user, days in input.iteritems():
        result = []
        lst_pre_act = Activity.get_activity_by_priority(user)
        pref_activity = lst_pre_act[0]
        global done
        for day in days:

            session = {"user_id": user, "start_datetime": "", "end_datetime": "", "session_status": "NOT_NOTIFIED",
                       "name": "Smart Session", "session_feedback_id": 0, "workout_type_id": act_type.get(pref_activity) }

            today = day.get("date")[:10]
            fslots = day.get("free_slots")
            rslots = day.get("rec_slots")
            done = False
            c_session = day.get("existing_session")

            # if existing session is available for the day
            if c_session:
                c_session = c_session[0]
                s_start = c_session.get('start_datetime')
                s_end = c_session.get('end_datetime')
                s_length = (s_end - s_start).seconds/60

                given_length = math.floor(day.get("duration").get(pref_activity))

                delta = s_length + (given_length - s_length)
                if(delta > MAX_LENGTH):
                    new_end = s_start + timedelta(minutes=MAX_LENGTH)
                else:
                    new_end = s_start + timedelta(minutes=delta)

                if(delta > MAX_LENGTH):
                    given_length = MAX_LENGTH

                # check for free slots if it contains the slot
                for slot in fslots:

                    tmp_st = Util.convert_string_to_datetime(today+" "+slot.get('start'))
                    tmp_end = Util.convert_string_to_datetime(today+" "+slot.get('end'))

                    if s_start >= tmp_st and new_end <= tmp_end:
                        session["start_datetime"] = str(s_start)
                        session["end_datetime"] = str(new_end)
                        
                        SessionModel.update_session(user, str(c_session.get("id")), session)
                        result.append({day.get("date")[:10]: session})
                        done = True
                        break

                for rec_slot in rslots:

                    tmp_st = Util.convert_string_to_datetime(today+" "+rec_slot.get('start'))
                    tmp_end = Util.convert_string_to_datetime(today+" "+rec_slot.get('end'))

                    tmp_length = (tmp_end - tmp_st).seconds/60

                    if(tmp_length > given_length):

                        session["start_datetime"] = str(tmp_st)
                        session["end_datetime"] = str(tmp_st + timedelta(minutes=given_length))
                        
                        SessionModel.update_session(user, str(c_session.get("id")), session)
                        result.append({day.get("date")[:10]: session})
                        done = True
                        break


            # if existing session is not available for the day
            else:
                # per rec session check conflict with available slots
                done = False
                for rec_session in rslots:
                    for a_session in fslots:
                        latest_start = max(Util.convert_string_to_datetime(today+" "+rec_session.get("start")),
                                           Util.convert_string_to_datetime(today+" "+a_session.get("start")))

                        earliest_end = min(Util.convert_string_to_datetime(today+" "+rec_session.get("end")),
                                           Util.convert_string_to_datetime(today+" "+a_session.get("end")))

                        temp_overlap = (earliest_end - latest_start).seconds/60
                        act_length = math.floor(day.get("duration").get(pref_activity))

                        if temp_overlap >= act_length or temp_overlap >= MAX_LENGTH:
                            session["start_datetime"] = str(latest_start)
                            if act_length> MAX_LENGTH:
                                session["end_datetime"] = str(latest_start + timedelta(minutes=MAX_LENGTH))
                            else:
                                session["end_datetime"] = str(latest_start + timedelta(minutes=act_length))

                            # TODO: add session to database
                            SessionModel.insert_user_session(user, session)
                            result.append({day.get("date")[:10]: session})
                            done = True
                            break

                    if done:
                        break