zeeguu/core/model/user_reading_session.py
def update_reading_session(
        cls, db_session, event, user_id, article_id, current_time=datetime.now()
    ):
        """
        Main callable method that keeps track of the reading sessions.
        Depending if the event belongs to the opening, interaction or closing list of events
        the method creates, updates or closes a reading session

        Parameters:
        db_session = database session
        event = event string (based on the user_activity_data events,
                                check list at the beginning of this python file)
        user_id = user identifier
        article_id = article identifier
        current_time = when this parameter is sent, instead of using the datetime.now() value for the current time
                    we use the provided value as the system time (only used for filling in historical data)

        returns: The reading session  or None if none is found
        """
        most_recent_reading_session = cls._find_most_recent_session(
            user_id, article_id, db_session
        )

        if is_opening_or_interactive_event(event):
            if not most_recent_reading_session:  # If there is no active reading session
                UserReadingSession._close_user_reading_sessions(db_session, user_id)
                return cls._create_new_session(
                    db_session, user_id, article_id, current_time
                )

            else:  # Is there an active reading session
                # If the open reading session is still valid (within the reading_session_timeout window)
                if most_recent_reading_session._is_still_active(current_time):
                    return most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=False, current_time=current_time
                    )
                # There is an open reading session but the elapsed time is larger than the reading_session_timeout
                else:
                    most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=True, current_time=current_time
                    )
                    most_recent_reading_session._close_reading_session(db_session)
                    UserReadingSession._close_user_reading_sessions(db_session, user_id)
                    return cls._create_new_session(
                        db_session, user_id, article_id, current_time
                    )

        elif is_closing_event(event):
            if most_recent_reading_session:  # If there is an open reading session
                # If the elapsed time is shorter than the timeout parameter
                if most_recent_reading_session._is_still_active(current_time):
                    most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=False, current_time=current_time
                    )
                # When the elapsed time is larger than the reading_session_timeout,
                # we add the grace time (which is n extra minutes where n=reading_session_timeout)
                else:
                    most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=True, current_time=current_time
                    )
                return most_recent_reading_session._close_reading_session(db_session)
            else:  # If there is no open reading session for the specified article,
                # we close all the articles from the user
                UserReadingSession._close_user_reading_sessions(db_session, user_id)
                return None
        else:
            return None

        Main callable method that keeps track of the reading sessions.
        Depending if the event belongs to the opening, interaction or closing list of events
        the method creates, updates or closes a reading session

        Parameters:
        db_session = database session
        event = event string (based on the user_activity_data events,
                                check list at the beginning of this python file)
        user_id = user identifier
        article_id = article identifier
        current_time = when this parameter is sent, instead of using the datetime.now() value for the current time
                    we use the provided value as the system time (only used for filling in historical data)

        returns: The reading session  or None if none is found
        
Main callable method that keeps track of the reading sessions.
Depending if the event belongs to the opening, interaction or closing list of events
the method creates, updates or closes a reading session



The reading session  or None if none is found
Main callable method that keeps track of the reading sessions . Depending if the event belongs to the opening interaction or closing list of events the method creates updates or closes a reading session The reading session or None if none is found
def update_reading_session(
        cls, db_session, event, user_id, article_id, current_time=datetime.now()
    ):
        most_recent_reading_session = cls._find_most_recent_session(
            user_id, article_id, db_session
        )
        if is_opening_or_interactive_event(event):
            if not most_recent_reading_session:  
                UserReadingSession._close_user_reading_sessions(db_session, user_id)
                return cls._create_new_session(
                    db_session, user_id, article_id, current_time
                )
            else:  
                if most_recent_reading_session._is_still_active(current_time):
                    return most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=False, current_time=current_time
                    )
                else:
                    most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=True, current_time=current_time
                    )
                    most_recent_reading_session._close_reading_session(db_session)
                    UserReadingSession._close_user_reading_sessions(db_session, user_id)
                    return cls._create_new_session(
                        db_session, user_id, article_id, current_time
                    )
        elif is_closing_event(event):
            if most_recent_reading_session:  
                if most_recent_reading_session._is_still_active(current_time):
                    most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=False, current_time=current_time
                    )
                else:
                    most_recent_reading_session._update_last_action_time(
                        db_session, add_grace_time=True, current_time=current_time
                    )
                return most_recent_reading_session._close_reading_session(db_session)
            else:  
                UserReadingSession._close_user_reading_sessions(db_session, user_id)
                return None
        else:
            return None
def update_reading_session ( cls , db_session , event , user_id , article_id , current_time = datetime . now ( ) ) : most_recent_reading_session = cls . _find_most_recent_session ( user_id , article_id , db_session ) if is_opening_or_interactive_event ( event ) : if not most_recent_reading_session : UserReadingSession . _close_user_reading_sessions ( db_session , user_id ) return cls . _create_new_session ( db_session , user_id , article_id , current_time ) else : if most_recent_reading_session . _is_still_active ( current_time ) : return most_recent_reading_session . _update_last_action_time ( db_session , add_grace_time = False , current_time = current_time ) else : most_recent_reading_session . _update_last_action_time ( db_session , add_grace_time = True , current_time = current_time ) most_recent_reading_session . _close_reading_session ( db_session ) UserReadingSession . _close_user_reading_sessions ( db_session , user_id ) return cls . _create_new_session ( db_session , user_id , article_id , current_time ) elif is_closing_event ( event ) : if most_recent_reading_session : if most_recent_reading_session . _is_still_active ( current_time ) : most_recent_reading_session . _update_last_action_time ( db_session , add_grace_time = False , current_time = current_time ) else : most_recent_reading_session . _update_last_action_time ( db_session , add_grace_time = True , current_time = current_time ) return most_recent_reading_session . _close_reading_session ( db_session ) else : UserReadingSession . _close_user_reading_sessions ( db_session , user_id ) return None else : return None
Main callable method that keeps track of the reading sessions.
Main callable method that keeps track of the reading sessions .
"""
        Main callable method that keeps track of the reading sessions.
        Depending if the event belongs to the opening, interaction or closing list of events
        the method creates, updates or closes a reading session

        Parameters:
        db_session = database session
        event = event string (based on the user_activity_data events,
                                check list at the beginning of this python file)
        user_id = user identifier
        article_id = article identifier
        current_time = when this parameter is sent, instead of using the datetime.now() value for the current time
                    we use the provided value as the system time (only used for filling in historical data)

        returns: The reading session  or None if none is found
        """ # If there is no active reading session # Is there an active reading session # If the open reading session is still valid (within the reading_session_timeout window) # There is an open reading session but the elapsed time is larger than the reading_session_timeout # If there is an open reading session # If the elapsed time is shorter than the timeout parameter # When the elapsed time is larger than the reading_session_timeout, # we add the grace time (which is n extra minutes where n=reading_session_timeout) # If there is no open reading session for the specified article, # we close all the articles from the user
cls
db_session
event
user_id
article_id
current_time