fietsboek.models.user module

User models for fietsboek.

fietsboek.models.user.FINGERPRINT_SHAKE_BYTES = 10

Number of bytes to include in the SHAKE digest of the fingerprint.

class fietsboek.models.user.FriendRequest(**kwargs)

Bases: Base

Represents a request of friendship between two Users.

Variables:
  • id (int) – Database ID.

  • sender_id (int) – ID of the friendship initiator.

  • recipient_id (int) – ID of the request recipient.

  • date (datetime.datetime) – Date of the request.

  • sender (User) – Initiator of the friendship request.

  • recipient (User) – Recipient of the friendship.

date
id
recipient: Mapped[User]
recipient_id
sender: Mapped[User]
sender_id
exception fietsboek.models.user.PasswordMismatch

Bases: Exception

Exception that is raised if the passwords mismatch.

Using an exception forces the handling of the password mismatch and makes it impossible to accidentally ignore the return value of the password verification.

fietsboek.models.user.SESSION_SECRET_LENGTH = 32

Length of the secret bytes for the user’s session.

fietsboek.models.user.TOKEN_LIFETIME = datetime.timedelta(days=1)

Maximum validity time of a token.

class fietsboek.models.user.Token(**kwargs)

Bases: Base

A token is something that a user can use to perform certain account related functions.

A token with type TokenType.VERIFY_EMAIL can be used by the user to verify their email address.

A token with type TokenType.RESET_PASSWORD can be used by the user to reset their password.

Variables:
  • id (int) – Database ID.

  • user_id (int) – ID of the user.

  • uuid (str) – The token UUID.

  • token_type (TokenType) – The type of the token.

  • date (datetime.datetime) – Date of the token creation, in UTC.

  • user (User) – User that this token belongs to.

age()

Returns the age of the token.

Return type:

timedelta

date
classmethod generate(user, token_type)

Generate a new token for the given user.

Parameters:
  • user (User) – The user which to generate the token for.

  • token_type (TokenType) – The type of the token to generate.

Returns:

The generated token.

Return type:

Token

id
token_type
user: Mapped[User]
user_id
uuid
class fietsboek.models.user.TokenType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Type of tokens.

A token can be used either to verify the user’s email, or it can be used to reset the password.

RESET_PASSWORD = 2

A token that can be used to reset a user’s password.

VERIFY_EMAIL = 1

A token that can be used to verify a user’s email.

class fietsboek.models.user.User(**kwargs)

Bases: Base

A fietsboek user.

Variables:
  • id (int) – Database ID.

  • name (str) – Name of the user.

  • password (bytes) – (Hashed) Password of the user. Note that you should use set_password() and check_password() to interface with the password instead of accessing this field directly.

  • salt (bytes) – Password salt.

  • email (str) – Email address of the user.

  • session_secret (bytes) – Secret bytes for the session fingerprint.

  • is_admin (bool) – Flag determining whether this user has admin access.

  • is_verified (bool) – Flag determining whether this user has been verified.

  • tracks (list[fietsboek.models.track.Track]) – Tracks owned by this user.

  • tagged_tracks (list[fietsboek.models.track.Track]) – Tracks in which this user is tagged.

  • uploads (list[fietsboek.models.track.Upload]) – Currently ongoing uploads by this user.

  • tokens (list[fietsboek.models.user.Token]) – List of tokens that this user can use.

  • comments (list[fietsboek.model.comment.Comment]) – List of comments left by this user.

add_friend(friend)

Add the given user as a new friend.

Parameters:

friend (User) – The user to befriend.

all_tracks_query()

Returns a query that selects all the user’s tracks.

This includes the user’s own tracks, as well as any tracks the user has been tagged in.

The returned query can be further modified, however, you need to use sqlalchemy.orm.aliased() to access the correct objects:

>>> from fietsboek.models.track import Track, TrackType
>>> from sqlalchemy import select
>>> from sqlalchemy.orm import aliased
>>> user = retrieve_user()
>>> query = user.all_tracks_query()
>>> query = query.filter(query.c.type == TrackType.ORGANIC)
>>> query = select(aliased(Track, query))
Return type:

sqlalchemy.sql.expression.Selectable

authenticated_user_id()

Returns a string suitable to re-identify this user, e.g. in a cookie or session.

The returned ID contains a “fingerprint” to ensure that the re-identified user matches.

Return type:

str

Returns:

The string used to re-identify this user.

autocomplete_tags()

Returns all tags the user has ever used, suitable for autocompletion lists.

Returns:

All tags of the user.

Return type:

Iterator[str]

check_password(password)

Checks if the given password fits for the user.

Does nothing if the passwords match, raises an exception otherwise.

Raises:

PasswordMismatch – When the password does not match the stored one.

Parameters:

password (str) – The password to check.

comments: Mapped[list[Comment]]
email
classmethod factory(request)

Factory method to pass to a route definition.

This factory retrieves the user based on the user_id matched route parameter, and returns the user. If the user is not found, HTTPNotFound is raised.

Raises:

pyramid.httpexception.NotFound – If the user is not found.

Parameters:

request (Request) – The pyramid request.

Return type:

User

Returns:

The user.

favourite_tracks: Mapped[list[Track]]
friends_1: Mapped[list[User]]
friends_2: Mapped[list[User]]
classmethod get_by_authenticated_user_id(session, user_id)

Retrieves a user by their authenticated user ID (as returned by authenticated_user_id()).

This method returns None on any of the following conditions:

  • The user ID is malformed.

  • The user with the given ID was not found.

  • The fingerprint of the user_id and the database user do not match.

Parameters:
  • session (Session) – The database session which to retrieve the user from.

  • user_id (str) – The user ID.

Return type:

Optional[User]

Returns:

The user, if found.

get_friends()

Returns all friends of the user.

This is not a simple SQLAlchemy property because the friendship relation is complex.

Returns:

All friends of this user.

Return type:

list[User]

id
incoming_requests
is_admin
is_verified
name
outgoing_requests
password
principals()

Returns all principals that this user fulfills.

This does not include the Everyone and Authenticated principals, as those have to be checked before accessing the user.

Returns:

The seceurity principals that this user fulfills.

Return type:

list[str]

classmethod query_by_email(email)

Returns a query that can be used to query a user by its email.

This properly ensures that the email is matched case-insensitively.

Parameters:

email (str) – The email address to match.

Returns:

The prepared query.

Return type:

sqlalchemy.sql.selectable.Select

remove_friend(friend)

Remove the friend relationship between two users.

Parameters:

friend (User) – The befriended user.

roll_session_secret()

Rolls a new session secret for the user.

This function automatically generates the right amount of random bytes from a secure source.

salt
session_secret
set_password(new_password)

Sets a new password for the user.

This function automatically generates a new random salt and updates the stored password hash and salt value.

Parameters:

new_password (str) – The new password of the user.

tagged_tracks: Mapped[list[Track]]
toggle_favourite(track)

Toggles the favourite status for the given track.

Parameters:

track (Track) – The track to (un)favour.

tokens: Mapped[list[Token]]
tracks: Mapped[list[Track]]
uploads: Mapped[list[Upload]]
classmethod visible_tracks_query(user=None)

Returns a query that selects all tracks visible to the given user.

The user might be None, in which case tracks for public users are returned.

The returned query can be further modified, however, you need to use sqlalchemy.orm.aliased() to access the correct objects (see also all_tracks_query()).

Parameters:

user (User) – The user for which the tracks should be searched.

Return type:

sqlalchemy.sql.expression.Selectable