14.2.66. camcops_server.cc_modules.cc_session


Copyright (C) 2012-2019 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.

Implements sessions for web clients (humans).

class camcops_server.cc_modules.cc_session.CamcopsSession(ip_addr=None, last_activity_utc=None)[source]

Class representing an HTTPS session.

  • ip_addr – client IP address
  • last_activity_utc – date/time of last activity that occurred
classmethod delete_old_sessions(req: CamcopsRequest) → None[source]

Delete all expired sessions.

classmethod get_oldest_last_activity_allowed(req: CamcopsRequest) → pendulum.datetime.DateTime[source]

What is the latest time that the last activity (for a session) could have occurred, before the session would have timed out?

Calculated as now - session_timeout.

classmethod get_session(req: CamcopsRequest, session_id_str: Union[str, NoneType], session_token: Union[str, NoneType]) → CamcopsSession[source]

Retrieves, or makes, a new camcops_server.cc_modules.cc_session.CamcopsSession for this Pyramid Request, given a specific session_id and session_token.

classmethod get_session_for_tablet(ts: TabletSession) → CamcopsSession[source]

For a given camcops_server.cc_modules.cc_tabletsession.TabletSession (used by tablet client devices), returns a corresponding camcops_server.cc_modules.cc_session.CamcopsSession.

This also performs user authorization.

User authentication is via the camcops_server.cc_modules.cc_session.CamcopsSession.

classmethod get_session_using_cookies(req: CamcopsRequest) → CamcopsSession[source]

Makes, or retrieves, a new camcops_server.cc_modules.cc_session.CamcopsSession for this Pyramid Request.

The session is found using the ID/token information in the request’s cookies.

get_task_filter() → camcops_server.cc_modules.cc_taskfilter.TaskFilter[source]

Returns the camcops_server.cc_modules.cc_taskfilter.TaskFilter in use for this session.


Returns a formatted version of the date/time at which the last activity took place for this session.

login(user: camcops_server.cc_modules.cc_user.User) → None[source]

Log in. Associates the user with the session and makes a new token.

logout(req: CamcopsRequest) → None[source]

Log out, wiping session details. Also, perform periodic maintenance for the server, as this is a good time.


Returns the user’s username, or None.

class camcops_server.cc_modules.cc_session.SessionTests(methodName='runTest')[source]

Unit tests.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

camcops_server.cc_modules.cc_session.generate_token(num_bytes: int = 16) → str[source]

Make a new session token that’s not in use.

It doesn’t matter if it’s already in use by a session with a different ID, because the ID/token pair is unique. (Removing that constraint gets rid of an in-principle-but-rare locking problem.)