def store_user_id(self, user_id): """ Store a user's id for retrieval later :param user_id: the user id """ if EQ_SESSION_ID not in session: eq_session_id = str(uuid4()) logger.debug("creating new eq session id", session_id=eq_session_id) session[EQ_SESSION_ID] = eq_session_id eq_session = EQSession(eq_session_id, user_id) else: eq_session_id = session[EQ_SESSION_ID] logger.debug("got session id from session", session_id=eq_session_id) eq_session = self._get_user_session(eq_session_id) if eq_session is not None: logger.debug("got session from database", eq_session_id=eq_session.eq_session_id, user_id=eq_session.user_id, timestamp=eq_session.timestamp.isoformat()) with commit_or_rollback(db_session): # pylint: disable=maybe-no-member # session has a add function but it is wrapped in a session_scope which confuses pylint db_session.add(eq_session)
def delete(self): logger.debug("deleting users data", user_id=self.user_id) if self.exists(): questionnaire_state = self._get() with commit_or_rollback(db_session): # pylint: disable=maybe-no-member # session has a delete function but it is wrapped in a session_scope which confuses pylint db_session.delete(questionnaire_state)
def test_commit_after_inserts(): # Given with patch('app.data_model.database.db_session', autospec=True) as db_session: # When db_session action within commit_or_rollback with commit_or_rollback(db_session): db_session.add('data') # Then .add() followed by .commit() db_session.assert_has_calls([call.add('data'), call.commit()])
def add_or_update(self, data): if self.exists(): logger.debug("updating questionnaire data", user_id=self.user_id) questionnaire_state = self._get() questionnaire_state.set_data(data) else: logger.debug("creating questionnaire data", user_id=self.user_id) questionnaire_state = QuestionnaireState(self.user_id, data) with commit_or_rollback(db_session): # pylint: disable=maybe-no-member # session has a add function but it is wrapped in a session_scope which confuses pylint db_session.add(questionnaire_state)
def test_rollback_after_commit_fail(): # Given with patch('app.data_model.database.db_session', autospec=True) as db_session: db_session.commit.side_effect = IntegrityError(Mock(), Mock(), Mock()) try: # When db_session action within commit_or_rollback with commit_or_rollback(db_session): pass except IntegrityError: pass # Then .add() followed by .rollback() db_session.assert_has_calls([call.commit(), call.rollback()])
def clear(self): """ Removes a user id from the session """ logger.debug("SessionManager remove_user_id() - session %s", session) if EQ_SESSION_ID in session: eq_session_id = session[EQ_SESSION_ID] eq_session = self._get_user_session(eq_session_id) logger.debug("About to delete entry from eq_session table %s", eq_session) with commit_or_rollback(db_session): # pylint: disable=maybe-no-member # session has a delete function but it is wrapped in a session_scope which confuses pylint db_session.delete(eq_session) else: logger.debug("No eq session id exists")
def clear(self): """ Removes a user id from the session """ if EQ_SESSION_ID in session: eq_session_id = session[EQ_SESSION_ID] eq_session = self._get_user_session(eq_session_id) if eq_session is not None: logger.debug("deleting session from eq_session table", eq_session_id=eq_session.eq_session_id, user_id=eq_session.user_id, timestamp=eq_session.timestamp.isoformat()) with commit_or_rollback(db_session): # pylint: disable=maybe-no-member # session has a delete function but it is wrapped in a session_scope which confuses pylint db_session.delete(eq_session) else: logger.debug("no eq session id exists")
def store_user_id(self, user_id): """ Store a user's id for retrieval later :param user_id: the user id """ logger.debug("SessionManager store_user_id() - session %s", session) if EQ_SESSION_ID not in session: eq_session_id = str(uuid4()) logger.debug("Created new eq session id %s", eq_session_id) session[EQ_SESSION_ID] = eq_session_id eq_session = EQSession(eq_session_id, user_id) logger.debug("Constructed EQ Session object %s", eq_session) else: eq_session_id = session[EQ_SESSION_ID] logger.debug("Found eq_session_id %s in session", eq_session_id) eq_session = self._get_user_session(eq_session_id) logger.debug("Loaded object eq session %s", eq_session) logger.debug("About to commit to database") with commit_or_rollback(db_session): # pylint: disable=maybe-no-member # session has a add function but it is wrapped in a session_scope which confuses pylint db_session.add(eq_session)